Calculator code not working

I'm making a calculator code for a class assignment, but it's not working. For some reason my cin statement inside the if statement isn't working when I test the code, but it does build successfully. Any ideas?

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <iostream>

using namespace std;

int number1;
int number2;
int symbol;
int sum = number1 + number2;
int division = number1 + number2;
int subtraction = number1 + number2;
int multiplication = number1 + number2;
int modulus = number1 + number2;

int main()
{
	

	cout << "Welcome to my calculator\n"
		<< "Please enter a symbol to indicate which type of problem we will be solving\n"
		<< "The choices are +, -, /, *, or % to find the modulus of two numbers\n";
	cin >> symbol;


	if ("+")
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;

		cout << number1 << "plus" << number2 << "is" << sum;
	}
		
		
	else if ("-")
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;


		cout << number1 << "minus" << number2 << "is" << subtraction;
	}

	else if ("/")
	
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;

		cout << number1 << "divided by" << number2 << "is" << division;
	}


	else if ("*")
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;


		cout << number1 << "times" << number2 << "is" << multiplication;
	}

	else if ("%")
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;

		cout << number1 << "modulus" << number2 << "is" << modulus;
	}


	else
	{
		cout << "Invalid operator entered\n"
			 << "Please enter a symbol to indicate which type of problem we will be solving\n";

	}

		system("pause");
		return 0;

}
there is a difference between an int, char,string.
you defined symbol in line 7 as int and you intend to compare it will string in the if statements.

Compare int with int, char with char and so on.

A char is represented by single quotes. char character = 'A';.
A string by double quotes. string testString = "xxx";.

To make a comparison, say if(something compares-to another). The result is bool.
Ofcourse, the must be comparable;
if(1) is always true. Same as if('A') and so on.
Last edited on
I'm still confused on how to make the if statement say that if they type +,-,/,*,% then the following statement will happen. Can I not use + in an if statement?
you can but in that case you will not need an int data type. "char" would be helpful.

eg
1
2
3
char op='*';
if(op=='/')
  cout<<"how is this possible?";


Aceix.
Last edited on
lines 8-12: These lines are nonsense. number1 and number2 have not been assigned values. You're going to be adding garbage to garbage.

Line 21: symbol is an int. The runtime is going to expect you to enter a number, not a symbol (char).
1
2
3
4
5
  char symbol;
...
   cin >> symbol;
...
   if (symbol == '+')   // Note the use of single quotes when comparing a char 


lines 24,33,42,52,61: You're testing if a const char * (a pointer to an array of chars) is non-zero. You're not comparing anything.

Line 29: sum was calculated before you read in number1 and number2. As previously mentioned, you're going to get garbage.

Lines 39,48,58,66: ditto regarding subtraction, division, multiplication, and modulus.
Last edited on
Topic archived. No new replies allowed.