inputs and loop

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include "Vehicle.h"

using namespace std;
int main()
{
    Vehicle myVehicle;
    const int j = 8;
    string theVehicle;
    for (int i = 0; i < j; i++){
    cout << "\nPlease enter the vehicle type" << i+1 << ": ";
    getline(cin, theVehicle);
    myVehicle[i].setType(theVehicle);
 }
    int i = 0;
    while( i < j)
    {
        cout << "Your vehicle type is " << myVehicle.getType() << endl;
        i++;
    }
}
Vehicle myVehicle;
this has to be an array to hold the incoming Vehicle objects
okay so I changed it but now my while loop isn't working.. any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include "Vehicle.h"

using namespace std;
int main()
{
    Vehicle myVehicle[8];
    const int j = 8;
    string theVehicle;
    for (int i = 0; i < j; i++){
    cout << "\nPlease enter the vehicle type" << i+1 << ": ";
    getline(cin, theVehicle);
    myVehicle[i].setType(theVehicle);
    }
    int i = 0;
    while( i < j)
    {
        cout << "Your vehicle type is " << myVehicle[i].getType << endl;
        i++;
    }
}
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.

1
2
Vehicle myVehicle[8];
const int j = 8;

Give your variables better names.
1
2
3
// better
const int num_vehicles = 8;
Vehicle myVehicle[num_vehicles];


Keep your code consistent. Why do you use a for loop to iterate over your array and then a while loop to do the same thing?
1
2
3
4
// better
for (int i = 0; i < num_vechicles; i++){  
    cout << "Your vehicle type is " << myVehicle[i].getType() << '\n';
}

I'm assuming getType is a function, so you're missing the function call parentheses.
Last edited on
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.
Topic archived. No new replies allowed.