Pascal refuses to read a string variable... 
Author Message
 Pascal refuses to read a string variable...

I have recently installed Pascal 5.5 and pascal 7 on my system.

My problem is that both version refuse to do a read of a string
variable,
example :

var
  Number1,
  Number2  : integer;

  aString  : string[10];

begin
  ClrScr;

  WriteLn('Enter number 1 :');
  WriteLn('Enter number 2 :');
  WriteLn('Enter a word   :');

  GoToXY(17, 1);
  Read(Number1);

  GoToXY(17, 2);
  Read(Number2);

  GoToXY(17, 3);
  Read(aString);       { Pascal simply walks over this line of code,
                         without performing a read, leaving aString = ''

                         Anyone has an idea ?? }

  GoToXY(1, 10);
  Write('Finished...');

end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Pascal refuses to read a string variable...


Quote:
>I have recently installed pascal 5.5 and pascal 7 on my system.

>My problem is that both version refuse to do a read of a string
>variable,
>example :

>var
>  Number1,
>  Number2  : integer;

>  aString  : string[10];

>begin
>  ClrScr;

>  WriteLn('Enter number 1 :');
>  WriteLn('Enter number 2 :');
>  WriteLn('Enter a word   :');

>  GoToXY(17, 1);
>  Read(Number1);

>  GoToXY(17, 2);
>  Read(Number2);

>  GoToXY(17, 3);
>  Read(aString);       { Pascal simply walks over this line of code,
>                         without performing a read, leaving aString = ''

It's reading the carriage return you used after the second number - thus
you get an empty string. Change the Read to ReadLn and try it again.
--
Information on Newsgroup posted weekly on Sunday - read before writing!
Contains links to    |  http://homepages.force9.net/pascal/faq/
helpful information  |  http://www.merlyn.demon.co.uk/clpb-faq.txt
and some guidelines  |  ftp://garbo.uwasa.fi/pc/doc-net/faqclpb.zip


Wed, 18 Jun 1902 08:00:00 GMT  
 Pascal refuses to read a string variable...

Quote:

>   GoToXY(17, 1);
>   Read(Number1);

Use Readln instead

Quote:
>   GoToXY(17, 2);
>   Read(Number2);

Use Readln here, too.


Wed, 18 Jun 1902 08:00:00 GMT  
 Pascal refuses to read a string variable...
Never encountered anything like that, but you can try to drop the length
restriction on the string, or try readln instead of read. If that doesn't
help, there's a more creative solution - create your own read procedure,
using readkey and the ability to access the specified chars in a string (I'd
be happy to explain more)


Quote:
> I have recently installed pascal 5.5 and pascal 7 on my system.

> My problem is that both version refuse to do a read of a string
> variable,
> example :

> var
>   Number1,
>   Number2  : integer;

>   aString  : string[10];

> begin
>   ClrScr;

>   WriteLn('Enter number 1 :');
>   WriteLn('Enter number 2 :');
>   WriteLn('Enter a word   :');

>   GoToXY(17, 1);
>   Read(Number1);

>   GoToXY(17, 2);
>   Read(Number2);

>   GoToXY(17, 3);
>   Read(aString);       { Pascal simply walks over this line of code,
>                          without performing a read, leaving aString = ''

>                          Anyone has an idea ?? }

>   GoToXY(1, 10);
>   Write('Finished...');

> end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Pascal refuses to read a string variable...
Just change the second read statement to a readln and it should work.
Change read(Number2) to readln(Number2).



Quote:
>Never encountered anything like that, but you can try to drop the length
>restriction on the string, or try readln instead of read. If that doesn't
>help, there's a more creative solution - create your own read procedure,
>using readkey and the ability to access the specified chars in a string (I'd
>be happy to explain more)



>> I have recently installed pascal 5.5 and pascal 7 on my system.

>> My problem is that both version refuse to do a read of a string
>> variable,
>> example :

>> var
>>   Number1,
>>   Number2  : integer;

>>   aString  : string[10];

>> begin
>>   ClrScr;

>>   WriteLn('Enter number 1 :');
>>   WriteLn('Enter number 2 :');
>>   WriteLn('Enter a word   :');

>>   GoToXY(17, 1);
>>   Read(Number1);

>>   GoToXY(17, 2);
>>   Read(Number2);

>>   GoToXY(17, 3);
>>   Read(aString);       { Pascal simply walks over this line of code,
>>                          without performing a read, leaving aString = ''

>>                          Anyone has an idea ?? }

>>   GoToXY(1, 10);
>>   Write('Finished...');

>> end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Pascal refuses to read a string variable...

Quote:

>Just change the second read statement to a readln and it should work.
>Change read(Number2) to readln(Number2).

Correct. The Pascal read is buffered. Once the code encounters a read it
asks user a line. Then it reads from that line the variable that are
specified in the parameter. If one reads a number then spaces and line
changes are skipped until a significant character is found. Then the
number is read and possible error message given if it is not proper.
Reading a character always produces one character, #13 and then #10 in
case of line change. However, strings can be variable length, even
empty, so reading a string ends when a line change is encountered or
when the string is full.

For most complete control of input one should read only strings and then
parse that with for example val(). If one, however, reads numbers
directly, using use readln, or read, followed by checking seekeoln() to
see that there are no other characters on the line and then empty
readln. If one reads numbers directly one has to turn IO checking off
{$I-} and then check with IOResult if the input is proper.

Osmo

Quote:



>>Never encountered anything like that, but you can try to drop the length
>>restriction on the string, or try readln instead of read. If that doesn't
>>help, there's a more creative solution - create your own read procedure,
>>using readkey and the ability to access the specified chars in a string (I'd
>>be happy to explain more)



>>> I have recently installed pascal 5.5 and pascal 7 on my system.

>>> My problem is that both version refuse to do a read of a string
>>> variable,
>>> example :

>>> var
>>>   Number1,
>>>   Number2  : integer;

>>>   aString  : string[10];

>>> begin
>>>   ClrScr;

>>>   WriteLn('Enter number 1 :');
>>>   WriteLn('Enter number 2 :');
>>>   WriteLn('Enter a word   :');

>>>   GoToXY(17, 1);
>>>   Read(Number1);

>>>   GoToXY(17, 2);
>>>   Read(Number2);

>>>   GoToXY(17, 3);
>>>   Read(aString);       { Pascal simply walks over this line of code,
>>>                          without performing a read, leaving aString = ''

>>>                          Anyone has an idea ?? }

>>>   GoToXY(1, 10);
>>>   Write('Finished...');

>>> end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Pascal refuses to read a string variable...


Quote:
> Pascal simply walks over this line of code,
> without performing a read, leaving aString = ''

> Anyone has an idea ??

When you input something from the keyboard with "read", remember that it
will take the data but leave the CR/LF{*filter*} around in the buffer
(which messes up your next "read"). A good rule is not to use "read" for
keyboard input unless you're prepared to process the whole line
yourself, including the CR/LF. Use "readln" instead (which consumes the
CR/LF automatically).

--

http://www.*-*-*.com/



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

 Relevant Pages 

1. Reading different size variables from same open file in Turbo Pascal 3.0

2. Variable length strings to DLLs

3. how to use strings/arrays of variable length?

4. Loading 12000 string variables in memory.

5. Passing string from DBF field to a variable (Halcyon 4.0)

6. string variables

7. How Do I copy one field value to a string variable

8. Read-only variables

9. How can I read text files into variables?

10. Read variables from text file?

11. Help variable reads t

12. Help variable reads too far THANX to anyone who tries to help

 

 
Powered by phpBB® Forum Software