Consider:
class vehicle{
private:
vector<string>carList;
public:
void info();
void displayCarInfo(vector<string>&carList);
}
void vehicle::info(){
char a;
string choice;
do{
cout<<"Please enter your car Reg number: ";
cin >> carPlate;
carList.push_back(carPlate);
cout << "Do you want to continue add car info(y/n)?";
cin >> choice;
}while(choice == "y");
}
void vehicle::displayCarInfo(vector<string>&carList){
for(int i = 0; i < 100; i++){
cout << carList[i];
}
}
//----------------------------------------------------------------
int main(){
vehicle a;
a.displayCarInfo(carList);
return 0;
}
Errors are shown when displaying all the car list. How can I solve it?
And how can I retrieve information for a particular element inside carList
?
You don't need to pass the vector as an argument to void displayCarInfo();
:
class Vehicle{ //<--- define classes starting with uppercase
private:
vector<string>carList;
public:
void info();
void displayCarInfo();
}; //<----- you need a semicolon here
The termination condition should be the vector size.
void vehicle::displayCarInfo() {
for (std::size_t i = 0; i < carList.size(); ++i) {
cout << carList[i];
}
}
In main()
:
int main(){
vehicle a;
a.displayCarInfo();
return 0;
}