Creating a user defined number of variables

This is a discussion on Creating a user defined number of variables within the C++ Programming forums, part of the General Programming Boards category; Hi, is there any way I can make a user-defined number of variables and then call on them with the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69

    Question Creating a user defined number of variables

    Hi, is there any way I can make a user-defined number of variables and then call on them with the variable name with a number at the end or beginning or something? (eg. How many names do you want to enter?: 30) and then call them with something like this (<< name1) and then (<< name2) or (<< 1name) and then << 2name or something similar.
    Thank you,
    Last edited by beanroaster; 09-14-2005 at 03:36 PM.
    Adam

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You'll be wanting arrays.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I would use a vector before using an array. Either way, you will have one variable (the vector or array) and use indexing to access each entry (e.g. name[0], name[1], etc).

  4. #4
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Ok, but I want to have the user store the variables not just pull them out of a list. Is there a way to do that with arrays or something else?
    Adam

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    What do you mean the user store the variables? Liek the user is given a variable name in which the data is stored?
    -signed confused

  6. #6
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    What do you mean the user store the variables? Liek the user is given a variable name in which the data is stored?
    No, let the user (store, input, type in, enter, fill in, define, tell the computer what it is etc...) the variable.
    Adam

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Daved
    I would use a vector before using an array. Either way, you will have one variable (the vector or array) and use indexing to access each entry (e.g. name[0], name[1], etc).
    Usually I'd agree with you, but the concept of arrays is basic, useful knowledge that the OP is apparently lacking. In my opinion, it's better to understand a concept before trying to use an abstraction of the concept.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    is there any way I can use it like this though?:
    Code:
    int numofvar, counter;
               counter = 1;
               cout  << "How many variables do you want to enter?: ";
               cin << numofvar;
               while (counter <= numofvar)
               {
                char variable + counter[4];//how can i make this variable keep changing names until the loop is finished?
               cout << "Enter variable " << counter << ": ";
               cin variable[counter];//is there any way i can do that?
               cout << "Variable #" << counter " is " << variable << ".";
               counter++;
               }
    Obiously I just thought of that as an example so please don't be critical about anything basic missing. But is there any way to do that?
    Adam

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Here is a very simple program illustrating using arrays.
    Code:
    int main()
    {
    	int array[100];
    	int numEntries;
    	do
    	{
    		cout << "How many numbers would you like to enter? (Max: 100)"
    			<< endl;
    		cin >> numEntries;
    	} while (numEntries > 100);
    
    	for(int index = 0; index < numEntries; index++)
    	{
    		cout << "Enter a number: ";
    		cin >> array[index];
    	}
    
    	cout << "You entered: ";
    	for(int index = 0; index < numEntries; index++)
    		cout << array[index];
    }
    [edit]You'll really want to read the tutorial on arrays.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Thanks, I'll try that.
    Adam

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Usually I'd agree with you, but the concept of arrays is basic, useful knowledge that the OP is apparently lacking. In my opinion, it's better to understand a concept before trying to use an abstraction of the concept.

    This may be true, but I would submit that a vector would be a more appropriate tool for learning the array concept if you were to do so in C++. Either method should work for the OP's needs, though, so it is not terribly important.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 10:57 PM
  2. Sum of User Defined Matrix Powers
    By QuaX in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:33 AM
  3. User defined functions
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 03:53 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 09:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21