Trying to set user input into an array.

I'm wanting to make sure this is the proper way to do so.
Here is a code fragment:

1
2
3
4
5
6
7
8
9
10
11
12
13

   int a, b, c, d, e;

   int balance[5] = {a, b, c, d, e};
   
   cout <<  "Input 5 numbers." << endl;

   cin >> a;
   cin >> b;
   cin >> c;
   cin >> d;
   cin >> e;


Thanks in advance.
No it isn't. If you use a proper compiler you should get these errors.
Error	C4700	uninitialized local variable 'a' used	main.cpp 28	
Error	C4700	uninitialized local variable 'b' used	main.cpp 28	
Error	C4700	uninitialized local variable 'c' used	main.cpp 28	
Error	C4700	uninitialized local variable 'd' used	main.cpp 28	
Error	C4700	uninitialized local variable 'e' used	main.cpp 28	

A better way is to use a loop and read them into the array.
1
2
3
4
5
6
7
8
  const int MAX = 5;
  int balance[MAX] = { 0 };

  cout << "Input 5 numbers." << endl;
  for (int i = 0; i < MAX; i++)
  {
    cin >> balance[i];
  }
Would you be willing to explain why my example isn't correct?
I get that your way is proper, but. Why isn't mine?
Is it due to scoping?
There are a few thing.
As you can see in Thomas’s code he defines a const variable for the size. This is a better way of using known sizes in arrays.
Another thing is that you are doing cin into the variables a,b,c,d,e. When you are doing this. There is no longer any connection between these variables and the cells in your array.
Makes sense?
Try and play around with it.
Print out a,b... then print the array.
The best way of understanding is by trying different things
Please mark as solved if you feel like your question has been answered
cryptz wrote:
Would you be willing to explain why my example isn't correct?
I get that your way is proper, but. Why isn't mine?
Is it due to scoping?


Cryptz, scope does not come into picture here. I think you're a little confused but it's all fine. Consider int balance[5], here balance is an integer array. That means that each element of balance is equivalent to be an integer variable itself.

So what that means is, instead of
1
2
3
4
5
   cin >> a;
   cin >> b;
   cin >> c;
   cin >> d;
   cin >> e;


You can directly write:
1
2
3
4
5
   cin >> balance[0];
   cin >> balance[1];
   cin >> balance[2];
   cin >> balance[3];
   cin >> balance[4];

(keep in mind that array index start from 0 and not 1)

The above snippet would compile perfectly. In fact it's the equivalent to
1
2
3
4
  for (int i = 0; i < 5; i++)
  {
    cin >> balance[i];
  }


But it's better if you do it with for-loop because it makes it neater and also requires you to type much less.


Coming back to the confusion, when we reach this point at the code in your original snippet:
int balance[5] = {a, b, c, d, e}; the elements of balance (i.e balance[0], balance[1] etc) are being assigned a, b, c, d, e etc.

So it's equivalent to say
1
2
3
balance[0] = a;
balance[1] = b;
// and etc. 


But wait. What is a and what is b? The compiler doesn't know what is a and b and it can't assign just nothing to a variable so the compiler gets confused and complains about it.

Notice that if you initialized the variables to 0, still balance wouldn't change after you changed the values of a, b, c etc (instead it will remain to be 0). Because when you're assigning balance with those variables, you're only assigning their values and you're not holding those variables inside the array.

A good compiler wouldn't compile your script, but if your compiler does not complain then try printing the values of balance[0] and etc., you will get rubbish.

So in short, it's just simpler to use a for-loop like so ;)
1
2
3
4
  for (int i = 0; i < 5; i++)
  {
    cin >> balance[i];
  }
Last edited on
Topic archived. No new replies allowed.