C++ Begginer - errors

#include <iostream>
int main()
{
int a;
int b;
int c;
int d;
cin>>a>>b;
c=a+b ; d=a*b;
if(a%2==0) std::cout<<c<<d;
else cout<<d<<c
}

I got a lot of errors. Can anyone fix it and explain it to me?
Last edited on
closed account (LA48b7Xj)
Write std:: in front of cout and cin and put a ; on the last statement.
Instead of writing std:: all the time, write "using namespace std;" before int main().
also in the second last line you forgot to add a semi-colon
I think you have to write
return 0;
at the end of main()
Also using code tags will help you for your future posts
http://www.cplusplus.com/articles/jEywvCM9/
closed account (LA48b7Xj)
I think you have to write
return 0;
at the end of main()

main implicitly returns 0 at the end of the function.
Last edited on
Thank you all.
I fixed it, no compilation errors, but when I open it and type values for a and b a random result appears and the executable stops. What to do?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main()
{
int a;
int b;
int c;
int d;
cin>>a>>b;
c=a+b ; d=a*b;
if(a%2==0) std::cout<<c<<d;
else cout<<d<<c
}


Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;
int main()
{
    int a, b, c, d;
    cin >> a >> b;

    c = a + b; d = a * b;
    if(a % 2 == 0) 
    cout << c << " " << d; else 
    cout << d << " " << c;

    cin.get();
    return 0;
}
Topic archived. No new replies allowed.