Basic Calculator Loop???

Hey, I am new to C++, I wanted to create a basic calculator and at the end of the program have a prompt which asks the user if they want to loop the program to enter a new equation or to just end the program. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
	
using namespace std;

int main(int argc, char *argv[])
{

double numOne, numTwo, answer;
char sign;

cout<<"Enter your equation (ex: 1 + 2)"<<endl;
cin>>numOne>>sign>>numTwo;

//Addition
if(sign == '+'){
	cout<<numOne<<" + "<<numTwo<<" = "<<numOne+numTwo;	
}
//Subtraction
else if(sign == '-'){
	cout<<numOne<<" - "<<numTwo<<" = "<<numOne-numTwo;	
}
//Multiplication
else if(sign == '*'){
	cout<<numOne<<" * "<<numTwo<<" = "<<numOne*numTwo;	
}
//Division with 0 Error 
else if(sign == '/' && numTwo == 0){
	cout<<"Invalid operation, you can not divide by 0";
}
//Division
if(sign =='/' && numTwo != 0){
	cout<<numOne<<" / "<<numTwo<<" = "<<numOne/numTwo;
}
return 0;
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/7352/

oops, or more to the point:

Note: search "while yes no" has plenty of precious examples on this site

http://www.cplusplus.com/forum/beginner/150171/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
bool loop=1;
double numOne, numTwo, answer;
char sign, Restart;
do{
cout<<"Enter your equation (ex: 1 + 2)"<<endl;
cin>>numOne>>sign>>numTwo;

//Addition
if(sign == '+'){
	cout<<numOne<<" + "<<numTwo<<" = "<<numOne+numTwo;
}
//Subtraction
else if(sign == '-'){
	cout<<numOne<<" - "<<numTwo<<" = "<<numOne-numTwo;
}
//Multiplication
else if(sign == '*'){
	cout<<numOne<<" * "<<numTwo<<" = "<<numOne*numTwo;
}
//Division with 0 Error
else if(sign == '/' && numTwo == 0){
	cout<<"Invalid operation, you can not divide by 0";
}
//Division
if(sign =='/' && numTwo != 0){
	cout<<numOne<<" / "<<numTwo<<" = "<<numOne/numTwo;
}
cout<<endl<<"Would you like to Restart? Y or N?"<<endl;
cin>>Restart;
cout<<endl;
if(Restart=='n'||Restart=='N')
    loop=0;
}while(loop);
return 0;
}
Topic archived. No new replies allowed.