2016-09-18 11 views
-2

私は、クレーマーのルールと行列についてプログラムを作成しています。そしてどこかで、私はあなたが「はい」または「いいえ」を入力する必要があるこの問題を抱えています。そして私が他の言葉(例えば笑)をタイプしたら、それはelse if文に行きません。回答を「はい/いいえ」に限定するにはどうすればよいですか?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; 
    } 

私はコードブロックdo-while文をよく読んで

+1

入力を受け入れながらあなたは、入力値を制御することはできません。最初に入力を受け入れる必要がありますし、受け入れられた入力が正しいかどうかの条件を確認することができます。間違った入力に対して警告を生成することができます。 – Raman

+0

'z'の種類は何ですか? –

答えて

4

を使用してC++ でちょうど初心者です!これはポストテストループなので、zがyesかnoかを確認できます。 http://www.cplusplus.com/doc/tutorial/control/

基本的な考え方は次のとおりです。 ユーザー入力が「はい」または「いいえ」に等しくないときにユーザー入力を行います。

Basicの例:

int x; 
do { 
    cout << "Please enter 1 or 0" << endl; 
    cin >> x; 
} while (x != 0 && x != 1); 
関連する問題