Code to get user input not executing/skipping in C

2019-03-05 07:30发布

问题:

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

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

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

回答1:

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.

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.

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.

This fixes the code:

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


回答2:

from looking at code showMenu function has problem. and it's not returning asccii equivalent of '1' that is: 31 integer. try printing value returned by showmenu. you will get that

UPDATE: It is because cin in delimited by ' '(whitespace) and getline by '\n' character, so when enter name and press enter cin in showmenu will consume whole string except '\n' from istream and that is read by getline. to see this when it ask for choice enter string like 1 myname (1 whitespace myname)and press ENTER will display name. now cin will read 1 in choice and myname in name by getline.