What is wrong with my C++ code???

Comment below rating threshold, click here to show it.

Brosama

This user has referred a friend to League of Legends, click for more information

Senior Member

02-01-2013

#include <iostream>
#include <string>
using namespace std;
int main();
{
cout<<"HI"<<endl;

return 0;
}

Visual Studios is saying there is errors with this


Comment below rating threshold, click here to show it.

I Know Better

Member

02-01-2013

Look at this symbol: ; This is the most fantastic punctuation mark ever, it's called a semicolon; when programming, it is an instruction terminator. When you type this:

#include <iostream>
#include <string>
using namespace std;
int main();
{
cout<<"HI"<<endl;

return 0;
}


You're using the semicolon to finish the main function before it has even done its stuff; typing this: "int main();" is the same as "int main() {}", it is an empty function. The problem is that this part:

{
cout<<"HI"<<endl;

return 0;
}


Makes no sense, because it doesn't actually belong to the main function, the main function already ended with the terminator. You need to remove the semicolon in order to make sure that this block actually is part of the main function and not a senseless block belonging to nowhere. This is how you make it work:

#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<"HI"<<endl;

return 0;
}


But then, you can always make it better; cleaner actually.


Comment below rating threshold, click here to show it.

JacobianMatrix

Senior Member

02-01-2013

Quote:
Originally Posted by Brosama View Post
#include <iostream>
#include <string>
using namespace std;
int main();
{
cout<<"HI"<<endl;

return 0;
}

Visual Studios is saying there is errors with this
Quote:
Originally Posted by Brosama View Post
int main();
There's your problem. Get rid of the semicolon.


Comment below rating threshold, click here to show it.

lukertin

Member

02-01-2013

Quote:
Originally Posted by I Know Better View Post
Look at this symbol: ; This is the most fantastic punctuation mark ever, it's called a semicolon; when programming, it is an instruction terminator. When you type this:

#include <iostream>
#include <string>
using namespace std;
int main();
{
cout<<"HI"<<endl;

return 0;
}


You're using the semicolon to finish the main function before it has even done its stuff; typing this: "int main();" is the same as "int main() {}", it is an empty function. The problem is that this part:

{
cout<<"HI"<<endl;

return 0;
}


Makes no sense, because it doesn't actually belong to the main function, the main function already ended with the terminator. You need to remove the semicolon in order to make sure that this block actually is part of the main function and not a senseless block belonging to nowhere. This is how you make it work:

#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<"HI"<<endl;

return 0;
}


But then, you can always make it better; cleaner actually.
wowowow you are sooooo smart!!!!11


Comment below rating threshold, click here to show it.

Mere Intricacy

This user has referred a friend to League of Legends, click for more information

Senior Member

02-01-2013

Quote:
Originally Posted by lukertin View Post
wowowow you are sooooo smart!!!!11
That awkward moment hen Dobs isn't being sarcastic.


Comment below rating threshold, click here to show it.

lukertin

Member

02-01-2013

It's been like...5 years since I wrote code, I think I stared at OP for about 30 min today trying to find what was wrong, considered making a troll post telling him to add *.h to the end of his #include and then decided not to.

don't tase me sis with your judgement


Comment below rating threshold, click here to show it.

Aliath

Senior Member

02-01-2013

lol


Comment below rating threshold, click here to show it.

vcadoda

This user has referred a friend to League of Legends, click for more information

Senior Member

02-01-2013

Quote:
Originally Posted by lukertin View Post
It's been like...5 years since I wrote code, I think I stared at OP for about 30 min today trying to find what was wrong, considered making a troll post telling him to add *.h to the end of his #include and then decided not to.

don't tase me sis with your judgement
I dont see anything wrong with adding .h, just means you using c libraries now


Comment below rating threshold, click here to show it.

Umbasa

Senior Member

02-01-2013

semicolon on main function LOL. we all got trolled

you win this time op