【问题标题】:Trouble with while loop C++ [duplicate]while循环C ++出现问题[重复]
【发布时间】:2016-03-22 00:18:54
【问题描述】:

我对 C++ 比较陌生,但对编程不熟悉,我很困惑为什么我的 C++ 程序中的 while 循环会出现这个非常奇怪的问题。

while (runUserInputLoop)
{
    displayMenu();
    cout << "Enter the number that corresponds with your choice from the menu: ";
    getline(cin, menuLoopChoice);

    if (menuLoopChoice == "1")
    {
        cout << "Enter mean: ";
        cin >> mean;
        cout << "Enter z-score: ";
        cin >> z;
        cout << "Enter standard deviation: ";
        cin >> stDev;
        cout << "Enter sample size: ";
        cin >> n;
        oneSampZInt(mean, z, stDev, n);
    }

    else if (menuLoopChoice == "2")
    {
        cout << "Enter mean: ";
        cin >> mean;
        cout << "Enter t-score: ";
        cin >> t;
        cout << "Enter standard deviation: ";
        cin >> stDev;
        cout << "Enter sample size: ";
        cin >> n;
        oneSampTInt(mean, t, stDev, n);
    }

    else if (menuLoopChoice == "3")
    {
        cout << "Enter mean for first sample: ";
        cin >> mean1;
        cout << "Enter mean for second sample: ";
        cin >> mean2;
        cout << "Enter standard deviation for first sample: ";
        cin >> stDev1;
        cout << "Enter standard deviation for second sample: ";
        cin >> stDev2;
        cout << "Enter z-score: ";
        cin >> z;
        cout << "Enter size of first sample: ";
        cin >> n1;
        cout << "Enter size of second sample: ";
        cin >> n2;
        indepMeansZInt(mean1, mean2, stDev2, stDev1, n1, n2, z);
    }

    else if (menuLoopChoice == "4")
    {
        cout << "Enter mean for first sample: ";
        cin >> mean1;
        cout << "Enter mean for second sample: ";
        cin >> mean2;
        cout << "Enter standard deviation for first sample: ";
        cin >> stDev1;
        cout << "Enter standard deviation for second sample: ";
        cin >> stDev2;
        cout << "Enter t-score: ";
        cin >> t;
        cout << "Enter size of first sample: ";
        cin >> n1;
        cout << "Enter size of second sample: ";
        cin >> n2;
        indepMeansTInt(mean1, mean2, stDev2, stDev1, n1, n2, t);
    }

    else if (menuLoopChoice == "5")
    {
        cout << "Enter sample proportion: ";
        cin >> p;
        cout << "Enter sample size: ";
        cin >> n;
        cout << "Enter z-score";
        cin >> z;
        onePropZInt(p, n, z);
    }

    else if (menuLoopChoice == "6")
    {
        cout << "Enter proportion from sample one: ";
        cin >> p1;
        cout << "Enter proportion from sample two: ";
        cin >> p2;
        cout << "Enter size of sample one: ";
        cin >> n1;
        cout << "Enter size of sample two: ";
        cin >> n2;
        cout << "Enter z-score";
        cin >> z;
        twoPropZInt(p1, p2, n1, n2, z);
    }

    else if (menuLoopChoice == "7")
    {
        cout << "Enter chi-sqaured right value: ";
        cin >> chiSqrdRight;
        cout << "Enter chi-sqaured left value: ";
        cin >> chiSqrdLeft;
        cout << "Enter sample variance: ";
        cin >> variance;
        cout << "Enter sample size: ";
        cin >> n;
        chiSqrdInt(chiSqrdRight, chiSqrdLeft, variance, n);
    }

    else if (menuLoopChoice == "8")
    {
        cout << "Enter mean of differences: ";
        cin >> DBar;
        cout << "Enter standard deviation of differences: ";
        cin >> SD;
        cout << "Enter number of matched pairs: ";
        cin >> n;
        cout << "Enter t-score: ";
        cin >> t;
        matchedPairsTInt(DBar, SD, n, t);
    }

    else if (menuLoopChoice == "A" || menuLoopChoice == "a")
    {
        cout << "Enter population mean: ";
        cin >> mean1;
        cout << "Enter sample mean: ";
        cin >> mean2;
        cout << "Enter population standard deviation: ";
        cin >> stDev;
        cout << "Enter size of sample: ";
        cin >> n;
        cout << "Enter z-score: ";
        cin >> z;
        oneSampZTest(mean1, mean2, stDev, n, z);
    }

    else if (menuLoopChoice == "B" || menuLoopChoice == "b")
    {
        cout << "Enter mean of sample one: ";
        cin >> mean1;
        cout << "Enter mean of sample two: ";
        cin >> mean2;
        cout << "Enter standard deviation of population one: ";
        cin >> stDev1;
        cout << "Enter standard deviation of population two: ";
        cin >> stDev2;
        cout << "Enter size of sample one: ";
        cin >> n1;
        cout << "Enter size of sample two: ";
        cin >> n2;
        cout << "Enter z-score: ";
        cin >> z;
        twoSampZTest(mean1, mean2, stDev1, stDev2, n1, n2, z);
    }

    else if (menuLoopChoice == "C" || menuLoopChoice == "c")
    {
        cout << "Enter population mean: ";
        cin >> mean1;
        cout << "Enter sample mean: ";
        cin >> mean2;
        cout << "Enter sample standard deviation: ";
        cin >> stDev;
        cout << "Enter size of sample: ";
        cin >> n;
        cout << "Enter t-score: ";
        cin >> t;
        oneSamptTest(mean1, mean2, stDev, n, z);
    }

    else if (menuLoopChoice == "D" || menuLoopChoice == "d")
    {
        cout << "Enter mean of sample one: ";
        cin >> mean1;
        cout << "Enter mean of sample two: ";
        cin >> mean2;
        cout << "Enter standard deviation of sample one: ";
        cin >> stDev1;
        cout << "Enter standard deviation of sample two: ";
        cin >> stDev2;
        cout << "Enter size of sample one: ";
        cin >> n1;
        cout << "Enter size of sample two: ";
        cin >> n2;
        cout << "Enter t-score: ";
        cin >> t;
        twoSamptTest(mean1, mean2, stDev1, stDev2, n1, n2, t);
    }

    else if (menuLoopChoice == "E" || menuLoopChoice == "e")
    {
        cout << "Enter the population proportion: ";
        cin >> p1;
        cout << "Enter the sample proportion: ";
        cin >> p2;
        cout << "Enter the sample size: ";
        cin >> n;
        cout << "Enter the z-score: ";
        cin >> z;
        onePropZTest(p1, p2, n, z);
    }

    else if (menuLoopChoice == "F" || menuLoopChoice == "f")
    {
        cout << "Enter sample proportion one: ";
        cin >> p1;
        cout << "Enter sample proportion two: ";
        cin >> p2;
        cout << "Enter the x value of proportion one: ";
        cin >> x1;
        cout << "Enter the x value of proportion two: ";
        cin >> x2;
        cout << "Enter the size of sample one: ";
        cin >> n1;
        cout << "Enter the size of sample two: ";
        cin >> n2;
        cout << "Enter the z-score: ";
        cin >> z;
        twoPropZTest(p1, p2, x1, x2, n1, n2, z);
    }

    else if (menuLoopChoice == "q" || menuLoopChoice == "Q")
    {
        runUserInputLoop = false;
    }
}

