My calculator isn't working

->I'm a 12 year old kid that tried to make a calculator in Linux C++. Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>

int main()
{	
	using namespace std;
					
	int a;							
	int b;
	int c;
	int answer;
	
	cout <<" Calculator !!! " << endl;
	cout <<" Made by UdAy ShArMa the GREAT!!! " << endl;
	cout <<" PLease ENTER your number. " << endl;
	cin >> a;
	cout << " PLease ENTER the operation. " << endl;
	cin >> b;
	cout << " PLease ENTER your other number. "<< endl;
	cin >> c;
	answer = a,b,c;
	cout << " Your anSwer is "<< answer <<", OMG that's sooo cool. "<< endl; 
	return 0;
} 


->It compile's and everything but it's out put is messed up. This is the output:

Calculator!!!
Made by UdAy ShArMa the GREAT!!!
Please ENTER your number.
123
PLease Enter your operation.
+
PLease ENTER your other number.
Your anSwer is 123, OMG that's sooo cool

->So like it doesn't let me do my third input???

WWWWWWWHHHHHHHHHYYYYYYYYY!!!!!!!!!!!!
Last edited on
Your second input is the symbol '+'. You are trying to store that symbol in a variable of type int.

The variable type int is for storing numbers. '+' is not a number. cin is trying to store a number in b, and you are breaking things by not putting in a number.

Note also that this
answer = a,b,c;
makes no sense whatsoever.
i changed the entire format (thx to you btw) and this is the code now:

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
#include <iostream>
#include <cmath>

int main()
{	
	using namespace std;
					
	int a;							
	int b;
	int c;
	int addition;
	int subtraction;
	int multiplication;
	int division;
	int answer;
	
	cout <<" Calculator !!! " << endl;
	cout <<" Made by UdAy ShArMa the GREAT!!! " << endl;
	cout <<" PLease ENTER your number. " << endl;
	cin >> a;
	cout << " PLease ENTER the operation(addition, subtraction, multiplication, division). " << endl;
	cin >> b;
	cout << " PLease ENTER your other number. " << endl;
	cin >> c;
	if (b = addition){
	answer = a + b;
	}
	if (b = subtraction){
	answer = a - b;
	}
	if (b = multiplication){
	answer = a * b;
	}
	if (b = division){
	answer = a / b;
	}
	cout << " Your anSwer is "<< answer <<", OMG that's sooo cool. "<< endl; 
	return 0;
}


it's still showing the sames output
Last edited on
Your second input is the symbol '+'. You are still trying to store that symbol in a variable of type int.

The variable type int is for storing numbers. '+' is not a number. cin is still trying to store a number in b, and you are breaking things by not putting in a number.

You have not changed anything. You are still trying to store something that is not a number in a variable that is meant only for storing numbers.

Looking at your code, I think you simply don't understand what a variable is (or possibly, you think there is only one kind of variable; int). You need to go back to the basics of what a variable is.
Last edited on
thanks for the advice so i looked it up in primer plus and i noticed char variables and studied them and so they store character variables so should i use them???
ooohhhhhhhhhh now i get it thank you
ill post up the new code
Ok so now what's 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
#include <iostream>
#include <cmath>

int main()
{	
	using namespace std;
					
	int a;							
	int b;
	int c;
	char addition;
	char subtraction;
	char multiplication;
	char division;
	int answer;
	
	cout <<" Calculator !!! " << endl;
	cout <<" Made by UdAy ShArMa the GREAT!!! " << endl;
	cout <<" PLease ENTER your number. " << endl;
	cin >> a;
	cout << " PLease ENTER the operation(1 addition,2 subtraction,3 multiplication,4 division). " << endl;
	cin >> b;
	cout << " PLease ENTER your other number. " << endl;
	cin >> c;
	if (b = 1){
	answer = a + b;
	}
	if (b = 2){
	answer = a - b;
	}
	if (b = 3){
	answer = a * b;
	}
	if (b = 4){
	answer = a / b;
	}
	cout << " Your anSwer is "<< answer <<", OMG that's sooo cool. "<< endl; 
	return 0;
}
	
	
(b = 1)
This says "set the value of b to one

