I was trying to input 'make' of the car, which can be a multi-word string (eg. Hudson Hornet). But my code is just skipping the getline(cin, arr[i].make)
, i.e. it is not allowing me to enter the make value. But when I used 2 getline(cin, arr[i].make)
one after the other, then it is letting me input the value of make.
I am not able to understand what is the reason for this, as I have studied that getline() is appropriate to input multi-word strings?
#include <iostream>
#include <string>
using namespace std;
struct car
{
string make;
int year;
};
int main(){
int n;
cout<<"How many cars do you wish to catalog? ";
cin>>n;
car *arr = new car[n];
for(int i=0; i<n; i++){
cout<<"Car#"<<i+1<<endl;
cout<<"Please enter the make: ";
getline(cin, arr[i].make);
cout<<"Please enter the year made: ";
cin>>arr[i].year;
}
cout<<"Here is your collection:\n";
for(int i=0; i<n; i++){
cout<<arr[i].year<<" "<<arr[i].make<<"\n";
}
return 0;
}