gpt4 book ai didi

c++ - while 循环 C++ 的问题

转载 作者:行者123 更新时间:2023-11-30 01:45:39 25 4
gpt4 key购买 nike

<分区>

我是 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);

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com