When we forgot to add semicolon in c++ program then generally we get error like c++ expected a ‘;’. Whenever you get similar error in c++ then you have to check mention line number that semicolon is added or missing.
1. c++ expected a ‘;’ [Scenario-1]
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello Mr.CodeHunter.com";
return 0
// Need to add ; at the end , return 0;
}
Output:
main.cpp:8:1: error: expected ‘;’ before ‘}’ token } ^
Here if you check that we forgot to add semicolon at return statement.
2. c++ expected a ‘;’ [Scenario-2]
#include <iostream>
using namespace std;
void HelloWorld(){
cout<<"Hello Mr.CodeHunter.com";
}
void Print(){
HelloWorld()
// Need to add ; at the end of function, HelloWorld();
}
int main()
{
Print();
return 0;
}
Output:
main.cpp:9:1: error: expected ‘;’ before ‘}’ token }
Here inside Print() function we are calling HelloWorld() function but we forgot to add semicolon at the end of function. For calling any function we use semicolon and if we forgot then we get error c++ expected a ‘;’.
3. c++ expected a ‘;’ [Scenario-3]
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello Mr.CodeHunter.com"
// Need to add ; at the end of cout
return 0;
}
Output:
main.cpp:7:5: error: expected ‘;’ before ‘return’ return 0; ^~~~~~
Conclusion for c++ expected a ‘;’
If you get any compilation error for c++ expected a ‘;’ then you need to check particular line that semicolon is added or not. Mostly this type of error occurs when we forgot to add semicolon.
Leave a Reply