I am currently writing a program in which I need to create a variable, then dynamically create a number of child variables based in the input of the user. Then I need to be able to assign a string to each child variable. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int count;
int variable; //needs to have many instances
int x;
int y;
cout << "Input a number" << endl;
cin >> count;
y == count;
while ((count) > 0)
{
cout << "input a variable for" << x;
cin >> variable.y
((count) - 1);
x++;
}
If I made any mistakes in the code, don't bother pointing them out, this code isn't what I'm trying to run, it's just there to give you an idea of what I'm trying to do.
EDIT: I know a multi-dimensional array would fit my description pretty well, but arrays can't be dynamically allocated, as far as I know.
So does anyone know how to do this? It would help me a lot, and I would greatly appreciate any answers.
This really didn't help much. I've looked through the tutorials for classes, but I can't figure out how to apply that to my program so that the variable is instanced.
*points out the errors in ur code*
y == count; // result is a boolean, not assignment, assignment is =
((count) - 1); // lol... did not assign anything, use count -= 1, count--, or count = count - 1
now, for the code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//number of values user is allowed to enter
// - and size of the array
int numVariables;
cout << "Input a number: ";
cin >> numVariables;
// you can use an integer variable as the size of the array
int variables[numVariables];
for(int i = 0; i < numVariables; i++)
{
cout << "Enter the next value: ";
cin >> variables[i];
}