c++ - cin 和 getline 跳过输入

标签 c++ input getline cin

之前我发布了一个关于 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 >> something 之后使用 getline,则需要将换行符从中间的缓冲区中清除。

如果不需要超过换行符的字符,我个人最喜欢的是 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

关于c++ - cin 和 getline 跳过输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553597/

相关文章:

C++11 统一初始化 : ambiguity between initializer list and multiple-parameter constructors?

javascript - HTML、javascript - 显示标签的输入值

python - 如果用户输入无效答案如何重做输入

C++:Getline 在第一个空格处停止读取

c - getdelim 报告的无效参数

c++ - C/C++ 用一个值填充结构数组

c++ - 超过三次函数根二分搜索时间限制

c++ - 派生类中虚拟方法的默认实现

C 接受关于传递关系的输入

c++ - 在 C++ 中读取具有多个定界符的文件