read 
Author Message
 read

Hi,

One of my students wrote the program below: the problem is with the read
statement - when it gets to operator, it returns an invalid numeric format
error message. Is this because when reading a char variable read does not
skip over leading blanks? If so, is there a simple way around this without
resorting to t{*filter*} strings etc.? She undersands that 3 readln statements
would work if the user inputs the 3 values using the enter key after each
value but she seems to want them all to be inputted on the same line.

Robert

program twelve (input, output);
var invalid_operator: boolean;
    operator: char;
    number1, number2,result: real;
Begin
     invalid_operator := false;
     writeln('enter two numbers and an operator in the format');
     writeln('number1 operator number2');
     read(number1, operator, number2);

     case operator of
          '*': result := number1*number2;
          '/': result := number1/number2;
          '+': result := number1+number2;
          '-': result := number1-number2;
          else
              invalid_operator := true
     end;
          if invalid_operator then
             writeln('Invalid operator')
          else
              writeln(number1:4:2,' ', operator, ' ', number2:4:2, 'is',
result:5:2)
End.



Wed, 18 Jun 1902 08:00:00 GMT  
 read
im not sure but operator might be storing the space they put before they enter
the operator (its a char so will only store 1 letter). Why dont you just have
it ask for each piece of data on a seperate line? I think that might help. To
test this just have the operator printed to the screen so you can see what it
is.


Wed, 18 Jun 1902 08:00:00 GMT  
 read

Quote:

>Hi,

>One of my students wrote the program below: the problem is with the read
>statement - when it gets to operator, it returns an invalid numeric format
>error message. Is this because when reading a char variable read does not
>skip over leading blanks? If so, is there a simple way around this without
>resorting to t{*filter*} strings etc.? She undersands that 3 readln statements
>would work if the user inputs the 3 values using the enter key after each
>value but she seems to want them all to be inputted on the same line.

>Robert

The first char the "read" sees after number1 is a space. The following
simple changes will let the program run properly but you may wish to tailor
your ouput format.

Quote:
>program twelve (input, output);
>var invalid_operator: boolean;
>    operator: char;     >>>>>>>>>> CHANGE TO operator:String[2];

................................

Quote:
>     case operator of   >>>>>>>>>>>>>  CHANGE TO case operator[2] of
>          '*': result := number1*number2;
>          '/': result := number1/number2;
>          '+': result := number1+number2;
>          '-': result := number1-number2;

An improved "user friendly" method of inputing multiple variables of
different types on one line is to use a string. Then you parse the string
into its various components and use VAL to change the numeric chars into
integers, real, etc.
amd chars into dimensioned strings. Virtually fool proof error checking can
be done this way.

Regards,



Wed, 18 Jun 1902 08:00:00 GMT  
 read

Quote:

> Hi,

> One of my students wrote the program below: the problem is with the read
> statement - when it gets to operator, it returns an invalid numeric format
> error message. Is this because when reading a char variable read does not
> skip over leading blanks?

Yes.  Think of the "logic" of it.  If you say "read the next number", you mean
(logically)
"keep reading characters until I have assembled a number; return an error if
what's present is NOT a number".  This should skip white space, should accept
integers, and
should "stop" when it encounters a non-numeric character (or space or EOLN).
If, however, you say "read the next character", you MEAN "read the next
character", including a space, if it is present.  Extremely logical (in my
opinion).

Bob Schor
Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 read


Quote:
> Hi,

> One of my students wrote the program below: the problem is with the read
> statement - when it gets to operator, it returns an invalid numeric format
> error message. Is this because when reading a char variable read does not
> skip over leading blanks? If so, is there a simple way around this without
> resorting to t{*filter*} strings etc.? She undersands that 3 readln
statements
> would work if the user inputs the 3 values using the enter key after each
> value but she seems to want them all to be inputted on the same line.

> Robert

> program twelve (input, output);
> var invalid_operator: boolean;
>     operator: char;
>     number1, number2,result: real;
> Begin
>      invalid_operator := false;
>      writeln('enter two numbers and an operator in the format');
>      writeln('number1 operator number2');
>      read(number1, operator, number2);

>      case operator of
>           '*': result := number1*number2;
>           '/': result := number1/number2;
>           '+': result := number1+number2;
>           '-': result := number1-number2;
>           else
>               invalid_operator := true
>      end;
>           if invalid_operator then
>              writeln('Invalid operator')
>           else
>               writeln(number1:4:2,' ', operator, ' ', number2:4:2, 'is',
> result:5:2)
> End.

Well, the concept was defineatly good, but unfortuneatly we can't use read
that way, it wont determine the number from the operators, so you have to
make it do that, i've modifed it a bit to do so, all it does is read's the
input as a string then locates the operator and then converts the chars
before it to a number and then the chars after it into the other number,
there may be better ways of doing this but it will do the trick to your
needs:

program twelve (input, output);
var
    inputstring : string;
    invalid_operator: boolean;
    operator: char;
    number1, number2,result: real;
    return : integer;
    temp : byte;
Begin
    invalid_operator := false;
    operator:=#0;
    writeln('enter two numbers and an operator in the format');
    writeln('number1 operator number2');
    readln(inputstring);

    for temp:=1 to length(inputstring) do
        if inputstring[temp] in ['*','+','-','/'] then
        begin
            operator:=inputstring[temp];
            val(copy(inputstring,1,temp-1),number1,return);
            if return<>0 then operator:=#1;

val(copy(inputstring,temp+1,length(inputstring)),number2,return);
            if return<>0 then operator:=#1;
        end;

    case operator of
         '*': result := number1*number2;
         '/': result := number1/number2;
         '+': result := number1+number2;
         '-': result := number1-number2;
         else
             invalid_operator := true
    end;
         if invalid_operator then
            writeln('Invalid operator and/or number(s)')
         else
             writeln(number1:4:2,' ',operator,' ',number2:4:2,' is
',result:5:2);
 end.

Ryan Jenkins
  -Silly Stuff 1999



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. if you read Help!!!! read this:

2. REQ: READ: A Simple, but realisitic flame program that can work at specific coordinates and sizes --please read

3. Table is read only with TUpdateSql

4. test - do not read

5. Reading synonyms and grants from Oracle via the BDE

6. can't read/write dbf fields with 10 decimals

7. reading Access 2 files

8. mini-FAQ V1.56 - essential reading for those new to the newsgroup

9. mini-FAQ V1.56 - essential reading for those new to the newsgroup

10. how to make multiple read outs - help

11. mini-FAQ V1.55 - essential reading for those new to the newsgroup

12. mini-FAQ V1.55 - essential reading for those new to the newsgroup

 

 
Powered by phpBB® Forum Software