trying to make calculator idk whats wrong

I'm trying to make a basic calculator that does addition, subtraction, multiplication, and division. it says two things wrong: line 17-statement cannot resolve address of overload functions and line 26-'else' without a previous 'if' (and says on line 26 same thing as line 17.) someone with more knowledge plz help me. ALSO if i delete the endl; it fixes it but then the program just skips the first "cout" and just goes to "please enter a number"
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>
using namespace std;

int main()
{
    int a;
    int b;
    int sum;
    int c;
    int d;
    int dif;



    cout << "Please type in one of the following of which you wish to use: add, sub, mult, or div \n";

    if("add") endl;
{
            cout << "Please enter a number \n";
            cin >> a;
            cout << "Please enter another number \n";
            cin >> b;
            sum = a + b;
            cout << sum << endl;
}
    else if("sub") endl;
{
            cout << "Please enter a number \n";
            cin >> c;
            cout << "Please enter another number \n";
            cin >> d;
            dif = c - d;
            cout << dif << endl;
}

    return 0;
}
Last edited on
The problem is the semicolon at the end of line 17. It means that lines 17-19 look like this to the compiler:
1
2
3
4
if ("add")
    endl;
{
    cout << "Please enter a number \n";

You have the same problem at line 26.

Your if statements aren't right either. You need to read the user's input into a string and then check the value of the string. So line 17 should be something like:
1
2
3
4
5
6
7
8
string operation;
cin >> operation;
if (operation == "add")
{
    ....
} else if (operation == "sub")
{
    ....

ty so much it worked this is literally my 4th day of learning just started 9th grade web programming class and wanted to do more so I started some tutorials and just went from there but it does work!
Also do u know what to do about the division not doing decimals? for example i did 5/3 and it gave me 1 even tho it was supposed to say 1.6666666666666666666666
It does but the 5 / 3 was just an example I need to know how have decimals for everything but thank you so much for ur help.
Also where would I put "double decimal" in my code?
closed account (3qD3AqkS)
hi here is the code. correct me if i am wrong.
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>
  #include <string>
using namespace std;

int main()
{
    int a;
    int b;
    int sum;
    int c;
    int d;
    int dif;
    string ans1;



    cout << "Please type in one of the following of which you wish to use: add, sub, mult, or div \n";
    cin>>ans1;

   
    if(ans1=="add"){
    cout << endl;
            cout << "Please enter a number \n";
            cin >> a;
            cout << "Please enter another number \n";
            cin >> b;
            sum = a + b;
            cout << sum << endl;
}else{

if(ans1=="sub")
{
    cout << endl;
            cout << "Please enter a number \n";
            cin >> c;
            cout << "Please enter another number \n";
            cin >> d;
            dif = c - d;
            cout << dif << endl;
}
}

    return 0;
}


I had to add string ans1 so the if statements can detect weather or not you want sub or add.

for the rest of your basic calculator it should be easy. just do the same for add and sub.
So basically add include string, and replace operation with ans1?
Ok thanks for all your help!
i did mult a while ago actually. but thanks for all ur help :)
for example i did 5/3 and it gave me 1 even tho it was supposed to say 1.6666666666666666666666

In C++, when you divide two integers, the result is an integer. It rounds towards zero, so 5/3 is 1 and (-5)/3 is -1.

An easy mistake to make is to do this:
1
2
3
int i1=5;
int i2=3;
double d = i1/i2;

You might think that this will result in d=1.666666... but it doesn't. It gives d=1. The reason is that the compiler evaluates the right side of the expression without caring what you will do with it, and as you saw above, integer division results in an integer result. Then the compiler takes that integer result and converts it to a double to assign to d.

To handle this, you just have to cast one of the values to a double. This will force the compiler to promote the other argument to double and do division of doubles:
double d = static_cast<double>(i1)/i2;
It's worth reading and understanding the subtleties of how expressions are evaluated. Read up on precedence and order of evaluation. Precedence tells you where the implicit parentheses go. E.g., that a+b*c is evaluated like a+(b*c). In other words, in what order are the operators executed. Order of evaluation describes the order in which the operands are evaluated. In other words, whether c = a() + b() is like
1
2
3
int op1 = a();
int op2 = b();
int c = op1+op2;
or
1
2
3
int op2 = b();
int op1 = a();
int c = op1+op2;

In general, the order of evaluation is undefined - the compiler is free to do it however it wants. So you have to write code that won't care. Precedence on the other hand is generally very well defined.
Last edited on
Topic archived. No new replies allowed.