Error

I am trying to make a program that can do 3 different function(info, calculator, and timer). Info and timer seem to be working fine but calculator is doing the wrong operation. Why is this and how can i fix it? Program is too long so here is the calculator bit. 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
 newLine();
                cout << "Enter first number." << endl;
                cin >> a;
                cout << "Enter a sign: +, -, *, or /." << endl;
                cin >> sign;
                cout << "Enter second number." << endl;
                cin >> b;
                if(sign == '+')
                {
                    total = a + b;
                    cout << "The total of " << a << " " << sign << " " << b << " is: " << total;
                    Sleep(4000);
                }
                else if(sign == '-')
                {
                    total = a - b;
                    cout << "The total of " << a << " " << sign << " " << b << " is: " << total;
                    Sleep(4000);
                }
                else if(sign == '*')
                {
                    total = a + b;
                    cout << "The total of " << a << " " << sign << " " << b << " is: " << total;
                    Sleep(4000);
                }
                else if(sign == '/')
                {
                    if(a,b = 0)
                    {
                        center();
                        cout << "ERROR 4: TOTAL = INFINITY" << endl;
                        Sleep(3000);
                    }
                    if(a,b != 0)
                    {
                        total = a / b;
                        cout << "The total of " << a << " " << sign << " " << b << "is:" << total;
                        Sleep(4000);
                    }
                }
                else
                {
                    center();
                    cout << "ERROR 3: INVALID CALCULATOR SIGN" << endl;
                    Sleep(3000);
                }
            }
What is the type of sign? For this code to work it needs to be a char.
Look at this snippet:

if(a,b = 0)
This is not doing what you seem to think. You probably don't need the 'a' and the comma. Also if 'a' and 'b' are integral variables, remember there are no fractions.

sign is a char. a and b are float variables. I will try and change the a,b = 0 and leave another reply.
Also remember there is a difference in C/C++ between the comparison operator== and the assignment operator=, make sure you use the correct one.

Line 22 is wrong. You are adding when you mean to multiply.

Regarding a/b, if a and b are both equal to zero then the result is undefined. Otherwise if b is zero then the result is +/- infinity, depending on the sign of a. Otherwise the result is a/b.
Topic archived. No new replies allowed.