Put values into an array from cin

I am trying to put values from cin that is put in before a loop, into an array. However, when I output the array, the values are not stored in. (Actually garbage). What do you think?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main ()
{
    const int MAX_WEIGHT = 20;
    const int CARS = 5;
    int train_cars[CARS],
        num_boxes,
        num_loaded,
        total;

    cout << "How many boxes of freight should each car be loaded with?" << endl;
    cin >> num_boxes;
    for(int i = 0; i < CARS; i++)
    {
        num_boxes >> train_cars[i];

        cout << train_cars << endl;
    }
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main ()
{
    const int MAX_WEIGHT = 20;
    const int CARS = 5;
    int train_cars[CARS],
        num_boxes,
        num_loaded,
        total;

    std::cout << "How many boxes of freight should each car be loaded with?" << std::endl;
    std::cin >> num_boxes;
    for(int i = 0; i < CARS; i++)
    {
        train_cars[i] = num_boxes;

        std::cout << train_cars[i] << std::endl;
    }
}
Just for my understanding, in my head, the logic is: num_boxes "goes into" (or >>) the array of train_cars[i].

Why is that not correct?

Thanks for the input.
closed account (48T7M4Gy)
>> is the stream extraction operator. This means that a stream of data, say from the keyboard is extracted and stored as a variable. Yours was an interesting (mis) interpretation but your num_boxes is an integer stored in memory, not a stream of data. You didn't have to 'stream' num_boxes over to train_cars[i], you just make them equal with the = operator. Hope this helps. :)
It absolutely does. However, on the same topic, why would something like this work:

1
2
3
4
5
6
7
8
9
10
11
12
13
const int NUM_EMPLOYEES = 10
int employee[NUM_EMPLOYEES];


for(int i = 0; i < NUM_EMPLOYEES; i++)
     {
           cout << "Employee #" << (i+1) << ": "
           cin >> employee[i];
           cout << " has been added to the list" << endl;
     }




Where as the user is inputing the data directly into the array. VS. my code at the top where the user is inputting the data(just a single data in my code) into a variable, and then the variable is putting it into the array sub count?

It seems like the same thing to me, just with an added step.

Thanks again for the input.
every line in your example is moving data from or to a stream. so << >> work.

your original problem (line14) involved no streams. so no use of << >>
Last edited on
Im still getting garbage when I cout the array. My code now looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int CARS = 5;
int train_cars[CARS];

for(int i = 0; i < CARS; i++)
    {
        cin >> train_cars[i];
        
        for (int i = 0; i < CARS; i++)
        {
            cout << train_cars[i] << endl;
        }

    }



So , lets say that I cin an integer of 5, it SHOULD store 5 into each array, and then once it ends the loop, it goes to the next loop(which is inside the first loop) and displays the array sub i.

The output Im getting is:
5
4285968
2686656
2686712
2686924


Thanks for the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main ()
{
    const int NUM_CARS = 5; // NUM_CARS is a slightly better name
    int train_cars[NUM_CARS] = {0}; // initialise to all zeroes

    // first read in the values into the array
    for( int i = 0; i < NUM_CARS; ++i ) std::cin >> train_cars[i];

    // after all the values have been read, print them out
    for( int i = 0; i < NUM_CARS; ++i ) std::cout << train_cars[i] << ' ' ;
    std::cout << '\n' ;
}
So what I was doing wrong was not initializing the array to zero? How is that any different from not initializing?

Each element would contain the value of 0, where as if not initialized, each value would contain garbage value for the empty space.

In both of these circumstances, the value that is in the array is being overwritten by the input of cin. How is it different?

The confusion comes because Ive never initialized an array to anything before.


However, when I do use the code youve provided, and cout it (using 5 as a int that is put in from the user) , the values are:
5
0
0
0
0
partly, that is why you got garbage, now you get zeroes.

you are printing out the entire array after entering only the first value.

1
2
3
4
5
6
7
8
9
10
11
12
13
const int CARS = 5;
int train_cars[CARS];

for(int i = 0; i < CARS; i++)
    {
        cin >> train_cars[i];   // enter a value into the array
        
        for (int i = 0; i < CARS; i++) // print the entire array!
        {
            cout << train_cars[i] << endl;
        }

    }


unnest your loops like JLBorges shows.
Ah, now that works. Thank you.

So to the original problem, Getting that input from the user into a variable BEFORE then loop, and then using that variable for the loop. What is the correct syntax for that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const int CARS = 5;
    int train_cars[CARS] = {0},
        num_boxes,
        num_loaded,
        total,
        temp;

    cout << "How many boxes of freight should each car be loaded with?" << endl;
    cin >> num_boxes;
    for(int i = 0; i < CARS; i++)
    {
        num_boxes = train_cars[i];

    }
    cout << "Test" << num_boxes;
    for (int i = 0; i < CARS; i++)
        {
            cout << train_cars[i] << endl;
        }
Last edited on
Same issue here, I get zeroes as the output
1
2
3
4
5
cin >> num_boxes;
for (int i = 0; i < CARS; i++)
{
	train_cars[i] = num_boxes;
}
Perfect. Thank you all.
Topic archived. No new replies allowed.