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. 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.
// 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. I have used just one 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. The breaks inside the switch-case statement end the switch-case. They dont effect the while loop. One possiblity to solve the problem is to move the '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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.