【发布时间】:2013-05-18 17:40:54
【问题描述】:
在我创建的程序中,我需要将一些客户信息保存到数组中。以下是关于我的问题的代码。
struct CustomerType
{
string fName;
string lName;
char gender;
string address;
string contactNo;
};
CustomerType Customer[1000];
我有以下代码来获取用户的输入。这里i 是我正在获取信息的客户的索引。
string add="";
cout<<left<<"\n"<<setw(29)<<"\t\t Name"<<": ";
cin>>Customer[i].fName>>Customer[i].lName;
cout<<left<<"\n"<<setw(29)<<"\t\t Gender"<<": ";
cin>>Customer[i].gender;
cout<<left<<"\n"<<setw(29)<<"\t\t Address"<<": ";
getline(cin,add); Customer[i].address=add;
cout<<left<<"\n"<<setw(29)<<"\t\t Contact No."<<": ";
cin>>Customer[i].contactNo;
但是当我运行程序时,它只要求输入姓名、性别和联系电话。但不是地址。它就像没有getline 命令一样工作。
我该如何解决这个问题?
【问题讨论】:
-
如果您在运算符周围使用空格,您的代码会更容易阅读。顺便说一句,不使用空格的编译过程中节省的时间可以忽略不计。
-
我建议您在结构中添加功能以读取其成员。你的代码看起来像
cin >> Customer[i];。
标签: c++ visual-studio-2010 getline cin