Question
2.4 Errors and warnings Syntax errors Picture of a beverage spilled on paper. People make mistakes. Programmers thus make mistakeslots of them. One kind of
2.4 Errors and warnings Syntax errors Picture of a beverage spilled on paper. People make mistakes. Programmers thus make mistakeslots of them. One kind of mistake, known as a syntax error, is to violate a programming language's rules on how symbols can be combined to create a program. An example is forgetting to end a statement with a semicolon. A compiler generates a message when encountering a syntax error. The following program is missing a semicolon after the first output statement. Figure 2.4.1: Compiler reporting a syntax error. 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: #include using namespace std; int main() { cout << "Traffic today" cout << " is very light"; cout << endl; return 0; } main.cpp:6:27: error: expected ';' after expression cout << "Traffic today" ^ ; Feedback? Above, the 6 refers to the 6th line in the code, and the 27 refers to the 27th column in that line. zyDE 2.4.1: Syntax errors often exist before the reported line. Complete the program below by typing the following statement as your solution. Note: the statement is intentionally missing an ending semicolon -- don't add the semicolon yet. cout << "Hello" Press Run, and notice that the error message is for a line that comes AFTER your statement, because that later line is where the compiler got confused. Add the semicolon, and press Run again. Your program should work correctly now. Load default template... Run Feedback? PARTICIPATION ACTIVITY 2.4.1: Syntax errors. Find the syntax errors. Assume variable numDogs has been declared. 1) cout << numDogs. 2) cout << "Dogs: " numDogs; 3) cout < "Everyone wins."; 4) cout << "Hello friends! << endl; 5) cout << "Amy // Michael" << endl; 6) cout << NumDogs << endl; 7) int numCats numCats = 3; cout << numCats << endl; 8) cout >> numDogs >> endl; Feedback? Unclear error messages Compiler error messages are often unclear or even misleading. The message is like the compiler's "best guess" of what is really wrong. Figure 2.4.2: Misleading compiler error message. 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: #include using namespace std; int main() { cout "Traffic today"; cout << " is very light"; cout << endl; return 0; } main.cpp:6:7: error: expected ';' after expression cout "Traffic today"; ^ ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started