My first calculator :)

Hello everyone.. spent a few hours studying and some time writing this code for a calculator.. I am trying to do the addition portion (sum) of the project first before I continue. I am stuck so far with this. Please help!


#include <iostream>
using namespace std;

int main () {
int iVal1, iVal2;
char chOperator[] = { '+', '-','/', '*' };
int sum = iVal1 + iVal2

cout << "Enter an integer:";
cin >> iVal1;
cout << "Enter another integer:";
cin >> iVal2;
cout << "Enter the operation you want to perform:";
cin >> chOperator;
if ( chOperator == + );
cout << "The result is " << sum << endl;
return 0;
}
You are not creating a function for sum. Please read here:

http://www.cplusplus.com/doc/tutorial/functions/
And read this thread, as it is EXACTLY the same problem: http://www.cplusplus.com/forum/beginner/90957/
Ok this is what I have..its still wrong but I feel closer.. this assignment is unique as i must use char and if / else statements.

Thanks for the help

#include <iostream>
using namespace std;

int sum (int iValue1, int iValue2) {
int answer = iValue1 + iValue2;
return answer;
}

int subtraction (int iValue1, int iValue2) {
int answer = iValue1 - iValue2;
return answer;
}

int multiply (int iValue1, int iValue2) {
int answer = iValue1 * iValue2;
return answer;
}

int division (int iValue1, int iValue2) {
int answer = iValue1 / iValue2;
return answer;
}

int main () {
int iValue1;
int iValue2;
char chOperator

cout << "Enter an integer:";
cin >> iValue1;
cout << "Enter another integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == +);
cout << "The result is " << sum << endl;
else if (chOperator == -);
cout << "The result is " << subtraction << endl;
else if (chOperator == *);
cout << "The result is " << multiply << endl;
else if (chOperator == /);
cout << "The result is " << division << endl;

return 0;
}
if (chOperator == +); You are forgetting single quotes:
if (chOperator == '+'); (for all operators of course).

But most importantly, you call your functions without parameters:
cout << "The result is " << sum << endl; instead of:
cout << "The result is " << sum(iValue1, iValue2) << endl;
Last edited on
You also don't need to assign an value to a variable before returning, you can return the operation:

1
2
3
4
5
int func()
{
   int a, b;
   return a+ b;
};


You'll want to check in your division function that the denominator is not zero.
Last edited on
Hello everyone,

I edited it based on what you all said.. its not working but I feel closer.

Thanks for the help:

#include <iostream>
using namespace std;

int sum () {
int iValue1, iValue2;
return iValue1 + iValue2;
}

int subtraction () {
int iValue1, iValue2;
return iValue1 - iValue2;
}

int multiply () {
int iValue1, iValue2;
return iValue1 * iValue2;
}

int division () {
int iValue1, iValue2;
return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator

cout << "Enter an integer:";
cin >> iValue1;
cout << "Enter another integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+')
cout << "The result is " << sum << endl;
else if (chOperator == '-')
cout << "The result is " << subtraction << endl;
else if (chOperator == '*')
cout << "The result is " << multiply << endl;
else if (chOperator == '/')
cout << "The result is " << division << endl;

return 0;
}
What about that semicolon here: char chOperator? ;P

Also, for practice, you should make the calculator detect mistakes when entering the operation. For example, what if the user accidentally types in the letter "a"?

EDIT: Also take a look at the second part of JockX's post, I think you forgot to add that.
Last edited on
wow that semicolon almost bit me lol

Tweaked it a bit based on JockX but more issues :/

#include <iostream>
using namespace std;

int sum () {
int iValue1, iValue2;
return iValue1 + iValue2;
}

int subtraction () {
int iValue1, iValue2;
return iValue1 - iValue2;
}

int multiply () {
int iValue1, iValue2;
return iValue1 * iValue2;
}

