# Putting number from cin into an arry

• 04-01-2011
Dontgiveup
Putting number from cin into an arry
Hi I would like to write a small program that takes the input of integers and puts them in an array, from where one can do things like calculating their mean, median, standard deviation and so forth. My Problem is that, after extracting the numbers one by one, I am stuck at putting them in an array. One problem is that the array where I stored the numbers is only accessible in the for-loop, I would appreciate if someone could help. Here is it:

Code:

```#include <iostream> using namespace std; int main(){     int array[50], x, n, sum =0;     cout << "Enter a number" << endl;     cin >> x;     int num = x;     for(x; x !=0; x = x/10)  sum++;//Trying to find the number of digits given     for(num; num != 0; num = num/10){             n = num%10;             array [sum - 1] = n; //I am trying to put in the numbers in an array             //in a back-to-front manner so I start with filling the last number in the array             //and looping to the first array, i.e array[0]             sum--;             cout << array[sum];             }             //But my Problem is that these variables are only valid within the for             //loop so I can't access them outside of the for-loop although I need them             // It would be grateful if someone can tell me how I can accessay array[sum]             //outside of the for-loop.      return 0; }```
• 04-01-2011
Mozza314
Can you explain what you mean by not being able to access your variables outside the for loop? In the code you've provided you should be able to access all those variables outside the for loop.

Also, please format your code better, something like this:

Code:

```#include <iostream> using namespace std; int main() {     int array[50], x, n, sum =0;     cout << "Enter a number" << endl;     cin >> x;     int num = x;     for (; x !=0; x = x/10)         sum++; //Trying to find the number of digits given     for (; num != 0; num = num/10)     {         n = num%10;         array [sum - 1] = n; //I am trying to put in the numbers in an array         //in a back-to-front manner so I start with filling the last number in the array         //and looping to the first array, i.e array[0]         sum--;         cout << array[sum];     }     //But my Problem is that these variables are only valid within the for     //loop so I can't access them outside of the for-loop although I need them     // It would be grateful if someone can tell me how I can accessay array[sum]     //outside of the for-loop.     return 0; }```
• 04-02-2011
Dontgiveup
What I meant is that the for-loop produces the numbers individually starting with the last digit. So if I input 1234, my array[sum] would contain [4321]. Now this array[sum] = 4321 exists only within the for-loop. Outside of the for-loop, if I write cout<<array[sum] I only get array[1] which is because I only get the last value of the for-loop. What I want is if there is a way I can access array[sum] that contains all the numbers 4321 outside of the for-loop.
• 04-02-2011
Mozza314
Quote:

Originally Posted by Dontgiveup
What I meant is that the for-loop produces the numbers individually starting with the last digit. So if I input 1234, my array[sum] would contain [4321]. Now this array[sum] = 4321 exists only within the for-loop. Outside of the for-loop, if I write cout<<array[sum] I only get array[1] which is because I only get the last value of the for-loop. What I want is if there is a way I can access array[sum] that contains all the numbers 4321 outside of the for-loop.

It sounds like you're having trouble understanding arrays. array is created in main(), not the for loop. You definitely have access to it until main() finishes. array[sum] is not the array, it is a particular element of the array. If your array has 4 elements, then you can access them as array[0], array[1], array[2], array[3].
• 04-03-2011
Dontgiveup
OK, thanks got it. Have already done it.