Board index » delphi » Answering a Question by Pressing the Enter Key

Answering a Question by Pressing the Enter Key

Assuming that I have a declaration as follows in my program:
  VAR
       Answer : char;

and that somewhere else in the same program I have the following:
  write("Enter "Y" for Yes, or "N" for No: ');
  readln(Answer);

If I answer to the above question by just pressing the Enter key (without entering anything else first), the program
hangs until I press the Enter key a second time.  When I do that, the next line after readln(Answer) is executed
normally.

Is there a way to avoid having to press the Enter key twice?

Thanks for your replies.

Tiziano Bianchi
tizi...@utdallas.edu

 

Re:Answering a Question by Pressing the Enter Key


Quote
Tiziano Bianchi wrote:

> Assuming that I have a declaration as follows in my program:
>   VAR
>        Answer : char;

> and that somewhere else in the same program I have the following:
>   write("Enter "Y" for Yes, or "N" for No: ');
>   readln(Answer);

> If I answer to the above question by just pressing the Enter key (without entering anything else first), the program
> hangs until I press the Enter key a second time.  When I do that, the next line after readln(Answer) is executed
> normally.

> Is there a way to avoid having to press the Enter key twice?

> Thanks for your replies.

> Tiziano Bianchi
> tizi...@utdallas.edu

You are already almost all the way there.  Since you are only looking
for the 'Y' or 'N' character and not the whole word (your variable _is_
just a char), just use the ReadKey function instead of ReadLn.  To make
the comparison easier, also use UpCase.  ReadKey will pause and wait for
exactly one keypress, at which time it will return with that key's value
(extended keys actually return two 'keypresses'; a #0 followed by the
extended key code).  Here is how you should rewrite it:

  write("Enter "Y" for Yes, or "N" for No: ');
  Answer := UpCase(ReadKey);
  If Answer = 'Y' then
    something
  else
    something else
  end

Of course, it may be more conveniant to use a Case statement.  You
could, for example, add a third option like 'ESC to cancel'.  I have
found that processing more than two options is best done with a Case
statement, but that only two options is really dependant on just what
you are actually doing.  Hope this helps.

Will <lib...@ix.netcom.com>

Re:Answering a Question by Pressing the Enter Key


Quote
In article <4rifvb$...@news.utdallas.edu> Tiziano Bianchi wrote:
>Assuming that I have a declaration as follows in my program:
>  VAR
>       Answer : char;

>and that somewhere else in the same program I have the following:
>  write("Enter "Y" for Yes, or "N" for No: ');
>  readln(Answer);

>If I answer to the above question by just pressing the Enter key (withoutentering anything else first), the program
>hangs until I press the Enter key a second time.  When I do that, the nextline after readln(Answer) is executed
>normally.

>Is there a way to avoid having to press the Enter key twice?

That's cute.  Can't remember ever running into that one before.
But it's nothing I'll ever lose any sleep over, so I probably
will never know the forces at work here.

You might try using the ReadKey function in the CRT Unit.  It has
the advantage of not requiring the user to press return at all. The
only time I ever require a return is when using the whole word "YES"
to verify a particularly dangerous operation..

--
Roger E. Donais
RDon...@gnn.com
http://members.gnn.com/rdonais/index.html
-------------------------------------------
Knowledge is one of the few things that you
can give away and still keep for yourself.

Re:Answering a Question by Pressing the Enter Key


Quote
Will wrote:

> Tiziano Bianchi wrote:

> > Assuming that I have a declaration as follows in my program:
> >   VAR
> >        Answer : char;

> > and that somewhere else in the same program I have the following:
> >   write("Enter "Y" for Yes, or "N" for No: ');
> >   readln(Answer);

> > If I answer to the above question by just pressing the Enter key (without entering anything else first), the program
> > hangs until I press the Enter key a second time.  When I do that, the next line after readln(Answer) is executed
> > normally.

> > Is there a way to avoid having to press the Enter key twice?

> > Thanks for your replies.

> > Tiziano Bianchi
> > tizi...@utdallas.edu

> You are already almost all the way there.  Since you are only looking
> for the 'Y' or 'N' character and not the whole word (your variable _is_
> just a char), just use the ReadKey function instead of ReadLn.  To make
> the comparison easier, also use UpCase.  ReadKey will pause and wait for
> exactly one keypress, at which time it will return with that key's value
> (extended keys actually return two 'keypresses'; a #0 followed by the
> extended key code).  Here is how you should rewrite it:

>   write("Enter "Y" for Yes, or "N" for No: ');
>   Answer := UpCase(ReadKey);
>   If Answer = 'Y' then
>     something
>   else
>     something else
>   end

> Of course, it may be more conveniant to use a Case statement.  You
> could, for example, add a third option like 'ESC to cancel'.  I have
> found that processing more than two options is best done with a Case
> statement, but that only two options is really dependant on just what
> you are actually doing.  Hope this helps.

> Will <lib...@ix.netcom.com>

OR, if you know them, scan codes are easy enough:

Write('Y or N ');
repeat
Answer:= Readkey;
until (Answer =#121) or (Answer=#110) or (Answer =#13);
                        {until answer = 'y' or 'n' or enter}
case Answer of
#13: {handle the enter key}
#121: {handle the y key}
#110: {handle the n key}
end;

Hope that helps. Scan codes are neat-o!
ronn

Re:Answering a Question by Pressing the Enter Key


Quote
Tiziano Bianchi wrote:

> Assuming that I have a declaration as follows in my program:
>   VAR
>        Answer : char;

> and that somewhere else in the same program I have the following:
>   write("Enter "Y" for Yes, or "N" for No: ');
>   readln(Answer);

> If I answer to the above question by just pressing the Enter key (without entering anything else first), the program
> hangs until I press the Enter key a second time.  When I do that, the next line after readln(Answer) is executed
> normally.

> Is there a way to avoid having to press the Enter key twice?

> Thanks for your replies.

> Tiziano Bianchi
> tizi...@utdallas.edu

How about trying this.

[ key.txt < 1K ]
repeat
     c:=readkey;
     c:=upcase(c);
until (c='Y') or (c='N');

Re:Answering a Question by Pressing the Enter Key


Re:Answering a Question by Pressing the Enter Key


Quote
Tiziano Bianchi <tizi...@utdallas.edu> writes:
> Assuming that I have a declaration as follows in my program:
>  VAR
>       Answer : char;

>and that somewhere else in the same program I have the following:
>  write("Enter "Y" for Yes, or "N" for No: ');
>  readln(Answer);

> If I answer to the above question by just pressing the Enter
> key (without entering anything else first), the program
> hangs until I press the Enter key a second time.  When I
> do that, the next line after readln(Answer) is executed
 >normally.

> Is there a way to avoid having to press the Enter key twice?

Simple answer (TP 4 or later)!  convert Answer to a string[1] instead of
Char.  When user responds by hitting the Enter key, the compiler will
return a null string.

Re:Answering a Question by Pressing the Enter Key


Re:Answering a Question by Pressing the Enter Key


Quote
Ronn <ronca...@netvoyage.net> wrote:
>OR, if you know them, scan codes are easy enough:
>Write('Y or N ');
>repeat
>Answer:= Readkey;
>until (Answer =#121) or (Answer=#110) or (Answer =#13);
>                    {until answer = 'y' or 'n' or enter}
>case Answer of
>#13: {handle the enter key}
>#121: {handle the y key}
>#110: {handle the n key}
>end;
>Hope that helps. Scan codes are neat-o!
>ronn

Only problem is as stated previously in the thread, Y for example
generates a different scan code than y does....

Re:Answering a Question by Pressing the Enter Key


Quote
Tiziano Bianchi <tizi...@utdallas.edu> wrote:
>Assuming that I have a declaration as follows in my program:
>  VAR
>       Answer : char;

>and that somewhere else in the same program I have the following:
>  write("Enter "Y" for Yes, or "N" for No: ');
>  readln(Answer);

>If I answer to the above question by just pressing the Enter key (without entering anything else first), the program
>hangs until I press the Enter key a second time.  When I do that, the next line after readln(Answer) is executed
>normally.

>Is there a way to avoid having to press the Enter key twice?

>Thanks for your replies.

>Tiziano Bianchi
>tizi...@utdallas.edu

You can use ReadKey instead of ReadLn:

Uses Crt;

Var
   Answer : Char;
Begin
  .....
  Write('Enter "Y" for Yes or "N" for no : ');
  Repeat
    Answer := Upcase(ReadKey); { Use Upcase for case insensitive input }
  Until (Answer in ['Y','N']);

  If Answer = 'Y' Then
   ....
End.

I hope this solution will help you,

Christiaan Heidema

Re:Answering a Question by Pressing the Enter Key


In article <4rjhgc$...@news-e2b.gnn.com>, R. E. Donais (RDon...@gnn.com) writes:

Quote

>In article <4rifvb$...@news.utdallas.edu> Tiziano Bianchi wrote:

>>Assuming that I have a declaration as follows in my program:
>>  VAR
>>       Answer : char;

>>and that somewhere else in the same program I have the following:
>>  write("Enter "Y" for Yes, or "N" for No: ');
>>  readln(Answer);

>>If I answer to the above question by just pressing the Enter key (withoutentering anything else first), the program
>>hangs until I press the Enter key a second time.  When I do that, the nextline after readln(Answer) is executed
>>normally.

>>Is there a way to avoid having to press the Enter key twice?

>That's cute.  Can't remember ever running into that one before.
>But it's nothing I'll ever lose any sleep over, so I probably
>will never know the forces at work here.

>You might try using the ReadKey function in the CRT Unit.  It has
>the advantage of not requiring the user to press return at all. The
>only time I ever require a return is when using the whole word "YES"
>to verify a particularly dangerous operation..

>--
>Roger E. Donais
>RDon...@gnn.com
>http://members.gnn.com/rdonais/index.html
>-------------------------------------------
>Knowledge is one of the few things that you
>can give away and still keep for yourself.

The following may be of interest in the context of an integer entry. As
you see in this sketch I actually enter a string and then do two checks.

1. For a nul string because only Enter has been pressed.
2. Using the val procedure any string that does not convert to an integer
results in an error.

Program prototype_readln_integer_entry_check;

Uses crt;

Var
  entry_s  : String;
  i, code  : Integer;
  response : Char;

Label again;

Begin
  again :
  ClrScr;
  WriteLn;
  WriteLn ('Type and Enter an Integer.');
  GotoXY (28, 2);
  ReadLn (entry_s);                   { we now perform the following tests }

  If Length (entry_s) = 0 Then        { 1. Test for no entry }
     Begin
     WriteLn;
     WriteLn ('You did not enter anything.');
     WriteLn;
     WriteLn ('Start again Y/N?');
     GotoXY (20, 6);
     ReadLn (response);
     If UpCase (response) = 'Y' Then
        Goto again
     End

  Else
     Begin                            { 2. Test for non integer entry, but }
     Val (entry_s, i, code);          {   first, convert string to integer }
     If code <> 0 Then  { now check for error during conversion to integer }
        Begin
        WriteLn;
        WriteLn ('Your entry is not an integer. Code error at position ', code);
        WriteLn;
        WriteLn ('Start again Y/N?');
        GotoXY (20, 6);
        ReadLn (response);
        If UpCase (response) = 'Y' Then
           Goto again
        End

     Else
        Begin                         { Entry now acceptable }
        WriteLn;
        WriteLn ('Your entry now confirmed as integer. ', i);
        End;
     End;

  WriteLn;
  WriteLn ('Press Enter to Exit.');
  ReadLn;
End.

Hope it helps.

Albert L. Fowler.

Re:Answering a Question by Pressing the Enter Key


In article <4rifvb$...@news.utdallas.edu>,
Tiziano Bianchi  <tizi...@utdallas.edu> wrote:

Quote
>Assuming that I have a declaration as follows in my program:
>  VAR
>       Answer : char;

>and that somewhere else in the same program I have the following:
>  write("Enter "Y" for Yes, or "N" for No: ');
>  readln(Answer);

>If I answer to the above question by just pressing the Enter key (without entering anything else first), the program
>hangs until I press the Enter key a second time.  When I do that, the next line after readln(Answer) is executed
>normally.

>Is there a way to avoid having to press the Enter key twice?

Make 'Answer' as a string.

Osmo

Re:Answering a Question by Pressing the Enter Key


Quote
In article <31DD2638.1...@netvoyage.net>, Ronn  <ronca...@netvoyage.net> wrote:
>Will wrote:

>OR, if you know them, scan codes are easy enough:

>Write('Y or N ');
>repeat
>Answer:= Readkey;
>until (Answer =#121) or (Answer=#110) or (Answer =#13);
>                    {until answer = 'y' or 'n' or enter}
>case Answer of
>#13: {handle the enter key}
>#121: {handle the y key}
>#110: {handle the n key}
>end;

Those are not scan codes. You have just used ASCII codes of 'y' and 'n'.
It would be just the same as if you replaced #121 with 'y' and #110 with
'n'. The only difference is that your code is harder to read. If you
want to make the code hard to read then why not go all the way and use
goto :-)

The readkey does not return scancodes for keys that have an ASCII
code.

Quote
>Hope that helps. Scan codes are neat-o!
>ronn

Osmo

Other Threads