(b == 1)
This says "test the value of b, to see if the value of b is one."

So now this comes as the output
Calculator !!! 
 Made by UdAy ShArMa the GREAT!!! 
 PLease ENTER your number. 
5
 PLease ENTER the operation(1 addition,2 subtraction,3 multiplication,4 division). 
2
 PLease ENTER your other number. 
3
 Your anSwer is 3, OMG that's sooo cool.
Last edited on
OOOOHHHHHHHHHH Now i see the problem


Thanks a lot....it works. xD
Last edited on
This is the working code( thx to you)
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
#include <iostream>
#include <cmath>

int main()
{	
	using namespace std;
					
	int a;							
	int b;
	int c;
	char addition;
	char subtraction;
	char multiplication;
	char division;
	int answer;
	
	cout <<" Calculator !!! " << endl;
	cout <<" Made by UdAy ShArMa the GREAT!!! " << endl;
	cout <<" PLease ENTER a number. " << endl;
	cin >> a;
	cout << " PLease ENTER the operation: (1) addition,(2) subtraction,(3) multiplication,(4) division. " << endl;
	cin >> b;
	cout << " PLease ENTER another number. " << endl;
	cin >> c;
	if (b == 1){
	answer = a + c;
	}
	if (b == 2){
	answer = a - c;
	}
	if (b == 3){
	answer = a * c;
	}
	if (b == 4){
	answer = a / c;
	}
	cout << " Your anSwer is "<< answer <<", OMG that's sooo cool. "<< endl; 
	return 0;
}
	
	
I really see you getting things fast and do good work.
Here if you have used
switch functionality that would be much better.

You also need to learn to make your program robust,
for example think : what happens if in operation asking user enter the number which is not in the list......

And I am suggesting you switch case, because that is better way to do multiple if else conditions.

I guess you will soon post your new code using switch case and you should provide quit option after every operation, so that if you want to perform another operation you don't have to run program again and again...
Last edited on
whats up with wanting to make calculators? that was like the first thing I made too
OMG, this is sooo cool.
hey guys.im new here too.

i started programming by printing my own name.lol.

but yah i also recomend the switch statement.its bettr.

Last edited on
closed account (N36fSL3A)
I'm confused, what does
1
2
3
4
	char addition;
	char subtraction;
	char multiplication;
	char division;
Do exactly? Are these variables even used? Also, is it necessary to use #include <cmath> ?

Also you can just do this for better readability...
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
#include <iostream>




int main()
{	
	using namespace std;
					
	int number;							
	int number2;
	int choice;
	int answer;
	
	//First Display
	cout <<" Calculator !!! " << endl;
	cout <<" made by uday sharma the great!!! " << endl;
	cout <<" please enter a number. " << endl;
	
	//Enter the information
	cin >> number;
	cout << " please enter the operation: (1) addition,(2) subtraction,(3) multiplication,(4) division. " << endl;
	cin >> choice;
	cout << " please enter another number. " << endl;
	cin >> number2;
	
	switch(choice)
	{
	//Addition
		case 1:
			answer = number + number2;
		break;
	
	//Subtraction
		case 2:
			answer = number - number2;
		break;
		
	//Multiplication
		case 3:
			answer = number * number2;
		break;
		
	//Division
		case 4:
			answer = number / number2;
		break;
		
	//Error
		default:
			cout << "Error: you did not enter a valid character";
		break;
	}
	cout << " your answer is "<< answer <<", omg that's sooo cool. "<< endl; 
	return 0;
}


P.S. Sorry for changing variable names, it was easier for me to read that way.
Last edited on by Fredbill30
Topic archived. No new replies allowed.