Solved

Controlling the user

Posted on 1998-01-28
2
140 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi ADO query as a string 4 2,625
Tstream to hex 1 1,701
Lazarus: saving BLOBs from Sybase database 3 155
delphi custom sort exception 6 162
An article on effective troubleshooting
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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