Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c programming noob question

Options
  • 07-12-2011 9:38pm
    #1
    Registered Users Posts: 57 ✭✭


    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 Posts: 57 ✭✭ihastakephoto


    break;
    added in


  • Registered Users Posts: 7,157 ✭✭✭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 Posts: 57 ✭✭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 Posts: 1,216 ✭✭✭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 Posts: 1,216 ✭✭✭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 Posts: 7,157 ✭✭✭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 Posts: 7,157 ✭✭✭srsly78




Advertisement