【问题标题】:Code to get user input not executing/skipping in C++获取用户输入的代码不在 C++ 中执行/跳过
【发布时间】:2013-11-17 04:59:06
【问题描述】:

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

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

【问题讨论】:

  • ShowMenu 的代码在哪里?
  • 还有getLottoPicks
  • @enhzflep 我用代码编辑了我的问题。
  • @RetiredNinja 我添加了 getLottoPicks 的代码。

标签: c++ string input getline


【解决方案1】:

char showMenu() 中执行cin &gt;&gt; choice; 后,如果用户输入1[ENTER]char 会消耗 cin 的 1 个字符,并且换行符停留在流中。然后,当程序到达getline(cin, name); 时,它注意到cin 内部还有一些东西,并读取它。这是一个换行符,所以getline 得到它并返回。这就是程序的行为方式。

为了修复它 - 在您阅读输入后立即在 char showMenu() 中添加 cin.ignore();cin.ignore() 忽略下一个字符 - 在我们的例子中,换行符。

还有一点建议 - 尝试不要getlineoperator &gt;&gt; 混合使用。它们的工作方式略有不同,可能会给您带来麻烦!或者,至少记得在从std::cin 得到任何东西后总是ignore()。它可能会为您节省大量工作。

这修复了代码:

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】:

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

更新: 这是因为cin' '(空格)和getline 分隔,由'\n' 字符分隔,所以当输入名称并按回车cin in showmenu 将消耗整个字符串,除了@987654331 中的'\n' @ 由getline 读取。当它要求选择时看到这一点,输入1 myname(1 个空格我的名字)之类的字符串,然后按 ENTER 将显示名称。现在cin 将在choicemyname 的名称中读取1,getline

【讨论】:

  • 不,showMenu() 是正确的。它返回 char '1' 并转到程序的下一行并打印出“请输入您的姓名:”但之后它会直接跳到 getLottoPicks() 而不允许用户输入他们的姓名。
猜你喜欢
  • 2021-01-14
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多