Thread: Putting number from cin into an arry

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    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;
    }

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    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;
    }

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    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.

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Dontgiveup View Post
    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].

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    OK, thanks got it. Have already done it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL DC Buffer Renders slow
    By Lane the Great in forum Game Programming
    Replies: 10
    Last Post: 01-07-2011, 07:52 PM
  2. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  3. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM