2017-10-19 28 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