CIN和getline跳过输入早些时候我发布了一个关于cin跳过输入,我得到了要刷新的结果,并使用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;}但是我仍然得到同样的东西,跳过输入,当它接受输入时,它接受它们并以名义存储空的任何东西,在地址上它需要我写的名字,但是从第二封信到结尾我的密码怎么了?我试过cin.ignore(), cin.get(),和cin.clear()他们都孤零零地在一起,没有人工作。编辑:main.cpp调用的主要方法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');}我将选择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');}
3 回答
汪汪一只猫
TA贡献1898条经验 获得超8个赞
getline
cin >> something
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 charactersstring s;getline (cin, s); //newline is gone, so this executes//other method: getline(cin >> ws, s); //remove all leading whitespace
慕田峪4524236
TA贡献1875条经验 获得超5个赞
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.
慕沐林林
TA贡献2016条经验 获得超9个赞
'\n'
do { system("cls"); manageCustomerMenu(); cin >> choice; #This cin is leaving a trailing \n system("cls"); switch (choice) { case '1': createNewCustomer(); break;
\n
createNewCustomer()
do { system("cls"); manageCustomerMenu(); getline(cin, choice) system("cls"); switch (choice) { case '1': createNewCustomer(); break;
- 3 回答
- 0 关注
- 677 浏览
添加回答
举报
0/150
提交
取消