trying to figure out what's wrong

#include<iostream>

using namespace std;

int main()
{
int Va;
int Vb;
int A;
int x;

if(x = "+");
{
A = Va + Vb;
}
else if(x = "-");
{
A = Va - Vb;
}
else if(x = "/");
{
A = Va / Vb;
}
else if(x = "*");
{
A = Va * Vb;
}
else if();
{
A = cout << "WRONG!!!";
}

cout << "MATH \n\n" << "enter varible 1: " ;
cin >> Va;
cout << "good.\n\n" << "now enter varible 2: " ;
cin >> Vb;
cout << "enter opperation +, /, -, *: " ;
cin >> x;
cout << "\n\n" << A;

}
{
cin.get
}

_________

I'm a newbie and this is probably a simple fix. But hopeful help will help me advance. So anyways here it is. I'm getting a bunch of errors and now I'm lost as to what I need to fix.
[code][/code] tags please! Also, you stole my old alias :(

As far as code goes, you're not using the equality operator '==', but instead the assignment operator '=' to check conditions, so right now al of your if statements are true. Remember, programs execute chronologically, so get your input before you check it! Also, a switch statement would probably be better here.
Last edited on
Thank you. I didn't do the switch statement, cause I don't know how to phrase it. But I almost got it working. Right now it will run but it will only multiply the last two inputted variables no matter what I set it two

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

using namespace std;

int main()
{
    int Va;
    int Vb;
    int A;
    char x;
    do{
    cout << "MATH \n\n" << "enter varible 1: " ;
    cin >> Va;
    cout << "good.\n\n" << "now enter varible 2: " ;
    cin >> Vb;
    cout << "enter opperation +, /, -, *: " ;
    cin >> x;
    cout << "\n\n" << A << "\n\n";
    
    if(x == '+');
    {
         A = Va + Vb;
    }
    if(x == '-');
    {
         A = Va - Vb;
    }
    if(x == '/');
    {
         A = Va / Vb;
    }
    if(x == '*');
    {
         A = Va * Vb;
    }
    
    
    } while(A != 0);
    



return 0; 
  
}
You might want to move your printing of A (the answer) until after it has been calculated.
that helped but it still will only multiply
if(x = "+");
This has two problems:
1) This will assign "+" to the variable x if(x == '+');
This fixes it

2) If you use "+" instead of '+' (single quotes are correct; double quotes are not), then it will try to check a 1-byte char against a two-byte + followed by a \0 (null or zero bit) ending.

There are other assignment errors in the rest, but pointing this out, you should be able to see where those mistakes occurred.

edit: beaten.

if(x == '-');
You have a semi-colon at the end of your if statement. Get rid of it and test.
Last edited on
Thanks that did it. It finally works.
Topic archived. No new replies allowed.