之前我发布了一个有关 cin
跳过输入的问题,我得到的结果是使用 flush 和 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;
}
但我仍然得到同样的结果,跳过输入,当它接受输入时,将它们存储在空的name中,而在address中它会取出我在name中写的内容,但只取第二个字母到末尾。
我的代码有什么问题?
我尝试了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;
}