2017-02-17 14 views
0

http://prnt.sc/e9q619 スイッチのデフォルトは無限ループになりますが、割り当てから他の重要なメモを実行するのに問題があります。私は別の家庭教師に行きましたが、彼はまだクラスで学んでいない方法でベクトルと文字列を使用するように指示しました。したがって、使用する 'c'と 'x'が固定されていない場合は大丈夫です。この無限ループやその他の問題をコードでどのように修正できますか?

#include <iostream> 
#include <stdlib.h> 

using namespace std; 


int main() 
{ 
    int i = 1; 
    int Num1 = 1; 
    int Num2 = 1; 
    char operation; 
    int answer = 1; 

    cout << "Here is a four function integer calculator." << endl; 
    cout << "Please use 'x' to exit the calculator, and 'c' to clear the calculator in the operator space. E.G. ""1 x 3"" would exit the calculator." << endl; 
    cout << "Enter the first number, operation, and second number in respective order, separated by a space each." << endl; 


    while (i > 0) 
    { 
     cin >> Num1 >> operation >> Num2; 
     switch (operation) 
     { 
     case '+': 
      answer = Num1 + Num2; 
      cout << "The solution is: " << answer << "." << endl; 
      break; 
     case '-': 
      answer = Num1 - Num2; 
      cout << "The solution is: " << answer << "." << endl; 
      break; 
     case '*': 
      answer = Num1 * Num2; 
      cout << "The solution is: " << answer << "." << endl; 
      break; 
     case '/': 
      if (Num2 == 0 && operation != 'x') { 
       break; 
      } 
      else { 
       answer = Num1/Num2; 
       cout << "The solution is: " << answer << "." << endl; 
      } 
      break; 
     case 'c': 
      system("cls"); 
      break; 
     case 'x': 
      return 0; 
      break; 
     default: 
      cout << "You entered a wrong number, operator, or form for the entire operation" << endl; 
      break; 
     } 
    } 
    cout << "The solution is: " << answer << "." << endl; 
} 
+0

ループ状態を考えます。 –

答えて

0

私は入力のそれぞれを個別にチェックし、入力検証文を書きました。

#include<iostream> 
#include<stdlib.h> 
using namespace std; 

int main() 
{ 
    int i = 1; 
    int Num1 = 1; 
    int Num2 = 1; 
    char operation; 
    int answer = 1; 

    cout << "Here is a four function integer calculator." << endl; 
    cout << "Please use 'x' to exit the calculator, and 'c' to clear the calculator in the operator space. E.G. ""1 x 3"" would exit the calculator." << endl; 
    cout << "Enter the first number, operation, and second number in respective order, separated by a space each." << endl; 


    while (i > 0) 
    { 
     cout << "Enter Num1: " << endl; 
     cin >> Num1; 
     cout << "Enter operation: " << endl; 
     cin >> operation; 
     cout << "Enter Num2: " << endl; 
     cin >> Num2; 

     while (operation != '+' && operation != '-' && operation != '*' && operation != '/') 
     { 
      cout << "You Suck. Try again." << endl; 
      cout << "Enter Num1: " << endl; 
      cin >> Num1; 
      cout << "Enter operation: " << endl; 
      cin >> operation; 
      cout << "Enter Num2: " << endl; 
      cin >> Num2; 
     } 

     switch (operation) 
     { 
     case '+': 
      answer = Num1 + Num2; 
      cout << "The solution is: " << answer << "." << endl; 
      break; 
     case '-': 
      answer = Num1 - Num2; 
      cout << "The solution is: " << answer << "." << endl; 
      break; 
     case '*': 
      answer = Num1 * Num2; 
      cout << "The solution is: " << answer << "." << endl; 
      break; 
     case '/': 
      if (Num2 == 0 && operation != 'x') { 
       break; 
      } 
      else { 
       answer = Num1/Num2; 
       cout << "The solution is: " << answer << "." << endl; 
      } 
      break; 
     case 'c': 
      system("cls"); 
      break; 
     case 'x': 
      return 0; 
      break; 
     default: 
      cout << "You entered a wrong number, operator, or form for the entire operation" << endl; 
      break; 
     } 
    } 
    cout << "The solution is: " << answer << "." << endl; 
}