2016-11-30 45 views
-3

我有一個任務來創建一個簡單的計算器,但我無法讓程序退出;它仍然處於連續循環。我究竟做錯了什麼?簡單的計算器C++,如何正確地循環和退出?

這項任務很簡單: 編寫一個C++程序,使用執行加法,減法,乘法和除法操作的函數(確保覆蓋的情況以避免被零除)來模擬基本的計算器。

顯示可執行的操作列表的菜單,並從用戶那裏獲得關於他的計算選擇的輸入。根據用戶的操作選擇,從用戶那裏輸入號碼。假設所有輸入值都是double類型的。計算必須使用返回double值的函數來實現。計算結果應顯示在屏幕上。

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

double add(double num1, double num2) 
{ 
    return num1 + num2; 
} 

double subtract(double num1, double num2) 
{ 
    return num1 - num2; 
} 

double multiply(double num1, double num2) 
{ 
    return num1 * num2; 
} 

double divide(double num1, double num2) 
{ 
    if (num2 != 0) 
     return num1/num2; 
    else 
     cout << "Invalid entry"; 
} 

int main() 
{ 
    double num1, num2; 
    char operation, q; 

    cout << "Calculator functions:" << endl; 
    cout << "Enter 'm' for Multiplication" << endl; 
    cout << "Enter 'd' for Division" << endl; 
    cout << "Enter 'a' for Addition" << endl; 
    cout << "Enter 's' for Subtraction" << endl; 
    cout << "Select operation or 'q' to quit program" << endl; 
    cin >> operation; 

    do 
    { 
     if (operation == 's' || operation == 'S') 
     { 
      cout << "Enter first number" << endl; 
      cin >> num1; 
      cout << "Enter first number" << endl; 
      cin >> num2; 
      cout << "The difference is " << subtract(num1, num2) << endl; 
     } 

     if (operation == 'a' || operation == 'A') 
     { 
      cout << "Enter first number" << endl; 
      cin >> num1; 
      cout << "Enter first number" << endl; 
      cin >> num2; 
      cout << "The sum is " << add(num1, num2) << endl; 
     } 

     if (operation == 'm' || operation == 'M') 
     { 
      cout << "Enter first number" << endl; 
      cin >> num1; 
      cout << "Enter first number" << endl; 
      cin >> num2; 
      cout << "The product is " << multiply(num1, num2) << endl; 
     } 

     if (operation == 'd' || operation == 'D') 
     { 
      cout << "Enter numerator" << endl; 
      cin >> num1; 
      cout << "Enter denominator" << endl; 
      cin >> num2; 
      cout << "The quotient is " << divide(num1, num2) << endl; 
     } 
     cout << "Select operation or 'q' to quit program" << endl; 
     cin >> operation; 
    } while (operation != 'q' || operation != 'Q'); 
    cout << "Goodbye!" << endl; 
    system("pause"); 
    return 0; 
} 
+1

'而(!操作= 'Q' ||操作= 'Q');'=>'而(!操作= 'Q' &&操作= 'Q');' –

+0

'operation!='q'|| operation!='Q''對於任何值都是成立的。您應該遵循@πάνταῥεῖ的建議,或者始終將您的輸入字符轉換爲小寫字母(或上部字符,您的選擇),然後您只需檢查單個字符。 –

+0

在附註中 - 您是否考慮過首先將'operation'轉換爲大寫或小寫,並跳過所有的OR? –

回答

1

更改此:while (operation != 'q' || operation != 'Q');while (operation != 'q' && operation != 'Q');

您的程序要求cout << "Enter first number" << endl;兩次,改變第二個這個cout << "Enter second number" << endl;

,並把這個當用戶對輸入0來處理的情況下分母:

if (operation == 'd' || operation == 'D') 
     { 
      cout << "Enter numerator" << endl; 
      cin >> num1; 
      cout << "Enter denominator" << endl; 
      cin >> num2; 
      if (num2 != 0) 
      { 
       cout << "The quotient is " << divide(num1, num2) << endl; 
      } 
      else 
      { 
       cout << "You can't devide by zero." << endl; 
      } 
     }