所以我環顧四周,但我顯然不是在正確的地方尋找,或者我誤解了某些東西。用C++中的if/else語句驗證輸入值
給出的指示是,程序只能接受2.0和5.0之間的穹頂高度值。我感覺它與我的條件有關,但我不知道如何糾正它。
當我在調試過程中輸入高度時,它跳到我的else語句,並沒有給我一個機會來重新輸入新的輸入。
//Retrieving name and first vault and date
cout << "Please enter the name of the pole vaulter.\n";
getline(cin, name);
cout << "Please enter the height of the first vault attempt.\n";
cin >> vault1;
if(5.0 >= vault1 >= 2.0) {
cout << "What date was this vault accomplished?\n";
getline(cin, date1);
} else {
cout << "Only jumps between 2.0 meters and 5.0 meters are valid inputs. Please enter the height of the first vault attempt.\n";
cin >> vault1;
cout << "What date was this vault accomplished?\n";
getline(cin, date1);
}
是的,檢查正在對所有三個金庫高度輸入執行。\ – 2013-02-09 02:12:07
我希望我能爲最後一句話+2。 – Johnsyweb 2013-02-09 02:12:26
@HeatherT你在緩衝區中有一個流浪'\ n'。 – Rapptz 2013-02-09 02:19:19