使用getline而不是cin获取结构时的错误
bug when use getline insead of cin for struct
我的代码做得很好,但是当我将cin>>new_person.address
更改为getline
时,当我运行它时,我有bug。
#include <iostream>
using namespace std;
struct person
{
string name;
string address;
int phone_number;
};
person getNewPerson()
{
person new_person;
cout<<"Enter a name ";
cin>>new_person.name;
cout<<"Enter an address ";
getline(cin, new_person.address, 'n');
cout<<"Enter a phone number ";
cin>>new_person.phone_number;
return new_person;
}
person printPerson(person a_person)
{
cout<<"This person name is: "<<a_person.name<<endl;
cout<<"His address is: "<<a_person.address<<endl;
cout<<"His phone number is: "<<a_person.phone_number<<endl;
}
int main()
{
person my_person=getNewPerson();
printPerson(my_person);
}
当我使用getline(cin, new_person.address, 'n');
时,它会无缘无故跳过该语句并转到下一个语句。不知道发生了什么,请复制并粘贴代码,你会明白的。
就是这样设计的。在c++中,std::cin >> str;
读取流中的下一个单词。更具体地说,它读取下一个令牌,令牌由空格分隔,因此它们有效地表示一个单词。
使用getline
功能输入多个单词
相关文章:
- C++ 结构错误"调用'erase'没有匹配函数
- 尝试传递结构数组,但出现"无法将'结构'转换为'结构*'错误
- 结构错误:"no matching function for call to "
- UE4 数据表结构错误
- STL 对priority_queue<int、结构>错误
- C :用于体系结构错误的经典未定义符号
- 体系结构错误的未定义符号
- 奇怪的用户定义的比较结构错误,用于C++中的priority_queue
- 模板结构错误
- C++结构错误 - 错误 C2061:语法错误:标识符
- C++ 结构错误的前向声明
- 结构错误"uninitialized reference member"
- 使用 MPL::fold 与占位符和我自己的结构错误
- C++结构错误"No matching function for call..."
- 插入映射<字符串,结构>错误
- CPP 头文件结构错误
- 矢量和结构错误
- C++ 结构 - 错误 1 错误 C2143:语法错误:缺少'*'之前的';'
- 结构错误的构造函数(调用类的构造函数 denconstructor,用于填充结构)
- 结构错误与C++