在C ++中获取用户输入未执行/跳过的代码 [英] Code to get user input not executing/skipping in C++

查看:57
本文介绍了在C ++中获取用户输入未执行/跳过的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,当我试图让用户输入他们的姓名时,我遇到了错误.我的程序只是跳过它,然后直接进行函数调用,而无需用户输入名称.尽管有错误,我的程序仍在编译.我不确定是怎么回事,因为我根据在此找到的其他示例编写了该部分.有什么建议?

In the below code, I'm running into an error when I try to get the user to input their name. My program just skips it over and goes right over to making the function calls without allowing the user to enter their name. Despite the error, my program is compiling. I'm not sure what's going wrong as I wrote that part based off other examples I found on here. Any suggestions?

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

char showMenu();
void getLottoPicks(int[]);
void genWinNums(int[]);
bool noDuplicates(int[]);

const int SIZE = 7;

int main()
{
    int userTicket[SIZE] = {0};
    int winningNums[SIZE] = {0};
    char choice;
    string name;

    srand(time(NULL));

    do
    {
        choice = showMenu();

        if (choice == '1')
        {
            cout << "Please enter your name: " << endl;
            getline(cin, name);

            getLottoPicks(userTicket);
            genWinNums(winningNums);

            for (int i = 0; i < SIZE; i++)
                cout << winningNums[i];
        }
    } while (choice != 'Q' && choice != 'q');

    system("PAUSE");
    return 0;
}

为showMenu添加了代码:

Added the code for showMenu:

char showMenu()
{
    char choice;

    cout << "LITTLETON CITY LOTTO MODEL:" << endl;
    cout << "---------------------------" << endl;
    cout << "1) Play Lotto" << endl;
    cout << "Q) Quit Program" << endl;
    cout << "Please make a selection: " << endl;
    cin >> choice;

    return choice;
} 

和getLottoPicks(这部分非常错误,我仍在研究中):

And getLottoPicks (this part is very wrong and I'm still working on it):

void getLottoPicks(int numbers[])
{
    cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;

    for (int i = 0; i < SIZE; i++)
    {
        cout << "Selection #" << i + 1 << endl;
        cin >> numbers[i];
        if (numbers[i] < 1 || numbers[i] > 40)
        {
            cout << "Please choose a number between 1 and 40: " << endl;
            cin >> numbers[i];
        }
        if (noDuplicates(numbers) == false)
            {
                do
                {
                cout << "You already picked this number. Please enter a different number: " << endl;
                cin >> numbers[i];
                noDuplicates(numbers);
                } while (noDuplicates(numbers) == false);
            }
    }
}

推荐答案

char showMenu()中执行cin >> choice;之后,如果用户输入1[ENTER],则char会消耗cin中的1个字符,换行符停留.然后,当程序进入getline(cin, name);时,它会发现cin内还有东西,并进行读取.这是换行符,因此getline可以获取并返回.这就是程序表现方式的原因.

After doing cin >> choice; inside char showMenu(), if a user inputs 1[ENTER], the char consumes 1 character from cin, and the newline stays inside the stream. Then, when the program gets to getline(cin, name);, it notices that there's still something inside cin, and reads it. It's a newline character, so getline gets it and returns. That's why the program is behaving the way it is.

为解决此问题,请在读取输入后立即在char showMenu()内添加cin.ignore();. cin.ignore()忽略下一个字符-在我们的例子中为换行符.

In order to fix it - add cin.ignore(); inside char showMenu(), right after you read the input. cin.ignore() ignores the next character - in our case, the newline char.

还有个建议-尝试getlineoperator >>混合使用.它们的工作方式略有不同,可能会惹上您的麻烦!或者,至少要记住从std::cin中获取任何内容后,始终要按ignore().它可以为您节省很多工作.

And a word of advice - try not to mix getline with operator >>. They work in a slightly different way, and can get you into trouble! Or, at least remember to always ignore() after you get anything from std::cin. It may save you a lot of work.

这可以修复代码:

char showMenu()
{
    char choice;

    cout << "LITTLETON CITY LOTTO MODEL:" << endl;
    cout << "---------------------------" << endl;
    cout << "1) Play Lotto" << endl;
    cout << "Q) Quit Program" << endl;
    cout << "Please make a selection: " << endl;
    cin >> choice;
    cin.ignore();

    return choice;
} 

这篇关于在C ++中获取用户输入未执行/跳过的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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