2017-09-17 6 views
0

式(例えば2 + 2)を求めて答えを表示し、ユーザーが 'q'を入力するまでプロセスを繰り返すスイッチケース計算機を作ろうとしています。終端文字を持つC++スイッチケース計算機

ユーザーが 'q'を入力したときにプログラムを終了させる方法がわかりません。以下のプログラムは、式を求めて答えを出し、別の式を求めます。しかし、間違った式を入力すると、 'q'を入力するときを含めて、デフォルトのケースが永遠に繰り返されます。

問題は、変数がどのようにcinされているかを知っています。オペランドはdouble型で、whileループでは何か問題があると考えていますが、どのような選択肢も考えられません。他の場所の解決策。

int main() { 

double operand1; 
double operand2; 
char operation; 
double answer; 

while (operation != 'q'){ 

cout << "Enter an expression:""\n"; 
cin >> operand1 >> operation >> operand2; 


    switch(operation) 
{ 
    case '+': 
     answer = (operand1 + operand2); 
     break; 

    case '-': 
     answer = (operand1 - operand2); 
     break; 

    case '*': 
     answer = (operand1 * operand2); 
     break; 

    case '/': 
     answer = (operand1/operand2); 
     break; 


    default: 
      cout << "Not an operation :/"; 
      return 0; 

} 

    cout <<operand1<<operation<<operand2<< "=" << answer<< endl; 

} 
return 0; 
} 
+0

はhttps://stackoverflow.com/questions/14907978/do-while-endlessly-looping-cout-ignores-cinを参照してください。 –

+0

std :: getlineで行全体を読み込み、std :: regexを使ってユーザ入力を解析します。 –

答えて

0

簡単な変更はPROBを修正します:

int main() 
{ 

    double operand1; 
    double operand2; 
    char operation = '8'; 
    double answer; 


    while (operation != 'q'){ 

     cout << "Enter an expression:""\n"; 
     cin >> operand1 >> operation >> operand2;  
     switch(operation) 
     { 
      case '+': 
       answer = (operand1 + operand2); 
       break; 

      case '-': 
       answer = (operand1 - operand2); 
       break; 

      case '*': 
       answer = (operand1 * operand2); 
       break; 

      case '/': 
       answer = (operand1/operand2); 
       break; 


      default: 
        cout << "Not an operation :/"; 
        return 0; 

     } 

     cout <<operand1<<operation<<operand2<< "=" << answer<< endl; 

     cout << "Wants to quit? Enter (q)"\n"; 
     cin >> operation; 

    } 
return 0; 
} 
+1

そのコードは機能しません、無限ループはまだそこにあります – DevDio

+0

@DevDio有効な点。私は今気づいた。私はあなたの好きなものと違う方法でそれを入れたかったのです。 – MKR

1

一度に3つの変数を読んでいるので、あなたがコンソールにqを入力すると、あるoperationoperand1に割り当てられていないが、 whileループのターミネータ - したがって、無限ループが開始されます。

基本的な問題は、アプリケーションのロジックにあります。そのコードの最速解像度は以下の通りです:

int main() { 

    double operand1; 
    double operand2; 
    char operation; 
    char more = 'y'; 
    double answer; 

    while (more != 'n') { 

     cout << "Enter an expression:""\n"; 
     cin >> operand1 >> operation >> operand2; 

     switch (operation) 
     { 
     case '+': 
      answer = (operand1 + operand2); 
      break; 

     case '-': 
      answer = (operand1 - operand2); 
      break; 

     case '*': 
      answer = (operand1 * operand2); 
      break; 

     case '/': 
      answer = (operand1/operand2); 
      break; 


     default: 
      cout << "Not an operation :/"; 
      return 0; 

     } 

     cout << operand1 << operation << operand2 << "=" << answer << endl; 
     cout << "Continue? (y/n) "; 
     cin >> more; 
    } 
    return 0; 
}