C++ Syntax Errors: Learn by Fixing Common Mistakes
#include
ii) For (a) – (d) below, in each case - make the suggested error(s) - build the program - find a line number and a description in the error messages - find and correct the error and run again to make sure that the error message has gone.
(a) change the third line to int a, result; (b) change the line a = 3; to a = 3 (leave out the semi-colon) (c) change cout to count in the first cout statement
(a) After making the suggested error, the program would look like this:
#include
When building the program, the error message would be:
error: ‘b’ was not declared in this scope
This error message indicates that the variable 'b' is not declared before its usage.
To correct the error, we can either declare the variable 'b' or remove its usage in the 'result' calculation. Let's remove its usage in this case:
#include
Now, when running the program, the error message should be gone.
(b) After making the suggested error, the program would look like this:
#include
When building the program, the error message would be:
error: expected ‘;’ before ‘b’
This error message indicates that a semicolon is missing after the assignment of 'a'.
To correct the error, we need to add a semicolon after the assignment of 'a':
#include
Now, when running the program, the error message should be gone.
(c) After making the suggested error, the program would look like this:
#include
When building the program, the error message would be:
error: ‘count’ was not declared in this scope
This error message indicates that the keyword 'count' is not recognized as a valid identifier.
To correct the error, we need to change 'count' to 'cout':
#include
Now, when running the program, the error message should be gone.
原文地址: https://www.cveoy.top/t/topic/o43d 著作权归作者所有。请勿转载和采集!