2015-12-16 150 views
1

我對C++比較陌生,但對編程不太熟悉,我很困惑,爲什麼我在C++程序中遇到這個非常奇怪的問題。While while循環的問題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); 

這裏是它的外觀在控制檯中的圖片:ghost iteration。粗略地圈出來的部分是「幽靈迭代」。我覺得它應該是簡單的,但我對C++還不是非常熟悉,所以我很難過。如有必要,該程序的完整代碼可用here

+0

您將「cin」問題描述爲「while循環」問題意味着您對一些奇怪的錯誤假設感到困惑。我沒有仔細研究你的帖子,看到**爲什麼**在前一次迭代的輸入緩衝區中留下了一些內容,以混淆下一次迭代。但你不需要知道爲什麼要看到這種情況。 – JSF

+0

當你喜歡時,你可以清除'cin',如果程序是交互式的,你可以在提示下一個輸入之前通過清除'cin'使它更加健壯。如果輸入可能提前輸入或重定向輸入,則需要在輸入下一行之前確保每行輸入已完全消耗。 – JSF

+0

或者,這可能更有可能,我前面提到的對C++編程的熟悉程度不夠,導致我對while循環問題的cin問題感到困惑,而實際上這不是假設的結果,無論是奇怪的還是不正確的。 – wjjd225

回答

4

std::istream::operator>>從流中讀取字符,直到它遇到空白(如行尾字符),但它在流中留下空白字符,因此下一次讀取將會看到它。如果下一次讀取是operator>>,那很好,因爲它會跳過前導空格,但std::getline()只是讀取,直到看到分隔符(默認爲'\n')。由於之前對operator>>的呼叫留下了'\n'作爲流中的下一個字符,因此std::getline()會看到它,並立即返回。爲了解決這個問題,您可以在循環結束時調用std::cin.ignore()來丟棄輸入流中剩餘的'\n'

+0

這個技巧。事後看來,這很有道理。 – wjjd225