2017-09-17 1 views
0

Ich versuche, einen Switch Case Calculator zu machen, der nach einem Ausdruck (z. B. 2 + 2) fragt und die Antwort ausdruckt, den Vorgang wiederholend, bis der Benutzer 'q' eingibt.C++ Switch Case Calculator mit Abschlusszeichen

Ich kann nicht herausfinden, wie das Programm beendet wird, wenn der Benutzer 'q' eingibt. Das folgende Programm fragt nach einem Ausdruck, gibt die Antwort und fragt nach einem anderen. Wenn Sie jedoch einen falschen Ausdruck eingeben, wird der Standardfall für immer wiederholt, auch wenn Sie 'q' eingeben.

Ich weiß, das Problem ist zu tun mit, wie ich die Variablen cin, unter Berücksichtigung der Operanden sind vom Typ double und auch etwas ist falsch mit der While-Schleife, aber ich kann nicht an Alternativen denken, und kann nicht scheinen zu finden eine Lösung anderswo.

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

Siehe https://stackoverflow.com/questions/14907978/do-while-endlessly-looping-cout-ignores-cin. –

+0

Lesen Sie die gesamte Zeile mit std :: getline und verwenden Sie dann std :: regex, um Benutzereingaben zu analysieren. –

Antwort

0

Eine einfache Änderung der prob beheben:

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

dieser Code funktioniert nicht, die Endlosschleife ist immer noch da – DevDio

+0

@DevDio Valid-Punkt. Ich habe es jetzt erkannt. Ich wollte es auf eine andere Art und Weise beschreiben, als etwas Deins mag. – MKR

1

Da Sie drei Variablen auf einmal lesen, wenn Sie q in Konsole eingeben, wird es operand1 zugewiesen, aber nicht die operation was der Terminator der while Schleife - daher beginnt die Endlosschleife.

Das grundlegende Problem ist mit der Logik der Anwendung. Die schnellste Auflösung dieses Codes ist unten:

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; 
}