我剛開始學習C++,之前用Java編碼。下面的代碼從用戶獲取輸入並驗證輸入。第一件要求選民的數量,這必須是一個正數。如果我輸入一個負數,該程序將按照我的預期行事。它打印出錯誤消息並再次請求輸入。但是,如果我輸入任何其他字符,例如任何字母,我會在控制檯中出現無限循環,要求輸入並打印錯誤消息。我究竟做錯了什麼?C++神祕的無限循環
我的代碼:
#include <iostream>
using namespace std;
struct dataT {
int numOfVoters = -1;
float preSpread = -1;
float votingError = -1;
};
void getUserInfo() {
dataT data;
while (data.numOfVoters == -1) {
cout << "enter the number of voters" << endl;
cin >> data.numOfVoters;
if (data.numOfVoters <= 0) {
data.numOfVoters = -1;
cout << "Invalid entry, please enter a number larger than zero." << endl;
}
}
while (data.votingError == -1) {
cout << "enter the percentage spread between candidates" << endl;
cin >> data.votingError;
if (data.votingError <= 0 || data.votingError >= 1) {
data.votingError = -1;
cout << "Invalid entry. Enter a number between 0 to 1." << endl;
}
}
while (data.preSpread == -1) {
cout << "Enter the precentage spread between the two candidates." << endl;
cin >> data.preSpread;
if (data.preSpread <= 0 || data.preSpread >= 1) {
data.preSpread = -1;
cout << "Invalid input. Enter a number between 0 and 1." << endl;
}
}
}
int main() {
getUserInfo();
return 0;
}
控制檯:
enter the number of voters
f
Invalid entry, please enter a number larger than zero.
enter the number of voters
Invalid entry, please enter a number larger than zero.
enter the number of voters
Invalid entry, please enter a number larger than zero.
...
...
...
你輸入失敗。你從來沒有檢查輸入失敗,只是有一個無效的值,你認爲在輸入失敗後有意義。 – chris 2014-10-10 16:27:42
相關:http://stackoverflow.com/questions/24504582/test-whether-stringstream-operator-has-parsed-a-bad-type?noredirect=1#comment37965807_24504582 – 2014-10-10 16:29:47
在第一個「WHILE」循環中,你只檢查對於負數,只要它不是負數,你的代碼將接受它,確保做一個轉換int – 2014-10-10 16:30:45