C++ cin 和 getline 跳过输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10553597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cin and getline skipping input
提问by hakuna matata
earlier i posted a question about cin
skipping input, and I got results to flush, and use istringstream
, but now I tried every possible solution but none of them work.
早些时候我发布了一个关于cin
跳过输入的问题,我得到了要刷新和使用的结果istringstream
,但现在我尝试了所有可能的解决方案,但没有一个有效。
here is my code:
这是我的代码:
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;
}
but I'm still getting the same thing, skipping input, and when it does take input, it takes them and stores in name empty nothing, and in address it takes what i wrote in name but from the 2nd letter to the end
但我仍然得到同样的东西,跳过输入,当它接受输入时,它接受它们并以空的名义存储任何东西,在地址中它需要我用名字写的但从第二个字母到结尾
what is wrong with my code?
我的代码有什么问题?
I tried the cin.ignore()
, cin.get()
, and cin.clear()
all of them together and alone, none of them worked
我单独尝试了cin.ignore()
,cin.get()
和cin.clear()
所有这些,但没有一个起作用
EDIT:
编辑:
main method in main.cpp invokes mainMenu()
only
main.cpp 中的 main 方法mainMenu()
只调用
void mainMenu () {
char choice;
do {
system("cls");
mainMenuDisplay();
cin >> choice;
system("cls");
switch (choice) {
case '1':
customerMenu();
break;
case '2':
dvdMenu();
break;
case '3':
receiptMenu();
break;
case '4':
outro();
break;
default:
cout << '\a';
}
cin.ignore();
cin.get();
} while (choice != '4');
}
i will choose 1 for the customer example, this is customerMenu()
我将为客户示例选择 1,这是 customerMenu()
void customerMenu () {
char choice;
do {
system("cls");
manageCustomerMenu();
cin >> choice;
system("cls");
switch (choice) {
case '1':
createNewCustomer();
break;
case '2':
deleteCustomer();
break;
case '3':
updateCustomerStatus();
break;
case '4':
viewCustomersList();
break;
case '5':
mainMenu();
break;
default:
cout << '\a';
}
cin.ignore();
cin.get();
} while (choice != '5');
}
I choose 1 again to create a new customer object, which will now go to the MainFunctions.cpp which will invoke the function createNewCustomer()
which is the first one.
我再次选择 1 来创建一个新的客户对象,该对象现在将转到 MainFunctions.cpp,它将调用createNewCustomer()
第一个函数。
void createNewCustomer () {
string name, address;
cout << "Creating a new customer..." << endl;
cout << "Enter the customer's name: "; cin.getline(name,256);
cout << "Enter the customer's address: "; cin.getline(address,256);
Customer c(name, address, 0);
CustomerDB::addCustomer(c);
cout << endl;
}
回答by chris
If you're using getline
after cin >> something
, you need to flush the newline out of the buffer in between.
如果您使用getline
after cin >> something
,则需要将换行符从缓冲区中刷新出来。
My personal favourite for this if no characters past the newline are needed is cin.sync()
. However, it is implementation defined, so it might not work the same way as it does for me. For something solid, use cin.ignore()
. Or make use of std::ws
to remove leading whitespace if desirable:
如果不需要超过换行符的字符,我个人最喜欢的是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
回答by jrok
The structure of your menu code is the issue:
您的菜单代码的结构是问题:
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.
回答by user2135533
I faced this issue, and resolved this issue using getchar() to catch the ('\n') new char
我遇到了这个问题,并使用 getchar() 解决了这个问题来捕获 ('\n') 新字符
回答by theharshest
Here, the '\n'
left by cin, is creating issues.
在这里,'\n'
cin的左边,正在制造问题。
do {
system("cls");
manageCustomerMenu();
cin >> choice; #This cin is leaving a trailing \n
system("cls");
switch (choice) {
case '1':
createNewCustomer();
break;
This \n
is being consumed by next getline in createNewCustomer()
. You should use getline instead -
这\n
是由下一个 getline in 消耗的createNewCustomer()
。你应该使用 getline 代替 -
do {
system("cls");
manageCustomerMenu();
getline(cin, choice)
system("cls");
switch (choice) {
case '1':
createNewCustomer();
break;
I think this would resolve the issue.
我认为这将解决问题。