2017-09-07 15 views
2

ユーザが何か愚かなことをしたときにコードエラーを受け入れないプログラムを作ろうとしています。整数の文字列を入れるなど、この時点で何をすべきかわかりません。私はこの時点で段落全体を書いたにもかかわらず、十分な細部を持っていないと言っているので、ランダムなものを書きます。むしろ、その後C++ cin.fail()whileループ

void scoretoletter::letter() { 
int a = 0; 
int question; 
while (a == 0) { 
    cout << "1.Rectangle" << endl; 
    cout << "2.Triangle" << endl; 
    cout << "3.Circle" << endl; 
    cout << "4.Exit" << endl; 
    float area; 
    float Width; 
    float Length; 
    float Height; 
    float base; 
    float radius; 
    int l; 
    cin >> question; 
    if (question == 1) { 
     cout << "Whats your length?" << endl; 
     cin >> Length; 
     if (cin.fail()) { 
      cout << "That is not valid try again" << endl; 
     } 
     else { 
      cout << "Whats your width?" << endl; 
      cin >> Width; 
      if (cin.fail()) { 
       cout << "That is not valid try again" << endl; 

      } 
      else { 
       if (Length == 0 || Width == 0) { 
        cout << "That is not valid try again." << endl; 
        system("pause"); 
       } 

       else { 
        area = Length * Width; 
        cout << "The area is: " << area << endl; 
       } 
      } 
     } 
    } 

    else if (question == 2) { 
     cout << "What is the Base?" << endl; 
     cin >> base; 
     if (cin.fail()) { 
      cout << "That is not valid try again." << endl; 


     } 
     else { 
      cout << "What is the Height?" << endl; 
      cin >> Height; 
      if (cin.fail()) { 
       cout << "That is not valid try again." << endl; 

      } 
      else { 
       if (base == 0 || Height == 0 || cin.fail()) { 
        cout << "That is not valid try again." << endl; 
        system("pause"); 

       } 
       else { 
        area = base * Height * .5; 
        cout << "The area is: " << area << endl; 
       } 
      } 
     } 
    } 
    else if (question == 3) { 
     cout << "What is the radius?" << endl; 
     cin >> radius; 
     if (radius == 0 || cin.fail()) { 
      cout << "That is not valid try again." << endl; 
      system("pause"); 


     } 
     else { 


      area = radius * radius * 3.14; 
      cout << "The area is: " << area << endl; 
     } 
    } 
    else if (question == 4) { 
     a = 1; 
    } 
    else { 
     cout << "That is not valid try again." << endl; 
    } 
    system("pause"); 








} 

}

+1

'int 'の入力を行う関数を定義することを検討してください。ユーザーに再試行を求める内部ループを持つことができます。 'cin 'を失敗状態にしないことが重要です(それはそれ以後の入力を無視します)。その関数では' 'ヘッダの' getline'を使用して文字列として行を読み込むことをお勧めします。または 'int'に変換するものを指定します。 –

+0

また、エラーは 'std :: cin.clear()'でクリアすることができます。 – spectras

答えて

3

ような深い入れ子になった醜いのif-else構文を使用して、あなたはreturnディレクティブを使用して、このはるかに簡単に書くことができます。重複を避けるために、成功するまでステータスとループを返すこともできます。

enum result_status 
{ 
    RESULT_STATUS_OK, 
    RESULT_STATUS_INVALID, 
    RESULT_STATUS_DONE 
} 

void scoretoletter::askQuestions() { 
    while(true) 
    switch(letter()) 
    { 
     RESULT_STATUS_OK: 
     system("pause"); 
     continue; 

     RESULT_STATUS_INVALID: 
     cout << "That is not valid try again." << endl; 
     system("pause"); 
     cin.clear();    
     continue; 

     RESULT_STATUS_DONE: 
     system("pause"); 
     return; 
    } 
} 

enum result_status scoretoletter::letter() { 
    int question; 

    cout << "1.Rectangle" << endl; 
    cout << "2.Triangle" << endl; 
    cout << "3.Circle" << endl; 
    cout << "4.Exit" << endl; 
    float area; 
    float Width; 
    float Length; 
    float Height; 
    float base; 
    float radius; 
    int l; 
    cin >> question; 

    if (question == 1) { 
     cout << "Whats your length?" << endl; 
     cin >> Length; 
     if (cin.fail()) 
      return RESULT_STATUS_INVALID; 

     cout << "Whats your width?" << endl; 
     cin >> Width; 
     if (cin.fail() || Length == 0 || Width == 0) 
      return RESULT_STATUS_INVALID; 

     area = Length * Width; 
     cout << "The area is: " << area << endl; 
     return RESULT_STATUS_OK; 
    } 

    if (question == 2) { 
     cout << "What is the Base?" << endl; 
     cin >> base; 
     if (cin.fail()) 
      return RESULT_STATUS_INVALID; 

     cout << "What is the Height?" << endl; 
     cin >> Height; 
     if (cin.fail()) 
      return RESULT_STATUS_INVALID; 

     if (base == 0 || Height == 0 || cin.fail()) { 
      return RESULT_STATUS_INVALID; 

     area = base * Height * .5; 
     cout << "The area is: " << area << endl; 
     return RESULT_STATUS_OK; 
    } 

    if (question == 3) { 
     cout << "What is the radius?" << endl; 
     cin >> radius; 
     if (radius == 0 || cin.fail()) { 
      return RESULT_STATUS_INVALID; 

     area = radius * radius * 3.14; 
     cout << "The area is: " << area << endl; 
     return RESULT_STATUS_OK; 
    } 

    if (question == 4) 
     return RESULT_STATUS_DONE; 

    return RESULT_STATUS_INVALID; 
} 

system("pause");の使用も悪い考えで、あなたが本当にあなたのプログラムに独自のPress any key to continue functionallityを記述する必要があります。