在循环的第一次迭代中,一切正常。但是,在所有后续迭代中,循环似乎迭代一次,没有任何输入,然后再次,允许我再次输入输入。所以本质上,有一个额外的迭代导致它执行这部分的 while 循环两次:

displayMenu();
cout << "Enter the number that corresponds with your choice from the menu: ";
getline(cin, menuLoopChoice);

这是它在控制台中的外观图片:。粗略圈出的部分是“幽灵迭代”。我觉得它应该很简单,但我对 C++ 还不是很熟悉,所以我很难过。如有需要,可在here 获取程序的完整代码。

【问题讨论】:

  • 您将cin 问题描述为while loop 问题意味着您对一些奇怪的错误假设感到困惑。我没有仔细研究您的帖子以了解 为什么 早期迭代的输入缓冲区中遗留了一些东西以混淆下一次迭代。但你不需要知道为什么会发生这种情况。
  • 您可以根据需要清除cin,如果程序是交互式的,您可以在提示输入下一个输入之前清除cin,使其更加健壮。如果输入可能是提前输入或重定向输入,那么您需要确保在期待下一行之前,每一行输入都被完全使用。
  • 或者,这很可能是我前面提到的对 C++ 编程缺乏熟悉,导致我将 cin 问题与 while 循环问题混淆了,实际上这不是假设的结果本质上是奇怪的或不正确的。
  • 题外话:省点麻烦,menuLoopChoice = tolower(menuLoopChoice); 所以你只需要测试小写字母。
  • 你不知道的东西永远不会像你认为你知道的东西那样有害。我的印象是,你认为你知道的东西是在错误的地方寻找问题的原因。这似乎在cin 或更糟糕的scanf 中询问此类问题的帖子中几乎普遍存在。

标签: c++


【解决方案1】:

std::istream::operator&gt;&gt; 从流中读取字符,直到遇到空格(例如行尾字符),但它会将空格字符留在流中,因此下一次读取会看到它。如果下一次读取是operator&gt;&gt;,那很好,因为它会跳过前导空格,但std::getline() 只会读取直到它看到分隔符(默认为'\n')。由于之前对operator&gt;&gt; 的调用将'\n' 作为流中的下一个字符,std::getline() 看到它并立即返回。为了解决这个问题,您可以在循环结束时调用 std::cin.ignore() 以丢弃留在输入流中的 '\n'

【讨论】:

  • 成功了。事后看来,这很有意义。
猜你喜欢
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 2014-11-08
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多