Thread: storing an unknown amount of variable

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    storing an unknown amount of variable

    hey, i've asked this on the c++ forum but got kind of cryptic answers i have a variable that lets say i = 15 and a switch statement that loops until i = 0 now depending on what they choose it subtracts a different amount, i want to somehow save their choices to be printed at the end, i was going to make a class that set all of the switch choices to 0 and if that particular choice is made set it to one, then only print the ones that = 1, but this greatly increased the size of my source code, so is there a better way to achieve this? i can include source if needed.
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You could store these variables in an array - which is like a whole bunch of different variables, referred to as a group by one name, but you can refer to individual variables by number. So what I would do, is set up an array, and every time the user makes a choice store that choice in the array. Each time your loop repeats, you increment the "index number" by one to store the next choice in the next slot in your array.

    The only problem you might have with arrays is that once you declare them, they're a set size, so you'll have to set a limit on the number of choices your user can enter. But once you get a little more advanced, you could have a look at vectors (not found in C - they're a C++ only concept), which expand as needed.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    but say i have an array set to 50 and they only choose 10 dif. times how do i print the 10 w/o the other 40? like i said before, i could set them all to 0 and when it is chosen it gets set to 1 and only the variables set to 1 will be printed but this could greatly increase my code
    Last edited by sreetvert83; 09-14-2005 at 10:00 AM.
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  4. #4
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Code:
    int array[50];
    int i;
    
    for(i=0; i<50; i++)
          array[i] = 0;   // This will set all of array to 0's
    
    .......
    .......
    
    for(i=0; i<50; i++)
          if(array[i] != 0)
                 printf("%d\n", array[i]);

    How does that greatly increase your code?

    You could do that in a 2d array but it's probably impractical depending what you're completely asking for.

    Just using for loops will basically print anything that isn't a 0 so just store the choices in the array and it won't print the things you haven't stored.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    Code:
    printf("%d\n", array[i]);
    is that c? what is the c++ equivilant?
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    cout << array[i] << endl;
    [edit]
    Code:
    printf("%d\n", array[i]);
    The %d means an integer. It matches the integer in the argument list, array[i]. If there are multiple %'s, the order matchs the arguments:
    Code:
    printf("%d.%d\n", var1, var2);
    // cout << var1 << "." << var2 << endl;
    [/edit]
    Last edited by dwks; 09-17-2005 at 05:47 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Storing an IP Address in a Variable?
    By guitarlover317 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2006, 07:54 AM
  3. Parsing and tracking unknown variable names
    By Wraithan in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2006, 08:03 PM
  4. Replies: 4
    Last Post: 11-15-2005, 02:13 PM
  5. I need help badly
    By taz_jiggy in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 09:36 PM