简体   繁体   English

C++ 程序,用于开发基于文本的计算器,您可以在其中进行加法、减法、乘法和除法

[英]C++ program to develop a text based calculator where you can do addition, subtraction, multiplication and division

So the issue i'm having is that when I run the program, it asks for the first input but after i give the operator, the output screen just stops and i have to force quit the output screen.所以我遇到的问题是,当我运行程序时,它要求第一个输入,但在我给操作员之后,output 屏幕刚刚停止,我不得不强制退出 output 屏幕。 I am using while loops to have the user input as many numbers as they want and when they enter = symbol it should end the program and print the output.我正在使用while循环让用户输入任意数量的数字,当他们输入=符号时,它应该结束程序并打印output。

//  C++ program to develop a simple text based calculator where you can do addition, subtraction, multiplication and division.
#include <iostream>
using namespace std;
class calci
{
        private:
            char op; // variable for the operator
            double no;   // variable for getting input numbers    
            double out; // for the output
        
        public:
            void input(char ,double,double);    //to accept input values as well as calculate the output
            
            void output();      //to print the output
};
void calci :: input(char op,double no,double out)
{   op= '+';        //given as a default value for op so that the while loop can start
    
    while(op != '=')    // Used just to make the user input as many numbers as they want untill they give a '=' sign
    {
        std::cout <<"Enter the first number:"<<endl;
        std::cin >>no;
        std::cout <<"\nEnter the operation to be performed with this number";
        std::cout <<"\nEnter + for Additon \nEnter - for Subtraction \nEnter * for multiplication \nEnter / for division \nEnter = to produce the output"<<endl;
        std::cin >>op;
        while(op != '=')
        {
            switch(op)
            {
                case '+' :
                    out=out+no;
                    break;
                case '-' :
                    out=out-no;
                    break;
                case '*':
                    out=out*no;
                    break;
                case '/':
                    out=out/no;
                    break;
                default:
                    break;
            }
        }
    }
    
}
void calci :: output()
{ 
    cout<<"the final answer is"<<out;
    
}
int main() 
{   
    calci c;
    std::cout << "Hello!"<<endl;
    c.input('+',2.0,2.0);
    c.output();
    return 0;
    
}

i have fixed the program, removing class.我已经修复了程序,删除了 class。 I have used just one function.我只使用了一个 function。

//A C++ program to develop a simple calculator where you can do addition, subtraction, multiplication and division with as many numbers as you want.


#include <iostream>
using namespace std;

int main()
{
    char op;    //variable to accept operators
    double no,n1;   //variables to accept input numbers
    double out;     //variable to calculate the output
    cout<<"Hello!"<<endl;
    cout<<"Enter the first number: ";
    cin>>n1;
    out=n1;
    while(op != '=')
    {   std::cout << "\nEnter the operation to be performed ";
        std::cout << "\nEnter \"+\" for Additon \nEnter \"-\" for Subtraction \nEnter \"*\" for multiplication \nEnter \"/\" for division \nEnter \"=\"  to end calculation"<< endl;
        cin>>op;
        if(op == '=')
            goto printing;
        else
        {
            std::cout << "Enter the next number:" << endl;
            std::cin >> no;
            switch(op)
                {
                    case '+' :
                        out = out + no;
                        break;
                    case '-' :
                        out = out - no;
                        break;
                    case '*':
                        out = out * no;
                        break;
                    case '/':
                        out = out / no;
                        break;
                    default:
                        break;
                }
          }
    }
    printing:
      cout<<"The final answer is ="<<out;
    return 0;
}

the while do loop is an endless loop. while do 循环是一个无限循环。 The breaks inside the switch-case statement end the switch-case. switch-case 语句中的断点结束了 switch-case。 They dont effect the while loop.它们不会影响 while 循环。 One possiblity to solve the problem is to move the 'std::cin >>op;'解决该问题的一种可能性是移动 'std::cin >>op;' in the inner loop.在内循环中。

void calci::input(char op, double no)
{   
    out = 0.0;
    std::cout << "Enter the first number:" << endl;
    std::cin >> no;
    std::cout << "\nEnter the operation to be performed with this number";
    std::cout << "\nEnter + for Additon \nEnter - for Subtraction \nEnter * for multiplication \nEnter / for division \nEnter = to produce the output"<< endl;
    
    while(op != '=')
    {
        std::cin >>op;
        switch(op)
        {
            case '+' :
                out=out+no;
                break;
            case '-' :
                out=out-no;
                break;
            case '*':
                out=out*no;
                break;
            case '/':
                out=out/no;
                break;
            default:
                break;               
        }
    }
}

There are many more problems还有很多问题

  1. The calculation does not output the correct result. output 的计算结果不正确。 Because you use the out parameter in the input function.因为你在输入 function 中使用了 out 参数。 Take a look to the scope of a variable看一下变量的 scope
  2. the out variable in calci is not initialized. calci 中的 out 变量未初始化。 Your program has undefined behaivour您的程序有未定义的行为
  3. the op parameter of input does not effect the calculation, since the first line override the parameter输入的 op 参数不影响计算,因为第一行覆盖了参数
  4. the outer while loop does not effect the caluculation.外部 while 循环不影响计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM