Arrays output.

Got another array issue. I am trying to display the cin from the user in the loop while dealing with arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  const int CARS = 5;
  int train_cars[CARS],
  char y, n;
  char answer;

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

    }
    cout << "***************Begin Loading***************" << endl;
    for (int count = 0; count < CARS; count++)
        {
            cout << "Load car" << (count+1) << "?" << endl;
            cin >> answer;
            if(answer = 'y')
                cout << num_boxes << " ";
                cout << "Train car #" << (count+1) << " has been loaded with "<<
                cout << num_boxes << " ";
                cout << num_boxes << " boxes." << endl;
        }


The issue: in the second for loop, cout num_boxes displays correctly, the second num_boxes does not, and the third num_boxes displays correctly.

I only have the first and third num_boxes to test the variable and test the code. I dont know why the second num_boxes(the one I actually need) is displaying incorrectly, regardless of if the other num_boxes are present or not.


EXAMPLE: If the user inputs 5 as the cin integer, the console displays:

How many boxes of freight should each car be loaded with?
5
**************Begin Loading***********
Load car1?
y
5 Train car #1 has been loaded with 0x4778645 5 boxes.
Load car2?




first int 5 is first num_boxes, 0x4778645 is the second num_boxes, and third int 5 is the third.

Why is this? And how can I go about fixing it?



Thanks for reading my post.
Bump
Uh.. One question, why are you using num_boxes instead of train_cars[i] ?
Because num_boxes is the variable that is used to store the information from the user input. The program asks for how many boxes of freight each car should store, and the user replies. In the second loop it asks if car(such number) should be stored with the boxes given before hand. If yes, then num_boxes (or the input that was given by the user) is displayed as saying that it was stored in the car
HOWEVER, when I replace the num_boxes with train_cars[i], I get the exact same results as my above first post
I run your code in cpp.sh but i change user input to direct assigment since im on my phone. And everything work fine
Im still having the exact same issue when I replace num_boxes with train_cars[i]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    cout << "How many boxes of freight should each car be loaded with?" << endl;
    cin >> num_boxes;
    for(int i = 0; i < CARS; i++)
    {
        train_cars[i] = num_boxes;

    }

    cout << "***************Begin Loading***************" << endl;
    for (int i = 0; i < CARS; i++)
        {
            cout << "Load car" << (i+1) << "?" << endl;
            cin >> answer;
            if(answer = 'y')
                cout << train_cars[i] << " ";
                cout << "Train car #" << (i+1) << " has been loaded with "<<
                cout << train_cars[i] << " ";
                cout << train_cars[i] << " boxes." << endl;
        }

Output is

How many boxes of freight should each car be loaded with?
5
**************Begin Loading***********
Load car1?
y
5 Train car #1 has been loaded with 0x4778645 5 boxes.
Load car2?
Last edited on
bump
I'm surprised that this will even compile. It looks like the problem is that you are trying to do cout << cout which makes no sense.

Line 16 cout << "Train car #" << (i+1) << " has been loaded with "<<
remove the << at the end of the line and replace it with a ;

cout << "Train car #" << (i+1) << " has been loaded with ";

Line 2 int train_cars[CARS], there should be a ; at the end of this line not a ,.

Line 14 if(answer = 'y') this is an assignment statement you want an equality statement.

if(answer == 'y')
All fixed. I get the same issue.
Anyone else have any possible input?
Topic archived. No new replies allowed.