-1
我檢查了一些「可能已經有你的答案的問題」,但仍然沒有找到任何工作。我的問題是一個簡單的while循環,當它被計算爲true時,它循環並要求用戶輸入,我輸入一個數字,該數字應該將while循環評估爲false並退出,但它會重複。我第二次輸入數字。我對C++相當陌生,但我認爲有些東西在我的iostream中變得越來越大。while loop evalutaing to true when it false C++
1)I中的「0」輸入,它計算while循環爲true,並再次爲用戶輸入詢問,因爲它是< 1.
2)當再次提示我在1進入,這顯然是不< 1,循環再次重複告訴我「無效的數字 - 數字必須爲bwtween」
3)如果我再次輸入1 ... while循環的計算結果爲false並繼續前進,因爲它應該在步驟2.
代碼:
string question = "Enter number of days rented (1-365): ";
int getValidNumber(int num, int lowerNum, int upperNum, string question){
while((num < 1) || (num > 365)) {
cout << endl;
cout << "Invalid number -- the number must be bwtween " <<
lowerNum << " and " << upperNum << "." << endl <<
"Please try again." << endl;
cout << question;
cin >> num;
}
return num;
}
您是否嘗試過打印'num',或者通過此代碼進行步進,並在輸入後觀察'num'的值? – kevintodisco
這種情況並不明顯,你如何閱讀你的初始號碼在這裏非常重要。把這個數字放在錯誤信息中,這樣你就可以看到它是什麼以及它爲什麼會一直失敗。你可能會感到驚訝。 – WhozCraig
經過我的問題....我發現了發生了什麼事。我正在參考函數twcie .... idiot。大聲笑 – Ducksauce88