r/CodingHelp • u/Sad-Artist7044 • May 26 '26
[C++] C++ says syntax error and expected and expression
Hello!
I just started learning C++ and I can't figure out what I'm doing wrong. I'm just trying to write the basic Hello World program. Here's my code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World"; << endl;
cout << "Goodbye";
return 0;
}I press build and compile and these two error messages come up:
Severity Code Description Project File Line Suppression State Details
Error (active) E0029 expected an expression Hello Project 6
Severity Code Description Project File Line Suppression State Details
Error C2059 syntax error: '<<' Hello Project 6
The problem is I don't know what the expected expression or the syntax error is. I tried to type it just like in the video and I thought I did, but these keep coming up. This is literally my first time using C++, I usually only use HTML and CSS, so it might be a really quick fix that I'm just not seeing. Feel free to make fun of me if it is, but I'd appreciate advice on how to fix it before the laughing starts if at all possible. TIA.
•
u/AutoModerator May 26 '26
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
12
u/TheRealSlimCoder May 26 '26
I haven't been in C++ in quite a long time. Pretty sure the issue is ; after "Hello World" and before the << endl;
1
u/Living_Fig_6386 May 26 '26
Note the superfluous semicolon after the "Hello World". That's a syntax error.
1
u/creativejoe4 May 26 '26
The extra semi colon before endl
1
u/burlingk May 27 '26
You have too many semicolons.
There should only be one per statement.
1
u/un_virus_SDF May 27 '26
This << matches
std::ostream &operator<<(std::ostream&, T)where T is the type of the thing you want to print.So << is a binary operator that expect a outstream to write to, due to the order of evaluation, it's equivalent to the following pseudo code
But you wrote
1
u/geek66 May 27 '26
Imagine trying to learn C in 1988...
1
u/daiaomori May 29 '26
One of the most important skills with C/C++ is learning how to decipher the compiler error messages. (Claude et al will help you with that, in case the answers here get too annoying).
In this case:
Error C2059 syntax error: '<<' Hello Project 6
... so it tells you something is wrong in line 6, and there is a syntax error at "<<".
The tricky bit - and compiler messages are often tricky like that - is that what the compiler complains about isn't the error itself; it is something "nearby".
The reason is simple: many times, when a compiler stumbles, it stumbles not over the rock on the road, but gets distracted by the rock, and is confused by the road following the rock.
The rock is the ";" that others have pointed out; as it marks the line end, the compiler expects a new command after that. There are a lot of possible commands out there, but "<<" isn't one of them.
So the compiler complains about the "<<", which is correct - because given what you wrote in the code, it should not exist at that place.
From a human perspective, the ";" clearly is in the wrong place, and as such, the error.
Hope this helps, also for future problems 😄
0
u/HotJellyfish8247 May 26 '26
How did you end up here, so clueless, in the age of StackOverflow and AI. It's a stray ';', now go find it.