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.