int division () {
int iValue1, iValue2;
return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator;

cout << "Enter an integer:";
cin >> iValue1;
cout << "Enter another integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+')
cout << "The result is " << sum(iValue1, iValue2) << endl;
else if (chOperator == '-')
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
else if (chOperator == '*')
cout << "The result is " << multiply(iValue1, iValue2) << endl;
else if (chOperator == '/')
cout << "The result is " << division(iValue1, iValue2) << endl;

return 0;
}
closed account (3qX21hU5)
Lookup again how functions work you are passing parameters in your function calls but your function definitions dont have any parameters.

1
2
3
4
int sum () {
int iValue1, iValue2;
return iValue1 + iValue2;
}


notice how the sum() function is defined with no parameters.

Now in main you are calling it with parameters in main sum(iValue1, iValue2).

So the sum function has no idea what to do with iValue1 and iValue2.

So try changing it to this

1
2
3
int sum (int value1, int value2) {
return value1 + value2;
}


Now when you call sum(iValue1, iValue2) it will add whatever variables you pass to it (iValue1, and iValue2) together and return the sum.

Also please use codetags when you post code in the forums (Hint: Highlight all your code and then press the <> button off to the right when replying) it just makes it a lot easier for us to read.
Last edited on
Using <> feature from now on. Sorry everyone..

Ok guys.. I went back and read everything. It all makes sense. Wow what an evolution for me from week 1 to week 3 now.

Last part I am stuck with..

I am trying to make the calculator continue to work, based on the last math done, until it reaches 0. But there is an issue. You'll see the function works fine at first, but when the do { kicks in and it starts working on the new value.. it fails.

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

#include <iostream>
using namespace std;

int sum(int iValue1, int iValue2) {
	return iValue1 + iValue2;
}

int subtraction (int iValue1, int iValue2) {
	return iValue1 - iValue2;
}

int multiply (int iValue1, int iValue2) {
	return iValue1 * iValue2;
}

int division (int iValue1, int iValue2) {
	return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator;

cout << "Enter the initial value as an integer:";
	cin >> iValue1;
	do {
	cout << "Enter the next value as an integer:";
cin >> iValue2;
	cout << "Enter the operation you want to perform (-,+,/,*):";
	cin >> chOperator;
	if (chOperator == '+')
	cout << "The result is " << sum(iValue1, iValue2) << endl;
	else if (chOperator == '-')
	cout << "The result is " << subtraction(iValue1, iValue2) << endl;
	else if (chOperator == '*')
	cout << "The result is " << multiply(iValue1, iValue2) << endl;
	else if (chOperator == '/')
	cout << "The result is " << division(iValue1, iValue2) << endl;
	iValue1 = iValue1 << chOperator << iValue2;

	} while (iValue1 !=0);	 
return 0;
}
what is line 41 supposed to do?? Delete it...
What do you mean by "it fails"?
I removed it earlier and the math came out all wrong.

It am trying to get my calculator to do something like this:

Enter the initial value: 5
Enter the next value : 7
Enter the math you want to perform: +
The result is 12
Enter the next value: 3
Enter the math you want to perform: -
The result is 9
Enter the next value : 10
Enter the math you want to perform: +
The result is 19
Enter the next value: 19
Enter the math you want to perform: -
The result is 0

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>
using namespace std;

int sum(int iValue1, int iValue2) {
	return iValue1 + iValue2;
}

int subtraction (int iValue1, int iValue2) {
	return iValue1 - iValue2;
}

int multiply (int iValue1, int iValue2) {
	return iValue1 * iValue2;
}

