我对C ++比较陌生,但不是编程,我很困惑为什么我在C ++程序中遇到这个非常奇怪的问题。
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);
以下是控制台中的外观图片:。粗略圈出的部分是重复的重复。&#34;我觉得它应该是简单的东西,但我还不是很熟悉C ++,所以我很难过。如有必要,该程序的完整代码可用here。
答案 0 :(得分:4)
std::istream::operator>>
从流中读取字符,直到遇到空格(例如行尾字符),但它会在流中留下空白字符,因此下一次读取将会看到它。如果下一次读取是operator>>
,那就没问题,因为它会跳过前导空格,但是std::getline()
只会读取直到它看到分隔符(默认为'\n'
)。由于上一次调用operator>>
会将'\n'
作为流中的下一个字符,std::getline()
会看到它,并立即返回。要解决此问题,您可以在循环结束时致电std::cin.ignore()
以放弃在输入流中保留的'\n'
。