2017-10-19 27 views
1

现在我有while循环工作,但现在它循环的第一个操作的答案,而不是要求输入另一个数字更多操作和当我按x退出它不会退出。我只是想知道在哪里把代码行要求输入另一组数字。然后对于输入Y或X是继续,x退出,我可以为此输入代码行。我只需要知道在哪里所有的感谢:)我不能循环工作,当我按下按钮退出我的开关它不起作用

#include <iostream> 
using namespace std; 
char op, x, y; 





double scanNumber() { 
    double d; 

    cin >> d; 

    return d; 
} 

bool readOperator(string s, char &operation) { 
    char c; 
    cout << "\nEnter Operator: "; 
    cin >> c; 
    if (s.find(c) != -1) { 
     operation = c; 
     return true; 
    } 
    return false; 
} 

double add(double d1, double d2) { 
    return d1 + d2; 
} 

double sub(double d1, double d2) { 
    return d1 - d2; 
} 

double mul(double d1, double d2) { 
    return d1*d2; 
} 


double division(double d1, double d2) { 
    return d1/d2; 
} 
int main() 

{ 

    double d1, d2; 


    cout << "Enter 1st Number: "; 
    d1 = scanNumber(); 

    cout << "Enter 2nd Number: "; 
    d2 = scanNumber(); 

    char operation; 
    while (!readOperator("+-*/", operation)) { 
     cout << "Invalid Operator Please Pick Another One" << endl; 


    } 

    bool valid; 
    double result; 



    do{ 
     switch (operation) 
     { 

     case '+': 
      result = add(d1, d2); 
      break; 
     case '-': 
      result = sub(d1, d2); 
      break; 
     case '*': 
      result = mul(d1, d2); 
      break; 
     case '/': 
      if (d2 == 0) { 
       cout << "For division, 2nd operator can't be 0." << endl; 

      } 
      else { 
       result = division(d1, d2); 
      } 
      break; 
     case 'c': 
      cout << "Clearing the Calculator " << endl; 
      valid = false; 
      break; 
     case 'x': 
      exit(0); 
     default: 
      cout << "invalid input"; 

     } 

    cout << "\nResult: " << result << endl; 
    cout << "\nDo another?(Enter 'y'for yes or 'x' to exit) "; 

    cin >> y, x; 
}while (op != 'x'); 

return 0; 
} 
+0

要么使用'函数getline()'来阅读你的投入,或使用'CIN .ignore()'在读取'operation'后跳过输入流中留下的换行符。 – Barmar

+0

“return”语句后面的任何代码都不会被执行。执行将在执行下一个语句之前离开函数。 –

+0

没有什么能检查'ch'是'y'还是'x'。 – Barmar

回答

1

你没有逻辑重复循环后,你做了一个计算。所以你需要一个do与你最后离开的唯一while (op != 'x');配对。

这是你如何能做到这一点:

int main() { 

    double d1, d2; 

    do { //main loop now starts here 
     cout << "Enter 1st Number: "; 
     d1 = scanNumber(); 
     cout << "Enter 2nd Number: "; 
     d2 = scanNumber(); 

     char operation; 

     //since you have a switch on the c and x operator they need to be taken into 
     //consideration in this while loop 
     while (!readOperator("+-*/cCxX", operation)) { 
      cout << "Invalid Operator Please Pick Another One" << endl; 
     } 

     bool valid; 
     double result; 

     switch (operation) { 
     case '+': result = add(d1, d2); break; 
     case '-': result = sub(d1, d2); break; 
     case '*': result = mul(d1, d2); break; 
     case '/': 
      if (d2 == 0) { 
       cout << "For division, 2nd operator can't be 0." << endl; 
      } 
      else { 
       result = division(d1, d2); 
      } 
      break; 
     case 'c': 
     case 'C': 
      cout << "Clearing the Calculator " << endl; 
      valid = false; 
      break; 
     case 'x': 
     case 'X': return 0; 
     default: cout << "invalid input"; 
     } 

     //You need to use the valid as well to know if you show the result 
     if (valid) cout << "\nResult: " << result << endl; 
     cout << "\nDo another(Enter 'y'for yes or 'x' to exit) ? "; 
     cin >> ch; 

    } while (ch != 'x'); //now the } ends here and the condition uses ch 

    return 0; //return is now after the while 
} 

采样运行与建议输入:

enter image description here

+0

谢谢你的帮助@Isac,但现在我遇到了另一种情况,在我做了2次数学运算后,我输入了第三个操作符并按回车。它给了我无效的操作员,并保持打印出无效操作员的行,当我按下c清除它也给我一个无效的操作员,它应该清除计算器。谢谢你们到目前为止的一切! – Jerry

+0

@Jerry我似乎无法重新创建该错误。你能准确地告诉我你输入了什么输入和它的确切顺序吗? – Isac

+0

是的,谢谢!第一次手术我做了3 + 3 = 6。 2 * 4 = 8表示第二次操作。第三次是在输入我输入给操作员的数字后,当它开始输出无效的操作员时,请不停地选择另一个。 – Jerry