Fixing a broken program - Solution

Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )   Date: Jan 31, 2006



The solution can be found at the end of this document, click the following link to jump to the solution: Start of Solution


Background: The following small program was created, but there seems to be some syntactical and logical errors in the program. Your goal is to find and fix these errors so that the program compiles and runs correctly.


What the program should do: The program reads in the users age and then prints what the user entered back out to the screen. After this the program then checks the age to see what message it should print out in addition. The following conditions are used to check for what message should be printed out.

  1. If the users age is between 0 and 10 then print out the message "You are very young."
  2. If the users age is between 11 and 20 then print out the message "You are getting older."
  3. If the users age is greater then 20 then print out the message "You are still getting older."
So for example if we use the following inputs we should get the following output:
InputExpected output
2"You are very young."
10"You are very young."
11"You are getting older."
20"You are getting older."
21"You are still getting older."
50"You are still getting older."



Below you will find the original broken code with syntax and log errors.
Here is a link to the original broken code: BrokenExample1.p

Code

Output from the compiler


Line |
 1:  |(* ==============================================================================
 2:  |*    File: BrokenExample1.p
 3:  |* Purpose: This program has no other use
 4:  |*          but to the correction of basic syntax errors in a Pascal program.
 5:  |*  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 6:  |* Created on: Jan 31, 2006, minor modifications by James Tam on Feb 3, 2006.
 7:  |============================================================================== *)
 8:  |program (input,output);
 9:  |
