Thread: using an input variable within a variable name

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    9

    Question using an input variable within a variable name

    Sorry if title is wrong im new to programming C and am stuck.

    Iv asked the tutor today how to do this and she wasn't sure how :/ basicaly i want to use a user inputed number as part of the call to a char variable. So say user inputs 225 the program uses 225 as part of the variable name to get the infomation eg 225 added to cake makes cake225 which displays "Chocolate Eclair". Iv include some code to show what i mean.

    Code:
    #include <conio.h>
    #include <stdio.h>
    void getcake (int num);
    char cake225[] = "Chocolate Eclair";
    char cake261[] = "Vanilla Slice";
    char cake262[] = "Custard Tart";
    char cake265[] = "Strawberry Meringue";
    char cake267[] = "Apple Turnover";
    char cake271[] = "Iced Finger";
    
    main()
    {
    int codenum;
    
    clrscr();
    printf("Please enter cake code ");
    scanf ("%i", &codenum);
    printf("you entered %s", cake+codenum); // <<Im unsure how the cake+codenum should be formatted.
    
    
    getch();
    return 0;
    }
    If user inputs 225 then codenum=225 - i want "cake+codenum" to tell the program that i want "cake225" but i keep getting the error "undefined symbol cake"

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    No you cant do this in printf
    Code:
    printf("&#37;s",cake+codenum);

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    My solution is store all your string in the characted pointer and you take the number from the user as an input and you increment that pointer to the takes value and print the string .And you will get the expedated string...
    Last edited by sreeramu; 12-11-2007 at 07:16 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Two obvious solutions:
    1. Using a table to convert from "number to name":
    Code:
    struct {
       int cake;
       char *name;
    } cakeToString[] = 
    {
        { 225, cake225 },
        { 261, cake261 }, 
        ...
        { 0, NULL }
    };
    Then walk through the list and find the cake number, and use the name.

    2. Use a switch statement:
    Code:
    char *getCakeName(int cake)
    {
       switch(cake) {
         case 225:  return cake225;
         case 261: return cake 261;
         ... 
         default: return "Invalid Cake";
       }
    }
    There are further solutions, but those are the first two that comes to mind. Which you prefer depends quite a bit on what the rest of the code is supposed to do, and perhaps you actually need some more functionality to be performed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    There is no way (as far as I know) to pick a variable by name like cake + (some number). That's just not how the compiler works. However, you can find another solution to the problem of picking a cake. I can think of two solutions there:

    1. A 2 dimensional array of chars (probably best implemented with pointers) where the first dimension is the codenum so cake255 would really be cake[255]. I don't recommend this because your cake numbers don't start at 0 or increment in a decent pattern.

    2. A list of cake names and a list of cake numbers. You can search through the cake numbers then pick out the corresponding cake name. Personally, I would wrap those 2 variables into a structure:
    Code:
    struct CAKE
    {
         char name[256];
         int number;
    };
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    @matsp
    good ideas ...There is no need any more things to solve this problem...

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Brad0407 View Post
    There is no way (as far as I know) to pick a variable by name like cake + (some number).
    It's possible, with a map. But that's probably way too complicated for this purpose, especially in C.
    Oh and main should explicitly return int. Don't leave put return type on your functions!
    Code:
    main() /* Bad */
    int main() /* Good! */
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sreeramu View Post
    No you cant do this in printf
    Code:
    printf("%s",cake+codenum);
    Yes you can, it just doesn't do what it looks like.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char cake225[] = "Chocolate Eclair";
    > char cake261[] = "Vanilla Slice";
    So can we assume that you start with
    char cake001[] = "Sponge";
    char cake002[] = "Chocolate";
    and so on all the way up to the numbers you've listed?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Making me hungry.

    But anyway, if Salem is right and you actually do have many, many cakes, consider putting them into a file, or even multiple files (if you actually have thousands ) .

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    9
    Thank you for your reply's

    The above code was just an example the actual code im doing only requires having 10 hard coded questions. I have already done most of the code and used Switch , Case to get the relevant question to be displayed but i was thinking that if you where to add alot more questions each of which is a constant variable at the top of the code

    Code:
    int numQuestions = 50;
    char q1[] = "question  a)ans1  b)ans2    c)ans3";
    char q2[] = "question  a)ans1  b)ans2    c)ans3";
    char q3[] = "question  a)ans1  b)ans2    c)ans3";
    // ... repeated for each question
    char q50[] = "question  a)ans1  b)ans2    c)ans3";
    that i could create a for loop that no matter how many questions you added would display them all using numQuestions for the amount ot times to run the loop

    Code:
    int i;
    for (i=1; i <=numQuestions; i++)
            printf ("%s", q+i); // where q+i represents q1 then q2 then q3 etc.
    i was just thinking that if you where able to call each question using q+i , q(i) or however it would be formatted if it worked, then it would save alot of code through not having to add a Switch
    Code:
    case 1: question = q1;
    break;
    for every question that was added

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It works, if you make it an array:
    Code:
    char mystr[10][50];
    strcpy(mystr[0], "My question 1");
    /* Etc */
    for (i=1; i <=numQuestions; i++)
            printf ("&#37;s", q[i]); // where q+i represents q1 then q2 then q3 etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    9
    Thank you gonna go experiment now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a Variable as a string input to a function
    By gaza2k1 in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2007, 01:04 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Choosing a variable based on user text input.
    By Compiling... in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2005, 01:21 AM
  4. for loop not letting me input variable data
    By RealityFusion in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 04:29 PM
  5. finding the type of a input variable
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2002, 08:40 AM