Cameron Rogers Cameron Rogers - 4 months ago 14
Pascal Question

Code not moving past Read

I'm having a problem with my code. It starts fine but when asking to get input from the user at the start it will not move onto the if statement. How can I fix this. I've tried heaps to get around this and eventually got it to read data but continually say that it is invalid.

PROGRAM AT3 (input, output);
uses crt, math;
CONST
band6 = 90;
band5 = 80;
band4 = 70;
band3 = 60;
band2 = 50;
VAR
Studname : array of string;
studmark : array of integer;
flag : boolean;
studinfo : text;
input : string;
count : integer;
num : integer;
input2: integer;
highmark, lowmark : integer;
median, average : integer;

BEGIN
lowmark := 100;
highmark := 0;
median := 0;
ASSIGN (Studinfo, 'ExamResults.txt');
flag := false;
WRITELN('welcome to the Band generator.');
WRITELN('To enter student results, please enter the number of students. To see class statistics, please type zzz. To clear the screen type clear screen. And to exit the program, type exit');
While flag = false DO
ReadLN (input);
IF input = 'zzz' THEN
WHILE not EOF(Studinfo) DO
BEGIN
WRITELN(studinfo);
END;
IF input = 'exit' THEN
Flag := true;
IF input = 'clear screen' THEN
CLRSCR
ELSE
if input2 <> 0 THEN
num := input2
ELSE
WRITELN('Please enter a valid number.');
FOR count := 0 to num-1 DO
BEGIN
WRITELN('Please enter name of student', count);
read(studname[count]);
WRITE(studinfo, studname[count]);
WRITELN('Please enter mark of student', count, 'out of 100 (nearest whole number)');
read(studmark[count]); write(studinfo, studmark[count]);
IF studmark[count] >=band6 THEN
WRITELN(studinfo, 'band6');
IF studmark[count] >=band5 THEN
WRITELN(studinfo, 'band5');
IF studmark[count] >=band4 THEN
WRITELN(studinfo, 'band4');
IF studmark[count] >=band3 THEN
WRITELN(studinfo, 'band3');
IF studmark[count] >=band2 THEN
WRITELN(studinfo, 'band2');
IF studmark[count] <band2 THEN
WRITELN(studinfo, 'band1');
IF studmark[count] >= highmark THEN
highmark := studmark[count];
IF studmark[count] <= lowmark THEN
lowmark := studmark[count];
END;
median := highmark MOD 2;

CLOSE(studinfo);
END.

Answer

Questions like this often attract a comment like "SO is not a homework-doing service." This q is a bit exceptional because there is no polite way I can think of to describe it. It has all the signs of having been written by someone who doesn't really know what they are doing - but don't despair, we were all beginners once!

I'm not going to rewrite all your code for you - because you won't learn anything from that - but the following should at least give you a functional main loop which you can then complete yourself and embellish as necessary.

The main thing I've fixed is the sequence of actions in the loop - the original was a complete muddle, to put it mildly. I've added various comments enclosed in braces {}. There is a more recent style of comments that uses // but I don't know what flavour of Pascal you're using.

Code:

const
    band6 = 90;
    band5 = 80;
    band4 = 70;
    band3 = 60;
    band2 = 50;
    ArraySize = 20;  { to set explicit array sizes }
var
    studname : array[1..ArraySize] of string;
    studmark : array[1..ArraySize] of integer;
    flag : boolean;
    studinfo : text;
    kbdinput : string; { to avoid name clash with program Input param}
    count : integer;
    num : integer;
    {input2: integer; not used}
    highmark,
    lowmark : integer;
    median, average : integer;

begin
    lowmark := 100;
    highmark := 0;
    median := 0;
    assign (studinfo, 'c:\temp\examresults.txt'); { always use full path for file name }
    Rewrite(studinfo);  { set studinfo in the state to allow writing to the file}

    flag := false;
    writeln('welcome to the band generator.');
        writeln('to enter student results, please enter the number of students when prompted.');
    while flag = false do
    begin
        write('please enter a valid number of students. ');
        readln(num);
        for count := 1 to num do { not change of start and stop values}
        begin
          write('please enter name of student #', count, ' followed by [Enter] ');
          readln(studname[count]);
          write(studinfo, studname[count]);

          write('please enter mark of student #', count, ' out of 100 (nearest whole number) followed by [Enter] ');
          readln(studmark[count]);
          write(studinfo, studmark[count]);
        end;
{
            if studmark[count] >=band6 then
                writeln(studinfo, 'band6');
            if studmark[count] >=band5 then
                writeln(studinfo, 'band5');
            if studmark[count] >=band4 then
                writeln(studinfo, 'band4');
            if studmark[count] >=band3 then
                writeln(studinfo, 'band3');
            if studmark[count] >=band2 then
                writeln(studinfo, 'band2');
            if studmark[count] <band2 then
                writeln(studinfo, 'band1');
            if studmark[count] >= highmark then
                highmark := studmark[count];
            if studmark[count] <= lowmark then
                lowmark := studmark[count];
            end;
        median := highmark mod 2;
}

      writeln('to see class statistics, please type zzz. to clear the screen type zzz and to exit the program, type exit');
      readln (kbdinput);
      if kbdinput = 'zzz' then
      {  The following does nothing useful
          while not eof(studinfo) do
          begin
              writeln(studinfo);
          end;
      }
      ;
      if kbdinput = 'exit' then
          flag := true
      else
        if kbdinput = 'clear screen' then
          {clrscr;'}

      close(studinfo);
    end;
end.
Comments