Board index » delphi » Question about Char and String

Question about Char and String

I wrote the following program to test Char and String:

program test;

var
  temp1 : string[1];
  temp2 : char;

begin
  write('String : ');
  readln(temp1);
  write('Char   : ');
  readln(temp2);
  writeln('String = "', temp1, '"');
  writeln('Char   = "', temp2, '"');
end.

If I enter character, I got the expecct result:
  String : a
  Char   : b
  String = "a"
  Char   = "b"

But when I enter nothing(just press ENTER), I got the following result:
String :
Char   :

String = ""
"har   = "

I have 2 question about this:
1. If I enter nothing to Char, why I should press ENTER twice(an extra line
before the output)
2. Why does the output like this?

Thanks

 

Re:Question about Char and String


Hi,

on Sun, 02 Jan 2000 at 11:08:35 o'clock, Kwok-ho wrote:

Quote
> But when I enter nothing(just press ENTER), I got the following result:
> String :
> Char   :

> String = ""
> "har   = "

When you press [Enter], you do *not* enter nothing into the char variable,
because, unlike a string, a char cannot be empty. Thus, the first time
you press [Enter], this keypress is taken as the character; the second
time it is taken as the completion of the Readln.

When you output the character (#13 = carriage return), the cursor position
is changed to column 1, and the closing '"' is printed over the 'C'.

Readln is not a good way to input characters - use Read instead.

 - Sebastian

--
This posting is sent in compliance with art. 22 of the German basic law.

Re:Question about Char and String


thx :)

"Sebastian Koppehel" <ba...@bastisoft.de> ?????
news:tjuk48.cb.ln@bastisoft.de...

Quote
> Hi,

> on Sun, 02 Jan 2000 at 11:08:35 o'clock, Kwok-ho wrote:

> > But when I enter nothing(just press ENTER), I got the following result:
> > String :
> > Char   :

> > String = ""
> > "har   = "

> When you press [Enter], you do *not* enter nothing into the char variable,
> because, unlike a string, a char cannot be empty. Thus, the first time
> you press [Enter], this keypress is taken as the character; the second
> time it is taken as the completion of the Readln.

> When you output the character (#13 = carriage return), the cursor position
> is changed to column 1, and the closing '"' is printed over the 'C'.

> Readln is not a good way to input characters - use Read instead.

>  - Sebastian

> --
> This posting is sent in compliance with art. 22 of the German basic law.

Re:Question about Char and String


Quote
"Kwok-ho" <kwokh...@yahoo.com> wrote in message

news:84kn85$7mq1@imsp212.netvigator.com...
Quote
> I wrote the following program to test Char and String:
> program test;
> var
>   temp1 : string[1];
>   temp2 : char;
> begin
>   write('String : ');
>   readln(temp1);
>   write('Char   : ');
>   readln(temp2);
>   writeln('String = "', temp1, '"');
>   writeln('Char   = "', temp2, '"');
> end.
> If I enter character, I got the expecct result:
>   String : a
>   Char   : b
>   String = "a"
>   Char   = "b"
> But when I enter nothing(just press ENTER), I got the following
result:
> String :
> Char   :
> String = ""
> "har   = "
> I have 2 question about this:
> 1. If I enter nothing to Char, why I should press ENTER twice(an
extra line
> before the output)
> 2. Why does the output like this?
> Thanks

Hi,
Just got back from holidays, so I'm catching up.
I agree with Sebastian about 'reading' characters with ReadLn(),
however I do not agree with his use of Read() for reading characters
If you want to 'read' 1 single character from the keyboard, you should
use *Character := ReadKey;*
Also when I need user input (in a fancy screen), I will always
reposition the cursor at the position I want the user to perform his
input.
A nice side-effect of ReadKey is that you have (practically) complete
control of what the user is doing.
Hope to have helped a little.
Marcel.

Other Threads