cin和getline跳过输入 [英] cin and getline skipping input

查看:124
本文介绍了cin和getline跳过输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早些时候我发布了一个关于 cin 跳过输入的问题,我得到结果刷新,并使用 istringstream 但现在我尝试了所有可能的解决方案,但没有一个可以工作。

earlier i posted a question about cin skipping input, and I got results to flush, and use istringstream, but now I tried every possible solution but none of them work.

这里是我的代码:

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

但我仍然得到同样的东西,跳过输入,没有接受输入,它需要他们和商店在名称空没有,在地址它需要我写在名称,但从第二个字母到结束

but I'm still getting the same thing, skipping input, and when it does take input, it takes them and stores in name empty nothing, and in address it takes what i wrote in name but from the 2nd letter to the end

什么是错误

我尝试了 cin.ignore() cin.get () cin.clear()所有这些都在一起,单独,都没有工作

I tried the cin.ignore(), cin.get(), and cin.clear() all of them together and alone, none of them worked

EDIT:

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

main method in main.cpp invokes mainMenu() only

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()

i will choose 1 for the customer example, this is 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()这是第一个函数。

I choose 1 again to create a new customer object, which will now go to the MainFunctions.cpp which will invoke the function createNewCustomer() which is the first one.

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


推荐答案

code> getline 之后 cin>>

If you're using getline after cin >> something, you need to flush the newline out of the buffer in between.

如果没有超过换行符的字符,我的个人最喜欢的是 cin.sync()。但是,它是实现定义的,所以它可能不会像我们一样工作。对于固体,使用 cin.ignore()。或者如果需要,可以使用 std :: ws 删除前导空格:

My personal favourite for this if no characters past the newline are needed is cin.sync(). However, it is implementation defined, so it might not work the same way as it does for me. For something solid, use cin.ignore(). Or make use of std::ws to remove leading whitespace if desirable:

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

这篇关于cin和getline跳过输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