Recent

Author Topic: [SOLVED] Mistreating End of Line in Unix.  (Read 1982 times)

Olimak

  • New member
  • *
  • Posts: 8
[SOLVED] Mistreating End of Line in Unix.
« on: September 13, 2017, 05:35:12 pm »
Hello again. A few days ago I asked for help with a pascal program capable of converting numbers from X base to base 10. I'm sorry if I didn't reply, I got a lot of help and I appreciate it. I was able to pull it off the day after asking for help.
I turned in my program but was told that it was incorrect due to a "possible mistreatment of end of line, a character present in Unix". Windows is my main OS so I guess I can't exactly fix this problem without installing Linux, something I'd rather not do.
Can anyone tell me if this problem is present in my code? And if so, how can I fix it?
Just in case, I'm 100% sure the program is working as intended. We were given a Perl file which gives the program 15 inputs and analyzes the output with an expected output. In my case, all 15 inputs were handled correctly by my program.

Code: Pascal  [Select][+][-]
  1. program test;
  2. const
  3.         maxlargo = 7;
  4.         dot = '.';
  5.         sep = ':';
  6.         sec = '$';
  7. var
  8.         c:char;
  9.         x,i,b,d,base:integer;
  10.         res:longint;
  11. begin
  12.         i := -1;
  13.         (*Reads the input and locks it to be shorter or equal to 7 numbers (maxlargo), and to close the program if the input is '$'.*)
  14.         read(c);
  15.         while (c <> sec) and (i <= maxlargo) do
  16.         begin
  17.                 b := 0;
  18.             base := 0;
  19.                 res := 0;
  20.                 x := 0;
  21.                 d := 0;
  22.                 i := -1;
  23.                         (*Reads until colon to determine base.*)
  24.                         while (c <> sep) do
  25.                         begin
  26.                                 d := ord(c) - ord('0');
  27.                                 b := b+1;
  28.                                 (*Checks if the base has one or two numbers, and reacts accordingly.*)
  29.                                 if b = 2 then
  30.                                         base := base*10 + d
  31.                                 else
  32.                                         base :=  d;
  33.                                 (*Reads the next input for the base.*)
  34.                                 read(c);
  35.                         end;
  36.                         (*Reads until period to determine number to convert.*)
  37.                         while (c <> dot) and (i <= maxlargo) do
  38.                         begin
  39.                                 i:=i + 1;
  40.                                 (*Locks said number to a max amount of characters. If the input is longer than 7, the program returns an ERROR and doesn't convert it at all.*)
  41.                                 if i > maxlargo then
  42.                                 writeln('ERROR')
  43.                                 else
  44.                                 begin
  45.                                         case c of
  46.                                                 '0'..'9' : x := ord(c) - ord('0');
  47.                                         'A'..'F' : x := ord(c) - ord('A') + 10;
  48.                                     end;
  49.                                                 res := (x + (res*base));
  50.                                                 (*Reads the next input for the number.*)
  51.                                                 read(c);
  52.                                 end;
  53.                         end;
  54.                         if i <= maxlargo then
  55.                         writeln(res);
  56.                         (*Restarts the program.*)
  57.                         read(c); read(c); read(c);
  58.         end;
  59. end.
« Last Edit: September 13, 2017, 10:57:59 pm by Olimak »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Mistreating End of Line in Unix.
« Reply #1 on: September 13, 2017, 08:41:49 pm »
Well, I put up some example code that runs perfectly well under Linux.

Just read until Eoln, in the mean time you can skip any input afteryou have read the maximum numberof characters you need.

My example does not have such constraints though.

Bart

Olimak

  • New member
  • *
  • Posts: 8
Re: Mistreating End of Line in Unix.
« Reply #2 on: September 13, 2017, 10:57:30 pm »
I was able to fix it by closing the last if (something I hadn't seen) and replacing two of the read(c); for readln;. This worked in Ubuntu so I can safely say it works.
Thanks!

 

TinyPortal © 2005-2018