Creating Multiple Instances of a Variable

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.
Last edited on
you need an object, use classes to instantiate objects.

check c++ faq lite on classes etc... or any other tutorial...
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];
}
Last edited on
Holy crap man, that's so simple I can't believe I didn't think of that.

Thanks a lot.
I just tried this out, and when I compile, it throws three errors about the array:


1
2
3
1>c:\documents and settings\student\desktop\flashcards\card_create.h(26) : error C2057: expected constant expression
1>c:\documents and settings\student\desktop\flashcards\card_create.h(26) : error C2466: cannot allocate an array of constant size 0
1>c:\documents and settings\student\desktop\flashcards\card_create.h(26) : error C2133: 'cards' : unknown size


EDIT: Nevermind, I've got it!

I used vector to dynamically create the arrays:

std::vector <int> array(numVariable);

It works perfectly, thanks for all your help guys.
Last edited on
Topic archived. No new replies allowed.