int division (int iValue1, int iValue2) {
	return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator;

cout << "Enter the initial value as an integer:";
	cin >> iValue1;
	do {
	cout << "Enter the next value as an integer:";
	cin >> iValue2;
	cout << "Enter the operation you want to perform (-,+,/,*):";
	cin >> chOperator;
	if (chOperator == '+')
	cout << "The result is " << sum(iValue1, iValue2) << endl;
	else if (chOperator == '-')
	cout << "The result is " << subtraction(iValue1, iValue2) << endl;
	else if (chOperator == '*')
	cout << "The result is " << multiply(iValue1, iValue2) << endl;
	else if (chOperator == '/')
	cout << "The result is " << division(iValue1, iValue2) << endl;
	} while (iValue1 !=0);	 
return 0;
}
Well in that case, you would have to do something like this:

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
cout << "Enter the initial value as an integer:";
	cin >> iValue1;
	do {

	cout << "Enter the next value as an integer:";
	cin >> iValue2;
	cout << "Enter the operation you want to perform (-,+,/,*):";
	cin >> chOperator;

	if (chOperator == '+'){	
	cout << "The result is " << sum(iValue1, iValue2) << endl;
	iValue1 = sum(iValue1, iValue2);
	}
	else if (chOperator == '-'){
	cout << "The result is " << subtraction(iValue1, iValue2) << endl;
	iValue1 = subtraction(iValue1, iValue2);
	}
	else if (chOperator == '*'){
	cout << "The result is " << multiply(iValue1, iValue2) << endl;
	iValue1 = multiply(iValue1, iValue2);
	}
	else if (chOperator == '/'){
	cout << "The result is " << division(iValue1, iValue2) << endl;
	iValue1 = division(iValue1, iValue2);
	}

} while (iValue1 !=0);	 
return 0;


This "updates" the value of iValue1 every time so that what you wanted was possible.
then you need to change the if else statement, instead of outputting the result, assign it to iValue1 and output iValue1 as result after the if else.

something like
1
2
3
4
5
6
7
8
9
10
11
12
if (chOperator == '+')
  iValue1 = sum(iValue1, iValue2);
else if (chOperator == '-')
  //...
else if (chOperator == '*')
  //...
else if (chOperator == '/')
  //...

cout << "The result is " << iValue1 << endl;

} while (iValue1 !=0);
Ok everyone.. trying to wrap this and make it real nice.

I put parameters to not allow for characters.. but formatting goes out of whack. Try putting a letter as an integer. Need help with getting it to look clean.

Also, I also put parameters so it cannot divide by 0 based on earlier feedback. It says enter a new integer then it crashes!

Thanks!

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
62
63
64

#include <iostream>
using namespace std;

int sum(int iValue1, int iValue2) {
	return iValue1 + iValue2;
}

int subtraction (int iValue1, int iValue2) {
	return iValue1 - iValue2;
}

int multiply (int iValue1, int iValue2) {
	return iValue1 * iValue2;
}

int division (int iValue1, int iValue2) {
	return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator;

cout << "Enter the initial value as an integer:";
	cin >> iValue1;
	do {

	cout << "Enter the next value as an integer:";
	cin >> iValue2;
	if (cin.fail())
		cout << "No integer found. Please use a number" << endl;
		cin.clear(); 
	
	cout << "Enter the operation you want to perform (-,+,/,*):";
	cin >> chOperator;
	
	if (chOperator == '+') {
	cout << "The result is " << sum(iValue1, iValue2) << endl;
	iValue1 = sum(iValue1, iValue2);
	}

	else if (chOperator == '-') {
	cout << "The result is " << subtraction(iValue1, iValue2) << endl;
	iValue1 = subtraction(iValue1, iValue2);
	}

	else if (chOperator == '*') {
	cout << "The result is " << multiply(iValue1, iValue2) << endl;
	iValue1 = multiply(iValue1, iValue2);
	}

	else if (chOperator == '/') {
		if (iValue2 == 0)
			cout << "You cannot divide by 0. Enter another integer ";
	cout << "The result is " << division(iValue1, iValue2) << endl;
	iValue1 = division(iValue1, iValue2);
	}
	
} while (iValue1 !=0);	 
return 0;
}
In line 55 although you are checking the value of iValue2 you are doing nothing to rectify it if indeed it is 0. So it will just proceed to the next line and continue with the division. Better is the following:

1
2
3
4
5
while (iValue2 == 0) 
{
cout << "You cannot divide by 0. Enter another integer ";
cin>>iValue2;
}  

Topic archived. No new replies allowed.