C++ cin和getline跳过输入
早些时候,我发布了一个关于跳过输入的问题,我得到了刷新的结果,并使用了C++ cin和getline跳过输入,c++,input,getline,cin,C++,Input,Getline,Cin,早些时候,我发布了一个关于跳过输入的问题,我得到了刷新的结果,并使用了istringstream,但现在我尝试了所有可能的解决方案,但都不起作用 这是我的密码: void createNewCustomer () { string name, address; cout << "Creating a new customer..." << endl; cout << "Enter the customer's name: "; get
istringstream
,但现在我尝试了所有可能的解决方案,但都不起作用
这是我的密码:
void createNewCustomer () {
string name, address;
cout << "Creating a new customer..." << endl;
cout << "Enter the customer's name: "; getline(cin, name);
cout << "Enter the customer's address: "; getline(cin, address);
Customer c(name, address, 0);
CustomerDB::addCustomer(c);
cout << endl;
}
我将为客户示例选择1,这是customerMenu()
如果在
cin>>之后使用getline
,则需要从中间的缓冲区中清除换行符
如果不需要换行符,我个人最喜欢的是cin.sync()
。但是,它是由实现定义的,所以它可能不会像我一样工作。对于实体,请使用cin.ignore()
。如果需要,也可以使用std::ws
删除前导空格:
int a;
cin >> a;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
//discard characters until newline is found
//my method: cin.sync(); //discard unread characters
string s;
getline (cin, s); //newline is gone, so this executes
//other method: getline(cin >> ws, s); //remove all leading whitespace
inta;
cin>>a;
cin.ignore(std::numeric_limits::max(),'\n');
//丢弃字符直到找到换行符
//我的方法:cin.sync()//丢弃未读字符
字符串s;
getline(cin,s),//换行符消失了,所以执行这个
//其他方法:getline(cin>>ws,s)//删除所有前导空格
在这里,cin留下的'\n'
正在制造问题
do {
system("cls");
manageCustomerMenu();
cin >> choice; #This cin is leaving a trailing \n
system("cls");
switch (choice) {
case '1':
createNewCustomer();
break;
此\n
正在被createNewCustomer()
中的下一个getline使用。您应该改用getline-
do {
system("cls");
manageCustomerMenu();
getline(cin, choice)
system("cls");
switch (choice) {
case '1':
createNewCustomer();
break;
我认为这将解决问题。菜单代码的结构就是问题所在:
cin >> choice; // new line character is left in the stream
switch ( ... ) {
// We enter the handlers, '\n' still in the stream
}
cin.ignore(); // Put this right after cin >> choice, before you go on
// getting input with getline.
我遇到了这个问题,并使用getchar()来捕获('\n')新字符解决了这个问题。请显示一个完整的可编译示例。如果这很难,请至少显示调用此函数的函数。好的,我将编辑此问题,以包括某种程度上类似于堆栈跟踪的内容和您所说的尝试cin.ignore的示例的屏幕截图。给出代码时,它应该已经起作用了。sync()
没有定义这样做,尽管它在某些情况下可能会起作用。它对我不起作用。@aizen92,任何时候你cin>>做某事时,它都会在缓冲区中留下一个换行符。切换功能时,只要仍在使用cin
,缓冲区就会保留。如果对cin
执行的下一个操作是getline
,它将读取剩余的换行符并似乎跳过获取输入。开始吧。n3242,上一个C++11草案,§27.9.1.5/19--“int sync()效果:如果存在put区域,则调用filebuf::overflow将字符写入文件。如果存在get区域,则效果由实现定义。”如果用户在一行中键入80个以上的字符,该怎么办?首选:std::cin.ignore(std::numeric_limits::max(),'\n')代码>谢谢,已经为此挣扎了很长一段时间。要使用getline
您需要将choice
更改为string,然后它不能像开关中那样使用:您可以使用choice[0]
,但这样会隐藏错误(例如,“11”将被视为1,但对于忽略行的其余部分的已接受错误也是如此。这对我很有效。
do {
system("cls");
manageCustomerMenu();
cin >> choice; #This cin is leaving a trailing \n
system("cls");
switch (choice) {
case '1':
createNewCustomer();
break;
do {
system("cls");
manageCustomerMenu();
getline(cin, choice)
system("cls");
switch (choice) {
case '1':
createNewCustomer();
break;
cin >> choice; // new line character is left in the stream
switch ( ... ) {
// We enter the handlers, '\n' still in the stream
}
cin.ignore(); // Put this right after cin >> choice, before you go on
// getting input with getline.