Simple Calculator Program

Oct 10, 2011 at 4:00pm
Hello Cplusplus, I have been working on a simple calculator program. I have the essential components put together and have created a function to perform various operations. I would just like some suggestions on how to tidy my code up a bit, remove any unneeded code, and to learn about any other methods to do this.

Thank you, I appreciate your help and suggestions!

Code:

#include<iostream>

using namespace std;

double calculate(double x, double y)
{
cout<<"Select the operation you wish to perform(+,-,*,/):";
char operate;
cin>>operate;
double answer;
if(operate=='+')
answer=x+y;
else if(operate=='-')
answer=x-y;
else if(operate=='/')
answer=x/y;
else if(operate=='*')
answer=x*y;
else
cout<<"That is not an operation I know.";

return answer;
}



int main()
{ cout<<"\t\t\t***Simple Calculator***\n\n";

double a;
double b;
double solution;

cout<<"Please enter two numbers seperated with a space\n";
cin>>a>>b;
solution=calculate(a, b);
cout<<solution<<endl;





system("pause");
return 0;
}
Last edited on Oct 10, 2011 at 4:09pm
Oct 10, 2011 at 4:14pm
You can use a switch case in place of if else.
It is of the structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(operate)
{
    case '+':
        //...
        break;
    
    case '-':
        //...
        break;
    //...
    //...
    default:
        cout<<"That is not an operation I know.";
        break;
}


It is good for comparing one variable with many values (works only for values which can be cast into integer).

Other than that:
Use [code][/code] tags!!!
Oct 10, 2011 at 4:35pm
Nisheeth, thanks for the suggestion. The only problem is when I use a switch I get a run-time error.
"Run-Time Check Failure #3 - The variable 'answer' is being used without being initialized."

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>

using namespace std;

double calculate(double x, double y)
{
	cout<<"Select the operation you wish to perform(+,-,*,/):";
	double answer;
	int operate;
	cin>>operate;
	
	switch(operate) {
	case '+': answer=x+y;
	break;

	case '-': answer=x-y;
	break;

	case '*': answer=x*y;
	break;

	case '/': answer=x/y;
	break;

	default: cout<<"I don't know that operation.";
	}
	

	return answer;
}



int main()
{	cout<<"\t\t\t***Simple Calculator***\n\n";

	double a;
	double b;
	double solution;

	cout<<"Please enter two numbers seperated with a space:\n";
	while(cin>>a>>b){


	solution=calculate(a, b);
	cout<<solution<<endl;

	cout<<"Please enter two more numbers: ";
	}
	system("pause");
	return 0;
}








Last edited on Oct 10, 2011 at 4:41pm
Oct 10, 2011 at 4:43pm
'operate' should probably be a char and not an int.

Also you should probably initialize answer:

1
2
	double answer = 0;  // initailize it so it's something known
	char operate;  // char, not int 

Oct 10, 2011 at 4:51pm
Ahhh, thank you so much. I tried initializing answer to 0 earlier and that didn't work for some strange reason. However, when I changed 'operate' to the char type it solved the problem.

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
45
46
47
48
49
50
51
52
53
#include<iostream>

using namespace std;

double calculate(double x, double y)
{
	cout<<"Select the operation you wish to perform(+,-,*,/):";
	char operate;
	cin>>operate;
	double answer=0;
	switch(operate) {
	

	case '+': answer=x+y;
	break;

	case '-': answer=x-y;
	break;

	case '*': answer=x*y;
	break;

	case '/': answer=x/y;
	break;

	default: cout<<"I don't know that operation.";
	break;
	}
	

	return answer;
}



int main()
{	cout<<"\t\t\t***Simple Calculator***\n\n";

	double a;
	double b;
	double solution;

	cout<<"Please enter two numbers seperated with a space:\n";
	while(cin>>a>>b){


	solution=calculate(a, b);
	cout<<solution<<endl;

	cout<<"Please enter two more numbers: ";
	}
	return 0;
}


So, now it looks like this. It compiles without any errors and does what it's intended to do. Thanks!


Last edited on Oct 10, 2011 at 4:53pm
Oct 11, 2011 at 2:23am
You are writing the simple calculator program in console so your program will not be user friendly.User have to follow two step to calculate just sum or other operation so make a program that user can preforms any operation only in one step.You can also use graphics library here is the sample code http://codeincodeblock.blogspot.com/2011/06/sample-calculator-with-windows-form.html written in C# and windows form application.If you don't have C# knowledge the try this link http://codeincodeblock.blogspot.com/2011/10/conversion-of-infix-operation-to.html
Oct 11, 2011 at 3:20am
Thanks for the suggestion Dinesh, I have no desire to incorporate graphics into this particular project. My only intent was to create a very simple calculator program. I do agree, however, that the 2 steps must be consolidated into one single step to be more efficient.
Topic archived. No new replies allowed.