2017-10-26 12 views
0

私は、ユーザーが負の数、偶数または奇数を入力すると、 を入力するプログラムを作成しています。プログラムは質問に「別の番号を入力しますか?y 'y'と 'n'の他に何かを入力するユーザーを考慮する必要があります。 ユーザーが整数を入力しない場合は、アカウントを設定する必要があります。彼らは整数を入力して(さえ正または負の奇数又は)その場合ならば、プログラムは、判定のループ処理を経るプログラムのループとif文で問題が発生する

int value; 
char choice; 

cout << "Please enter a number" << endl; 
cin >> value; 
while (!(cin >> value)) { 
    cin.clear(); 
    cin.ignore(999, '\n'); 
    cout << "Invalid data type! Please enter 'value' again" << endl; 
} 
if (value > 0 && value % 2 == 0) { 

    cout << value << " is even" << endl; 
    cout << value << " is positive" << endl; 
} 

else if (value < 0 && value % 2 != 0) { 
    cout << value << " is odd" << endl; 
    cout << value << " is negative" << endl; 
} 

else if (value > 0 && value % 2 != 0) { 
    cout << value << " is odd" << endl; 
    cout << value << " is postive" << endl; 
} 

else if (value < 0 && value % 2 == 0) { 
    cout << value << " is even" << endl; 
    cout << value << " is negative" << endl; 
} 
cout << "Would you like to try another number? Say y(es) or n(o)" << endl; 
cin >> choice; 

while (choice != 'y' &&choice != 'n') { 
    cin.clear(); 
    cin.ignore(999, '\n'); 
    cout << "Invalid response! Please enter 'choice' again" << endl; 
} 

do { 
    if (choice == 'y') { 
     cout << "Please enter a number" << endl; 
     cin >> value; 
     if (!(cin >> value)) { 
      cin.clear(); 
      cin.ignore(999, '\n'); 
      cout << "Invalid data type! Please enter 'value' again" << endl; 

     if (value > 0 && value % 2 == 0) { 

      cout << value << " is even" << endl; 
      cout << value << " is positive" << endl; 
     } 

     else if (value < 0 && value % 2 != 0) { 
      cout << value << " is odd" << endl; 
      cout << value << " is negative" << endl; 
     } 

     else if (value > 0 && value % 2 != 0) { 
      cout << value << " is odd" << endl; 
      cout << value << " is postive" << endl; 
     } 

     else if (value < 0 && value % 2 == 0) { 
      cout << value << " is even" << endl; 
      cout << value << " is negative" << endl; 
     } 
     cout << "Would you like to try another number? Say y(es) or n(o)" << endl; 
     cin >> choice; 
     } 
    } 
} while (choice == 'n'); 
    cout << "Thank you for using my program. Goodbye!" << endl; 
return 0; 

}

を必要とします
+0

。 –

+0

私のキャラクターは文字列であるべきですか? – JakeGatsby

+0

あなたの質問は何ですか? – unreleased

答えて

0

すべての条件をチェックするには、ネストされたdo-whileループが必要です。 ここではcin.fail()を使用しています。 cin.fail()は、入力された値が変数に定義された値に適合するかどうかを検出します。

int value; 
char choice; 
     do{  
      cout << "Please enter a number" << endl; 
      cin >> value; 
      if(cin.fail()) // check if input is int 
      { 
       cout<<"Not an int"; 
       choice = 'y'; 
      } 
      else 
      { 
       if (value > 0 && value % 2 == 0) 
       { 

        cout << value << " is even" << endl; 
        cout << value << " is positive" << endl; 
       } 

       else if (value < 0 && value % 2 != 0) 
       { 
        cout << value << " is odd" << endl; 
        cout << value << " is negative" << endl; 
       } 

       else if (value > 0 && value % 2 != 0) 
       { 
        cout << value << " is odd" << endl; 
        cout << value << " is postive" << endl; 
       } 

       else if (value < 0 && value % 2 == 0) 
       { 
        cout << value << " is even" << endl; 
        cout << value << " is negative" << endl; 
       } 
       do{ 
        cout << "Would you like to try another number? Say y(es) or n(o)" << endl; 
        cin >> choice; 
       }while(choice != 'y' || choice != 'n'); 
      } 
     }while (choice == 'n'); 

また、あなたはこれを読む必要があります:あなたは二回>> CIN [値]書いているChecking input value is an integer

関連する問題