I am trying to write a program that asks the user to input 8 types of vehicles then output the 8 vehicles. But I need to do it in a loop. Can anyone tell me why my code is not working and provide an example on how to do it?
okay so I changed it but now my while loop isn't working.. any ideas?
What do you mean by "isn't working"? Unexpected output? Can't compile? Please be more precise and avoid the rather vague "it's not working" when asking for help.
okay, thank you! I am tackling this project one step at a time..
Now I have to add another question to the vehicle type one which is the number of doors.
However, when i put them both in a loop, the computer is only asking the user for the answer to the second question..
1 2 3 4 5 6 7 8 9
string theVehicle;
int numDoors;
for (int i = 0; i < j; i++){
cout << "\n\nPlease enter the vehicle type:" << i+1 << " ";
getline(cin, theVehicle);
myVehicle[i].setType(theVehicle);
cout << "\n\nEnter the amount of doors the vehicle have" << i+1 << ": ";
cin >> numDoors;
myVehicle[i].setNumber(numDoors);
Do I need an if statement after asking for the type of vehicle?
You're mixing unformatted input (getline(cin, theVehicle);) with formatted input (cin >> numDoors;). cin will leave a newline character ('\n') after input when you press enter, but getline reads input up until a newline character. This will result in getline "skipping" input.
All you have to do is call cin.ignore() after inputting with cin. What this will do is remove the newline character at the end of your input, which will allow getline to work as expected.