48

早些时候我发布了一个关于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()所有这些,它们都不起作用

编辑:

mainMenu()main.cpp 中的 main 方法仅调用

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;
}
4

4 回答 4

86

如果您使用getlineafter 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 characters

string s;
getline (cin, s); //newline is gone, so this executes

//other method: getline(cin >> ws, s); //remove all leading whitespace
于 2012-05-11T14:58:25.410 回答
16

您的菜单代码的结构是问题所在:

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.
于 2012-05-11T15:09:47.447 回答
2

我遇到了这个问题,并使用 getchar() 来捕获 ('\n') 新字符解决了这个问题

于 2014-12-11T09:35:24.587 回答
2

在这里,'\n'cin 的左边正在制造问题。

do {
    system("cls");
    manageCustomerMenu();
    cin >> choice;               #This cin is leaving a trailing \n
    system("cls");

    switch (choice) {
        case '1':
            createNewCustomer();
            break;

\n正在被下一个 getline in 消耗掉createNewCustomer()。您应该改用 getline -

do {
    system("cls");
    manageCustomerMenu();
    getline(cin, choice)               
    system("cls");

    switch (choice) {
        case '1':
            createNewCustomer();
            break;

我认为这将解决问题。

于 2012-05-11T15:04:38.750 回答