When you forget a semicolon in C++, the compiler will generate an error because it can't figure out where one statement ends and the next begins. The error messages can sometimes be confusing though!
Let's look at an example:
#include <iostream>
using namespace std;
int main() {
// missing semicolon!
cout << "First line\n"
cout << "Second line\n";
}
error: expected ';' before 'cout'
The compiler tries to help by pointing to where it got confused, but sometimes the error appears on the line after the missing semicolon. This is because C++ doesn't know a statement is supposed to end until it hits either a semicolon or something that can't possibly be part of the current statement.
To make your code easier to debug:
Remember: In C++, semicolons are statement terminators, not line terminators. This means you can actually write multiple statements on one line (though it's not recommended for readability):
#include <iostream>
using namespace std;
int main() {
cout << "One"; cout << "Two"; cout << "Three";
}
OneTwoThree
Answers to questions are automatically generated and may not have been reviewed.
Getting our computer set up so we can create and build C++ programs. Then, creating our very first application