struct car
{
string name;
int year;
};
int main() {
int noOfCars;
cout<<"enter no_ of cars : ";
cin>>noOfCars;
car* cars = new car[noOfCars];
for(int i=0;i<noOfCars;i++)
{
cout<<"Car #"<<i<<endl;
cout<<"Name : ";
getline(cin,(cars[i].name)); //here is the problem
cout<<"\n year : ";
cin>>cars[i].year;
cout<<endl;
}
}
something wron with taking a whole line as a string input to the name in the strcut,doesn’t even take any thing and proceeds to ge the year directly … :S ???
it works with cin,but i want to take a whole line !
and it works with strings globally defined,but not with this inside the struct
Insert
cin.ignore( 1000, '\n' );aftergetline.