Mini calculator

Write a C++ program that reads two operands (numbers) and a character as an operator between them and prints the result. The problem is that the output is always a sum, no matter what I enter.

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
#include <iostream>
#include<windows.h>

using namespace std;

int main()
{
    int a,b,p,m,as,d,op;
    cout<<"Enter a : ";
    cin>>a;
    cout<<"Enter b : ";
    cin>>b;
    cout<<"Enter the operator (p,m,as,d) : ";
    cin>>op>>p>>m>>a>>d;
    if (op = p)
    {
        cout<<"The sum of a and b is : "<<a+b<<endl;
    }
    else if (op = m)
    {
        cout<<"The difference of a and b is : "<<a-b<<endl;
    }
    else if (op = as)
    {
        cout<<"The product of a and b is : "<<a*b<<endl;
    }
    else if (op = d)
    {
        cout<<"The quotient of a and b is : "<<a/b<<endl;
    }
    else
    {
        cout<<"That is not an operator!"<<endl;
    }
    system("pause");
    return 0;
}
if (op = p) here and the other if statements, should use == instead of
=
.
When I do that, it shows the else cout : This is not an operator.
Last edited on
use char ' ' instead of declaring p,m,as,d,op as int
I believe your idea would be to read only one input from the user considering the operation and if it equaled say "m" then you would print the difference and other operations would follow based on different input. What you are now trying to do in your code somehow is reading five different inputs for the operation. Could you try to understand little more from
http://www.cplusplus.com/doc/tutorial/basic_io/
maybe even check variables and strings etc. again.

What you will want to end up with is read only a, b and op and then compare for example op == "m" given that op is a string.
Last edited on
Then the program doesn't end after cout "enter character" (I mean with using char). I don't know how to create the program. It has to be like this, for example :
INPUT OUTPUT
3 * 7 21
5 - 12 -7
Last edited on
also " cin>>op>>p>>m>>a>>d;"

why would you do that ? just cin>>op; is enough
Cutefriendzoned, how can you use if and else without a variable (p,m,as,d)? Cause when you use symbols for the operators(if you write + or - (op == -)), the program is not running.
cin>>op>>p>>m>>a>>d;
this line is attempting to input five separate integers from the user. But the prompt said, "Enter the operator (p,m,as,d) : "

There are two problems there. First, the user I think should be typing just a single letter for the operator. Thus the variable op should be of type char.
char op;
I don't think the other variables p,m,as,d are required at all. So you will have just this:
1
2
    int a,b;
    char op;

Now there is another problem, a single letter cannot hold the string "as" so I think you might ask the user to enter one of these, "+ - * /" for the operator, or if you want to use letters of the alphabet, try "a s m d" (meaning add, subtract, multiply, divide).

Then the rest of your code will look something like this:
1
2
3
4
5
    if (op == 'a')
    {
        cout<<"The sum of a and b is : "<<a+b<<endl;
    }
    else // etc. 
or if you prefer,
1
2
3
4
5
    if (op == '+')
    {
        cout<<"The sum of a and b is : "<<a+b<<endl;
    }
    else // etc. 

Hope this helps.
Chervil, thanks a lot for the solution - it really helped me!! :D
Topic archived. No new replies allowed.