求高手解答C++问题。 5
#include<iostream>#include<string>usingnamespacestd;structcar{stringcompany;intyear;}...
#include<iostream>
#include<string>
using namespace std;
struct car
{
string company;
int year;
};
int main()
{
int nCars;
cout<<"How many cars do you wish to catalog? ";
cin>>nCars;
car *p=new car[nCars];
int i=0;
while(i<nCars)
{
cout<<"Car #"<<i+1<<":"<<endl;
cin.get();//为什么要用此语句啊??
cout<<"Please enter the make: ";
getline(cin,p[i].company);
cout<<"Please enter the year made: ";
cin>>p[i].year;
i++;
}
cout<<"\nHere is your collection:\n";
for(i=0;i<nCars;i++)
cout<<p[i].year<<" "<<p[i].company<<endl;
delete [] p;
return 0;
}
输入2辆汽车,若不用cin.get(),则出现错误,这是为什么? 展开
#include<string>
using namespace std;
struct car
{
string company;
int year;
};
int main()
{
int nCars;
cout<<"How many cars do you wish to catalog? ";
cin>>nCars;
car *p=new car[nCars];
int i=0;
while(i<nCars)
{
cout<<"Car #"<<i+1<<":"<<endl;
cin.get();//为什么要用此语句啊??
cout<<"Please enter the make: ";
getline(cin,p[i].company);
cout<<"Please enter the year made: ";
cin>>p[i].year;
i++;
}
cout<<"\nHere is your collection:\n";
for(i=0;i<nCars;i++)
cout<<p[i].year<<" "<<p[i].company<<endl;
delete [] p;
return 0;
}
输入2辆汽车,若不用cin.get(),则出现错误,这是为什么? 展开
3个回答
展开全部
1.接收输入缓冲区内上次输入的结束符'\n',从而使下一次输出正常进行,否则以后的输入遇到'/n'就结束,什么也没有输入进去!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上一次的输入还留有一个回车符,cin.get()就是为了吞掉这个回车符的否则当你再用getline(cin,p[i].company)是读入的将是回车符。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询