Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

c programming noob question

  • 07-12-2011 09:38PM
    #1
    Registered Users, Registered Users 2 Posts: 58 ✭✭


    ok, Im trying to assign a string(?) to an array when presented with an integer input

    ANY help appreciated.
    total noob and I'm sure there's a better way, but trying ALL day, banging my head off the wall now.

    #include <stdio.h>
    main()
    {

    char fruit_name[10] = "none";
    int choice;

    printf( "Please Select a fruit:\n") ;
    printf( "Apple : 1\n") ;
    printf( "Orange : 2\n") ;
    printf( "Banana : 3\n") ;
    printf( "to cancel : 0\n") ;

    scanf( "%d", &choice) ;

    switch (choice)
    {
    case 1 :

    fruit_name = "apple";

    case 2 :

    fruit_name = "orange";

    case 3 :

    fruit_name = "banana";


    case 0 :

    printf( "Please Enter a VALID option: 1 or 2 or 3:\n" ) ;
    default :


    printf( "%s is Your Chosen Fruit\n", fruit_name ) ;
    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 58 ✭✭ihastakephoto


    break;
    added in


  • Registered Users, Registered Users 2 Posts: 7,156 ✭✭✭srsly78


    fruit_name is a char array not a string.

    C does not have strings, C++ does.

    What you are looking for is something like strcpy(), which despite it's misleading name copies char arrays like you want (sometimes known as c-style string, but is just a char array with 0 as the terminating character).


  • Registered Users, Registered Users 2 Posts: 58 ✭✭ihastakephoto


    ok,
    i want to try to write all functions myself so i understand from the basics.
    Do you know how I could display my selected fruit (char) then?
    so this choice could be dynamic and used for later printf's

    as in,
    ("how many %c's do you want?", fruit_name);
    "that will cost %d", price); << where i do simple mathematical operations behind this based on initial choice


  • Registered Users, Registered Users 2 Posts: 1,346 ✭✭✭carveone


    Not sure what that last question means. You mean like this?
    strcpy(fruit_name, "apple");
    
    ....
    printf("Fruit name is: %s\n", fruit_name);
    

    Or do you mean based on the number? If it's based on the number you enter, you can use a table. All programming can be done with a look up table :p

    I'll show you in a sec...


  • Registered Users, Registered Users 2 Posts: 1,346 ✭✭✭carveone


    char *fruit_table[] = { "None", "Apple", "Orange", "Banana" };
    

    That's an array of pointers to strings. Four strings actually but I left the array bounds empty so C will work it out instead (I'm too lazy).

    The strings are likely in some read only data segment somewhere so don't go writing to them! gcc in particular gets very grumpy if you do that.

    So, fruit_table[1] returns a "char *", a pointer to a string which is "Apple". You can use this pointer in the same way as you used your "fruit_name" array:

    So now you can do
    printf("Fruit is %s\n", fruit_table[choice]);
    

    Hope this helps. Or confuses. One of those...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,156 ✭✭✭srsly78


    If you want to write your own version of strcpy() then write a little function that has a loop and copies one character at a time from char* A to char* B, terminating when it hits a null (byte value 0). Or copy it in one go once you know the number of chars.


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Don't use strcpy, use strncpy.


  • Registered Users, Registered Users 2 Posts: 7,156 ✭✭✭srsly78




Advertisement
Advertisement