what is wrong here?

Can,t figure it out what is wrong with this code:

#include <iostream.h>
int main(){
int a,b,c=10;
a=b=33;
if a>c then b=b+c else b-=c;
cout << a <<' ' << b << ' ' << c << '\n';
return 0;
}

if a>c then b=b+c else b-=c;

in C++ :

1
2
3
4
5
if (a > c) {
    b += c;
} else {
    b -= c;
}

You can skip the {} if you want to.



Also, use " instead of ' in cout:
cout << a << " " << b << " " << c << "\n";



Last edited on
Well, I am still a beginner myself, but I will try to help. First of all, it is usually best to enclose your code in the code tags. It makes it easier to read. I have made a few changes. Take a looko at my code below to see them. I am not sure that this line is legal: a=b=33;. Your "a>c" is not in () like it should be. That was probably your most fatal mistake. Also, I always enclose if/else statements in {} so they read like this if(true){code here}else{other code}. You do not need to say "then" as C++ implies it. I wasn't sure what you were aiming for with b-=c;. I assumed it was a typo and it was fixed in my code below. I have not compiled this code but it should work now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>
int main()
{
int a,b,c=10;
a=33;
b=33;
if (a>c)
{
b=b+c;
} else {
b=c;
}
cout << a <<' ' << b << ' ' << c << '\n';
return 0;
}
a=b=33; is OK, after that a==33 and b==33 b -= c; means b = b-c;
In C++ ' ' is for characters, " " is for strings
Last edited on
.;..
Last edited on
;....
Last edited on
...;
Last edited on
...
Last edited on
Why do you post programming exercises in Slovenian?
If google translates OK, seems that just surfing a bit on this site would solve your problems...
Last edited on
...
...
...
...

......
... ..
.. ..
.. ..
...
..
...
...
..
..

..
....
..
Topic archived. No new replies allowed.