我是介紹C++計算機科學課程的學生,這是我第一次在這裏發佈。我們剛剛瞭解while循環,儘管賦值不需要它,但我正在嘗試對此賦值進行輸入驗證。該計劃旨在讀取數字列表並找出列表中第一個和最後8個位置的位置。因此,如果我有四個數字(1,8,42,8)的列表,則第一個和最後8個位置是2和4.該組的大小由用戶決定。C++輸入驗證while循環不終止
我試圖做一個while循環,測試以確保用戶輸入的內容實際上是一個數字,但是當我嘗試輸入類似「。」的內容時。或「a」循環無限地繼續並且不終止。我無法找到我的錯誤,並且據我所知,我使用的語法與我的教科書中的語法完全相同。有人能告訴我我的while循環有什麼問題嗎?
int numbers, //How large the set will be
num, //What the user enters for each number
first8position = 0, //The first position in the set that has an 8
last8position = 0; //The last position in the set that has an 8
//Prompt the user to get set size
cout << "How many numbers will be entered? ";
cin >> numbers;
//Loop to get all the numbers of the set and figure out
//which position the first and last 8 are in
for (int position = 1; position <= numbers; position++)
{
cout << "Enter num: ";
cin >> num;
//If num isn't a digit, prompt the user to enter a digit
while (!isdigit(num))
{
cout << "Please enter a decimal number: ";
cin >> num;
}
//If num is 8, and first8position still isn't filled,
//set first8position to the current position.
//Otherwise, set last8position to the current position.
if (num == 8)
{
if (first8position == 0)
first8position = position;
else
last8position = position;
}
}
//If the set had an 8, print what its position was
if (first8position != 0)
cout << "The first 8 was in position " << first8position << endl;
//If there was more than one 8, print the last 8 position.
//Otherwise, the first and last 8 position are the same.
if (last8position != 0)
cout << "The last 8 was in position " << last8position << endl;
else
cout << "The last 8 was in position " << first8position << endl;
//If there were no 8s, say so.
if (first8position == 0)
cout << "Sorry, no eights were entered.";
return 0;
}
相關/欺騙https://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a - – NathanOliver
你也沒有正確使用'std :: isdigit'。要看它是如何工作的,請看:http://en.cppreference.com/w/cpp/string/byte/isdigit – NathanOliver