10:  |(* ========================== MAIN ============================================== *)
11:  |begin
12:  |    var age : integer
13:  |
14:  |    (* Read in the age of the user *)
15:  |    write('Please enter your age : ');
16:  |    readln(age);
17:  |    writeln('You entered: ',age);
18:  |
19:  |
20:  |
21:  |    (* Now print out some extra information *)
22:  |    write('Result: );
23:  |
24:  |    if (age < 10) then
25:  |      writeln('You are very young.')
26:  |    else if (age < 20) then
27:  |      writeln('You are getting older.')
28:  |    else if (age > 20) then
29:  |      writeln('You are still getting older');
30:  |
31:  |end.
32:  |(* ============================================================================== *)

> gpc BrokenExample1.p
BrokenExample1.p:29: unterminated string or character constant
BrokenExample1.p:22: possible real start of unterminated constant
>

Solution

Step 1


Code

Output from the compiler And extra explanation


Line |
 1:  |(* ==============================================================================
 2:  |*    File: BrokenExample1.p
 3:  |* Purpose: This program has no other use
 4:  |*          but to the correction of basic syntax errors in a Pascal program.
 5:  |*  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 6:  |* Created on: Jan 31, 2006
 7:  |============================================================================== *)
 8:  |program (input,output);
 9:  |
10:  |(* ========================== MAIN ============================================== *)
11:  |begin
12:  |    var age : integer
13:  |
14:  |    (* Read in the age of the user *)
15:  |    write('Please enter your age : ');
16:  |    readln(age);
17:  |    writeln('You entered: ',age);
18:  |
19:  |
20:  |
21:  |    (* Now print out some extra information *)
22:  |    write('Result: );
23:  |
24:  |    if (age < 10) then
25:  |      writeln('You are very young.')
26:  |    else if (age < 20) then
27:  |      writeln('You are getting older.')
28:  |    else if (age > 20) then
29:  |      writeln('You are still getting older');
30:  |
31:  |end.
32:  |(* ============================================================================== *)

> gpc BrokenExample1.p
BrokenExample1.p:29: unterminated string or character constant
BrokenExample1.p:22: possible real start of unterminated constant
>


Here it is telling us that we have problems on lines 29 and 22, as indicated by the number that appears after the name of the file on the output screen. When inspecting line 29, we can see that their seems to be no errors on this line. This is a good indication that the error came from an earlier line and that the compiler is just confused due to the earlier error. The second error message says that on line 22 we have an unterminated constant, this is a good indication of an unclosed string message. A string message should start and end with the same quotation mark. When looking at line 22 we can see that the string in the write command does not end with a single quote, so we should add one in to fix this problem. We can now make this change and recompile the program to see if their are any more syntax errors.


Step 2


Code

Output from the compiler And extra explanation


Line |
 1:  |(* ==============================================================================
 2:  |*    File: BrokenExample1.p
 3:  |* Purpose: This program has no other use
 4:  |*          but to the correction of basic syntax errors in a Pascal program.
 5:  |*  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 6:  |* Created on: Jan 31, 2006, minor modifications by James Tam on Feb 3, 2006.
 7:  |============================================================================== *)
 8:  |program (input,output);
 9:  |
10:  |(* ========================== MAIN ============================================== *)
11:  |begin
12:  |    var age : integer
13:  |
14:  |    (* Read in the age of the user *)
15:  |    write('Please enter your age : ');
16:  |    readln(age);
17:  |    writeln('You entered: ',age);
18:  |
19:  |
20:  |
21:  |    (* Now print out some extra information *)
22:  |    write('Result: ');
23:  |
24:  |    if (age < 10) then
25:  |      writeln('You are very young.')
26:  |    else if (age < 20) then
27:  |      writeln('You are getting older.')
28:  |    else if (age > 20) then
29:  |      writeln('You are still getting older');
30:  |
31:  |end.
32:  |(* ============================================================================== *)

After recompiling with the change made we get the following output from the compiler:

> gpc BrokenExample1.p
BrokenExample1.p:8: parse error before `('
BrokenExample1.p:8: missing program name
BrokenExample1.p:8: parse error before `Input'
>


Here is says we have a parse error on line 8, this indicates that we are missing something required by the syntax in the Pascal language. Line 8 represents the header for the program, which requires follows the general format of: "program (input,output);". In our case we are missing the name of the file the program is in. So we can make this change, and once again recompile the program and see if we have any more errors.


Step 3


Code

Output from the compiler And extra explanation


Line |
 1:  |(* ==============================================================================
 2:  |*    File: BrokenExample1.p
 3:  |* Purpose: This program has no other use
 4:  |*          but to the correction of basic syntax errors in a Pascal program.
 5:  |*  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 6:  |* Created on: Jan 31, 2006, minor modifications by James Tam on Feb 3, 2006.
 7:  |============================================================================== *)
 8:  |program BrokenExample1 (input,output);
 9:  |
10:  |(* ========================== MAIN ============================================== *)
11:  |begin
12:  |    var age : integer 
13:  |
14:  |    (* Read in the age of the user *)
15:  |    write('Please enter your age : ');
16:  |    readln(age);
17:  |    writeln('You entered: ',age);
18:  |
19:  |
20:  |
21:  |    (* Now print out some extra information *)
22:  |    write('Result ');
23:  |
24:  |    if (age < 10) then
25:  |      writeln('You are very young.')
26:  |    else if (age < 20) then
27:  |      writeln('You are getting older.')
28:  |    else if (age > 20) then
29:  |      writeln('You are still getting older');
30:  |
31:  |end.
32:  |(* ============================================================================== *)

> gpc BrokenExample1.p
BrokenExample1.p: In main program:
BrokenExample1.p:15: type name expected, identifier `Integer' given
BrokenExample1.p:15: initial value is of wrong type
BrokenExample1.p:15: warning: missing type in declaration of `Age'
BrokenExample1.p:15: parse error before `Write'
>



Here it says we have an error on line 15, but upon further inspection we can see that there is nothing wrong with this line. The error messages also talk about the terms integer and the variable age. This is again another good indication that we have a syntax error above line 15 that is causes problems. In this case it has to do with the declaration of the variable age. If we look at line 12 we can see that the line does not end with a semicolon, as required. As always we can make this change and recompile once more looking for more errors. (note: this process becomes faster over time as you get familiar with the syntax in Pascal, and do not make these type of mistakes )


Step 4


Code

Output from the compiler And extra explanation


Line |
 1:  |(* ==============================================================================
 2:  |*    File: BrokenExample1.p
 3:  |* Purpose: This program has no other use
 4:  |*          but to the correction of basic syntax errors in a Pascal program.
 5:  |*  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 6:  |* Created on: Jan 31, 2006, minor modifications by James Tam on Feb 3, 2006.
 7:  |============================================================================== *)
 8:  |program BrokenExample1 (input,output);
 9:  |
10:  |(* ========================== MAIN ============================================== *)
11:  |begin
12:  |    var age : integer;
13:  |
14:  |    (* Read in the age of the user *)
15:  |    write('Please enter your age : ');
16:  |    readln(age);
17:  |    writeln('You entered: ',age);
18:  |
19:  |
20:  |
21:  |    (* Now print out some extra information *)
22:  |    write('Result: );
23:  |
24:  |    if (age < 10) then
25:  |      writeln('You are very young.')
26:  |    else if (age < 20) then
27:  |      writeln('You are getting older.')
28:  |    else if (age > 20) then
29:  |      writeln('You are still getting older');
30:  |
31:  |end.
32:  |(* ============================================================================== *)

> gpc BrokenExample1.p
>


Finally we have gotten the program to compile, so we have fixed all the syntax errors. But alas we are not done, we need to now test the program to see if we have any logical errors. To test the program we can run it a multiple of times using the inputs described at the top of this web page , when describing what the program must do. For each input we need to verify if we got the correct output as we expected. Here is the output we get with this version of the code:

Legend:
PASS FAIL

> ./a.out
Please enter your age : 2
You entered: 2
Result: You are very young.



> ./a.out
Please enter your age : 10
You entered: 10
Result: You are getting older.



> ./a.out
Please enter your age : 11
You entered: 11
Result: You are getting older.



> ./a.out
Please enter your age : 20
You entered: 20
Result:



> ./a.out
Please enter your age : 21
You entered: 21
Result: You are still getting older



> ./a.out
Please enter your age : 50
You entered: 50
Result: You are still getting older
>


From the above results we can see that we have two fails. This indicates that we have some form of logic error in the second half of the program.

Step 5


Code

Output from the compiler And extra explanation


Line |
 1:  |(* ==============================================================================
 2:  |*    File: BrokenExample1.p
 3:  |* Purpose: This program has no other use
 4:  |*          but to the correction of basic syntax errors in a Pascal program.
 5:  |*  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 6:  |* Created on: Jan 31, 2006, minor modifications by James Tam on Feb 3, 2006.
 7:  |============================================================================== *)
 8:  |program BrokenExample1 (input,output);
 9:  |
10:  |(* ========================== MAIN ============================================== *)
11:  |begin
12:  |    var age : integer;
13:  |
14:  |    (* Read in the age of the user *)
15:  |    write('Please enter your age : ');
16:  |    readln(age);
17:  |    writeln('You entered: ',age);
18:  |
19:  |
20:  |
21:  |    (* Now print out some extra information *)
22:  |    write('Result: );
23:  |
24:  |    if (age < 10) then
25:  |      writeln('You are very young.')
26:  |    else if (age < 20) then
27:  |      writeln('You are getting older.')
28:  |    else if (age > 20) then
29:  |      writeln('You are still getting older');
30:  |
31:  |end.
32:  |(* ============================================================================== *)

In this case of the errors found in the last step we will have to evaluate the second half for the program in more detail. When looking at line 24 we can see that the first condition checks to see if the age of the user is less then 10. The problem with this is the fact that the condition does not include 10 ( the first error we had). To fix this there are two ways to go about this, this first could be to change the number 10 to 11. This would then change the condition to include the number 10. The second would be to change the the less the ( < ) to a less then or equal sign ( <= ). Continuing this theme we can see the same problems in the next two conditional statements on lines 26 and 28. Once these problems have been fix we are finally done. The following steps shows the completed corrections to the program.

Done


Code

Output from the compiler And extra explanation


Line |
 1:  |(* ==============================================================================
 2:  |*    File: WorkingExample1.p
 3:  |* Purpose: Working example for BrokenExample1.p. This program has no other use
 4:  |*          but to the correction of basic syntax errors in a Pascal program.
 5:  |*  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 6:  |* Created on: Jan 31, 2006, minor modifications by James Tam on Feb 3, 2006.
 7:  |============================================================================== *)
 8:  |program WorkingExample1 (input,output);
 9:  |
10:  |(* ========================== MAIN ============================================== *)
11:  |begin
12:  |    var age : integer;
13:  |
14:  |    (* Read in the age of the user *)
15:  |    write('Please enter your age : ');
16:  |    readln(age);
17:  |    writeln('You entered: ',age);
18:  |
19:  |
20:  |
21:  |    (* Now print out some extra information *)
22:  |    write('Result: );
23:  |
24:  |    if (age < 11) then
25:  |      writeln('You are very young.')
26:  |    else if (age < 21) then
27:  |      writeln('You are getting older.')
28:  |    else if (age > 20) then
29:  |      writeln('You are still getting older');
30:  |
31:  |end.
32:  |(* ============================================================================== *)


Here is a link to the fixed version of the code: WorkingExample1.p