0

したがって、私が抱えている問題は、プログラムを実行すると最初の入力を求められるが、オペレーターに入力した後、出力画面が停止し、出力画面を強制終了する必要があることです。while ループを使用して、ユーザーが必要な数の数字を入力できるようにしています。ユーザーが = 記号を入力すると、プログラムを終了して出力を出力する必要があります。

//  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;
    
}
4

2 に答える 2

1

クラスを削除してプログラムを修正しました。1つの機能だけを使用しました。

//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;
}
于 2021-01-23T09:01:07.300 に答える