assume we have this struct and we need to take data from user , the type of the car and the car number, how can we read line nr 2 below, and how to read the code , I mean what does this code mean vector <string> type
1 2 3 4 5 6 7 8 9 10 11 12 13
struct Car {
vector <string> type;
int carNr;
};
// read data funtion
// I done this so far, got errors
void readData (Car & car) {
cout << "Enter the car type please", getline (cin, car.type);
}
Enter the car's type: This is a TEST
Car type: This is a TEST
Car #: 1
Having the car type be a vector of strings may be the requirement, but without knowing EXACTLY what your assignment is, ALL of it, using a vector to store a bunch of types makes ZERO sense. Especially since a car's number is a single entry.
#include <iostream>
#include <string>
#include <vector>
struct Car
{
std::string type;
int carNr;
};
void readData (Car& car)
{
std::cout << "Enter the car type please: ";
std::getline (std::cin, car.type);
std::cout << "Enter the car number please: ";
std::cin >> car.carNr;
std::cin.ignore(1000, '\n');
}
int main()
{
std::vector<Car> car_store;
Car temp;
// INPUT & STORE
for(int i = 0; i < 3; i++)
{
readData(temp);
car_store.push_back(temp);
}
// OUTPUT STORE CONTENTS
for(int i = 0; i < car_store.size(); i++)
{
std::cout << car_store[i].type << ' ' << car_store[i].carNr << '\n';
}
return 0;
}
Enter the car type please: Ford Coupe
Enter the car number please: 1234
Enter the car type please: Buick
Enter the car number please: 99878
Enter the car type please: Chevrolet Impale
Enter the car number please: 888
Ford Coupe 1234
Buick 99878
Chevrolet Impale 888
Program ended with exit code: 0