#include <iostream>
#include <string>
using namespace std;
struct car
{
string maker;
int year;
};
int main()
{
int num,i;
cout << "How many cars do you wish to catalog? " ;
cin >> num;
while(cin.get() != '\n') // get rid of rest of line
;
car * ps = new car [num];
for (i=0;i<num;i++)
{
cout << "Car #" << i+1 << ": " << endl;
cout << "Please enter the make: ";
getline(cin,ps[i].maker);
cout << "Please enter the year made: ";
cin >> ps[i].year;
while(cin.get() != '\n') // get rid of rest of line
;
}
cout << "Here is your collection: \n";
for(i=0;i<num;i++)
{
cout << ps[i].year << " " << ps[i].maker << endl;
}
delete [] ps;
return 0;
}
如上,两个 while(cin.get() != '\n') 是干什么用的
#include <string>
using namespace std;
struct car
{
string maker;
int year;
};
int main()
{
int num,i;
cout << "How many cars do you wish to catalog? " ;
cin >> num;
while(cin.get() != '\n') // get rid of rest of line
;
car * ps = new car [num];
for (i=0;i<num;i++)
{
cout << "Car #" << i+1 << ": " << endl;
cout << "Please enter the make: ";
getline(cin,ps[i].maker);
cout << "Please enter the year made: ";
cin >> ps[i].year;
while(cin.get() != '\n') // get rid of rest of line
;
}
cout << "Here is your collection: \n";
for(i=0;i<num;i++)
{
cout << ps[i].year << " " << ps[i].maker << endl;
}
delete [] ps;
return 0;
}
如上,两个 while(cin.get() != '\n') 是干什么用的