getline 用法(这个不明白)

来源:百度知道 编辑:UC知道 时间:2024/05/08 22:57:56
这是源代码:
#include <iostream>
#include<string>
using namespace std;
struct car{
string maker ;
int year;
};
int main()
{
int carn=0;
cout<<"How many car do you want to inqure:";
cin>>carn;
car*pp;
pp=new car[carn];
for(int i=1; i<=carn; i++){
cout<<"car #"<<i<<": \n"
<<"Please enter the maker: ";
getline(cin,pp[i-1].maker);
cout<<"Please enter the year: ";
cin>>pp[i-1].year;
}
cout<<"Here is your collection: ";
for(i=1; i<=carn; i++){
cout<<pp[i-1].year<<" "<<pp[i-1].maker<<endl;
}
delete[] pp;
return 0;

}
运行之后
例如:How many car do you want to inqure: 2(输入的2)
car #1:
Please enter the maker: nihao(输入的nihao)
Please enter the

#include <iostream>
#include<string>
using namespace std;
struct car{
string maker ;
int year;
};
int main()
{
int carn=0;
cout<<"How many car do you want to inqure:";
cin>>carn;
car*pp;
pp=new car[carn];
for(int i=1; i<=carn; i++){
cout<<"car #"<<i<<": \n"
<<"Please enter the maker: ";
cin>>pp[i-1].maker; //直接用cin输入。你下面都用cin,怎么这里有用什么getline?
cout<<"Please enter the year: ";
cin>>pp[i-1].year;
}
cout<<"Here is your collection: ";
for(i=1; i<=carn; i++){
cout<<pp[i-1].year<<" "<<pp[i-1].maker<<endl;
}
delete[] pp;
return 0;

}