用于在C ++中执行/跳过用户输入的代码

时间:2013-11-17 04:59:06

标签: c++ string input getline

在下面的代码中,当我尝试让用户输入他们的名字时,我遇到了一个错误。我的程序只是跳过它然后直接进行函数调用而不允许用户输入他们的名字。尽管有错误,我的程序正在编译。我根据我在这里找到的其他例子编写了这部分,我不确定会出现什么问题。有什么建议?

#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的代码:

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(这部分非常错误,我还在努力):

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

2 个答案:

答案 0 :(得分:2)

cin >> choice;char showMenu()后,如果用户输入1[ENTER],则char会消耗cin中的1个字符,并且新行会保留在溪流里面。然后,当程序到达getline(cin, name);时,它会注意到cin内部仍然存在某些内容并将其读取。它是换行符,因此getline获取并返回。这就是为什么程序的行为方式。

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

并提出建议 - 尝试getlineoperator >>混合。它们的工作方式略有不同,可能会给您带来麻烦!或者,至少记得从ignore()获得任何内容后始终std::cin。它可以为你节省很多工作。

这修复了代码:

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

答案 1 :(得分:0)

从查看代码showMenu函数有问题。并且它不会返回等于'1'的asccii,即:31整数。尝试打印showmenu返回的值。你会得到那个

<强>更新   这是因为cin' '(空白)和getline'\n'字符分隔,因此输入名称并按cin时输入showmenu将从'\n'使用除istream之外的整个字符串,并由getline读取。当它要求选择时看到这个输入像1 myname(1空格我的名字)的字符串,然后按ENTER将显示名称。现在cin将在choicemyname中以getline的名义读取1。