我剛開始用C++(從Java來),我試圖做一些基本的練習。這個想法是要求除5以外的任何輸入,如果用戶輸入5,顯示消息,並且如果用戶輸入10次以外的任何其他信息,則顯示另一個消息。這裏的代碼:C++輸入不被讀取
void notFive() {
int count = 0;
while (count < 10) {
int input = 0;
cout << "Enter any number other than 5." << endl;
cin >> input;
if (input == 5)
break;
count++;
}
if (count == 10)
cout<<"You are more patient than I am, you win.";
else
cout << "You weren't supposed to enter 5!";
}
}
我的問題是,所有這些代碼是打印出「輸入除5以外的任何數字」。 10次,然後說「你對我更有耐心,你贏了。」任何想法有什麼不對?
,如果你們希望我所有的代碼(以確保我不只是作爲一個白癡),那就是:
#include <iostream>
#include <stdio.h>
using namespace std;
class Hello {
public:
void notFive() {
int count = 0;
while (count < 10) {
int input = 0;
cout << "Enter any number other than 5." << endl;
if (! (cin >> input)) {
cout << "std::cin is in a bad state! Aborting!" << endl;
return;
}
if (input == 5)
break;
count++;
}
if (count == 10)
cout<<"You are more patient than I am, you win.";
else
cout << "You weren't supposed to enter 5!";
}
}hello;
int main() {
Hello h;
h.notFive();
return 0;
}
你今天做之前,任何其他輸入?如果'cin'處於不良狀態,它將不會嘗試執行更多輸入。 – 2013-03-18 19:11:38
根據猜測,標準輸入未連接到交互式終端對等設備。您使用什麼操作系統? – antlersoft 2013-03-18 19:12:20
當你使用調試器時,哪一行失敗? – 2013-03-18 19:12:27