简单的计算器c ++,如何正确循环和退出?

时间:2016-11-30 03:11:15

标签: 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 个答案:

答案 0 :(得分: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;
            }
        }