首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >cin和getline跳过输入

cin和getline跳过输入
EN

Stack Overflow用户
提问于 2018-07-05 07:33:16
回答 2查看 0关注 0票数 0

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

这是我的代码:

代码语言:javascript
复制
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()只调用

代码语言:javascript
复制
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()

代码语言:javascript
复制
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()第一个函数。

代码语言:javascript
复制
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;
}
EN

回答 2

Stack Overflow用户

发布于 2018-07-05 15:38:49

如果你用getlinecin >> something,你需要在中间将换行符从缓冲区中清除。

我个人对此的最爱是,如果不需要超过换行符的字符cin.sync()。但是,它是定义的实现,所以它的工作方式可能与对我不同。对于实心的东西,请使用cin.ignore()或利用std::ws

如有需要,移除前导空格:

代码语言:txt
复制
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
票数 0
EN

Stack Overflow用户

发布于 2018-07-05 16:37:16

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

代码语言:txt
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档