Solved

Controlling the user

Posted on 1998-01-28
2
139 Views
Last Modified: 2010-04-16
Hello

I'm trying to do the screens for the user input and I would like to ask this.
-> I want the user to answer a question where the answers are just the numbers (1), (2) or (3). How can I make this so that the user can´t answer with letters or any other caracters but just 1,2 or 3. And when the user gives just an "enter" the cursor passes to the next line, but I want it to stay in the same position.
How can I control this?

Thanks
Alberto Simões
0
Comment
Question by:asimoes
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
tecbuilder earned 200 total points
ID: 1217226
Use the code below to control user input.

var
  ans: string[1];
  i: byte;

begin
Writeln('Enter a 1, 2, or 3');
repeat
  read(ans)
until (ans in ['1', '2', '3', #13]  {#13 is the return key}
if ans in ['1', '2', '3'] then
  begin
    readln();  {This will go to next line.}
    StrVal(ans, i);
    {Do something.}
  end
else  {ans = #13}
  begin
    {By not having a readln() here the cursor doesn't move to next line.}
    {Do something when return key pressed.}
  end;
0
 
LVL 6

Expert Comment

by:tecbuilder
ID: 1217227
Another way to control the user's input is to using the following code.  This is the same code as above with a slightly different way to read what was entered on the keyboard.  It uses ReadKey instead of Read.  I also did some investigating about locating the cursor to a particular position on the screen.  This seems to be a compiler dependant task.  You will have to look this command up for your specific compiler.  For my Borland 7.0 compiler it is Cursor(x,y).

var
  ans: char;
  i: byte;

begin
  Writeln('Enter a 1, 2, or 3');
  repeat
    ans := ReadKey;
  until (ans in ['1', '2', '3', #13] {#13 is the return key}
  if ans in ['1', '2', '3'] then
    begin
      Write(ans); {This will go to next line.}
      StrVal(ans, i);
        {Do something.}
    end
    else {ans = #13}
  begin
    Cursor(x+1,y);
  end;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Positioning and setting color of buttons on a Firemonkey form 12 2,127
Embarcadero Interbase 9 747
interacting with web form 4 142
Secure Boot on W8 & 8.1 8 45
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now