Hey guys... I keep getting an error(Error 26: Type Mismatch) in line 19(Until num='';) and I don't know how to fix it... Can you guys please assist me.....Thanks....

program sequence;

uses wincrt;

var  num,count_zero,count_non_zero:integer;

Begin

     count_non_zero:=0;
     Repeat
     writeln('Enter a number');
     readln(num);
     IF num=0 then
        count_zero:=count_zero+1
     Else
         count_non_zero:=count_non_zero+1;
     writeln('Non Zero Count: ',count_non_zero);
     writeln('Zero count: ',count_zero);
     Until num='';
End.

Dani AI

Generated

The compiler error comes from a type mismatch: the loop termination test compares an integer variable with an empty string, and Pascal does not allow comparing different types. Also note that count_zero was never initialized before being incremented, which yields an unpredictable value at runtime.

Both common fixes were suggested in the thread: recommended using a numeric sentinel (a special integer value to mean “stop”), and suggested detecting an empty input line. The numeric-sentinel approach is simple but requires choosing a value that will never be a legitimate input. The empty-line approach is safer for interactive input because it lets the user just press Enter to finish and allows proper validation of each entry.

A robust pattern is to read each line into a string, check for a blank line, then convert the string to an integer and validate the conversion before updating counters. Initialize both counters before the loop. Example (Free/Turbo Pascal style):

var
  line: string;
  num, code, count_zero, count_non_zero: integer;
begin
  count_zero := 0;
  count_non_zero := 0;
  repeat
    writeln('Enter a number (blank to stop):');
    readln(line);
    if line = '' then
      break;
    Val(line, num, code);        { code = 0 on success }
    if code <> 0 then
    begin
      writeln('Invalid integer — try again');
      continue;
    end;
    if num = 0 then
      count_zero := count_zero + 1
    else
      count_non_zero := count_non_zero + 1;
    writeln('Non Zero Count: ', count_non_zero);
    writeln('Zero count: ', count_zero);
  until False;
end.

Notes: always initialize variables, validate conversions (Val’s third parameter), and never compare values of different types. Reading into a string first avoids runtime input errors when the user just presses Enter.

Recommended Answers

All 2 Replies

You are comparing a number with a string. Try Until num=-1 or something like that.

I think that he wants when the line is empty and he press enter to go out of cycle so it will be better to be

Until num=#13;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.