【发布时间】:2016-02-05 22:57:58
【问题描述】:
之前我发布了一个关于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 中的 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');
}
我会为客户示例选择 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');
}
我再次选择 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;
}
【问题讨论】:
-
请展示一个完整的可编译示例。如果这很困难,请至少显示调用此函数的函数。
-
好的,我将编辑问题以包含类似于堆栈跟踪的内容和示例的屏幕截图
-
您说您尝试过 cin.ignore。给出代码,它应该可以工作。