2016-09-18 231 views
-2

我正在製作一個關於cramer規則和矩陣的程序(太多空閒時間)。而在某個地方,我有這個問題,您需要輸入「是」或「否」。如果我輸入其他詞語(例如lol),那麼如果陳述或再次提出問題,則不會去其他詞語。我如何限制答案只有是/否?C++ If/else if語句

cout << "There! you have now your first equation! is it " << a << "x+" << b << "y=" << e << "? Is it? Yes/No"<<endl; 
cin >> z; 
if (z== "No") 
    { 
     while (z== "No") 
      { 
       cout << "what is the value of x?"<<endl; 
       cin >> a; 
       cout << "\n"<<endl; 
       cout << "what is the value of y?"<<endl; 
       cin >> b; 
       cout << "\n"<<endl; 
       cout << "what is the value of answer?"<<endl; 
       cin >> e; 
       cout << "\n"<<endl; 
       cout << "There! you have corrected your first equation! is it " << a << "x+" << b << "y=" << e << "? Is it? Yes/No"<<endl; 
       cin >> z; 
      } 
    } 
else if (z== "Yes") 
    { 
     cout << "We will now proceed!"<<endl; 
    } 

我只是個初學者在C++中使用 代碼塊

+1

而接受輸入你無法控制的輸入值。您必須先接受輸入,然後才能檢查接受的輸入是否正確。您可以爲錯誤的輸入生成警告。 – Raman

+0

'z'的類型是什麼? –

回答

4

在while循環做閱讀了!這是一個後期測試循環,因此您可以驗證z是yes還是no。 http://www.cplusplus.com/doc/tutorial/control/

其基本思想是: 在用戶輸入不等於yes或no時執行用戶輸入。

基本例如:

int x; 
do { 
    cout << "Please enter 1 or 0" << endl; 
    cin >> x; 
} while (x != 0 && x != 1);