Solved

Controlling the user

Posted on 1998-01-28
2
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog we highlight approaches to managed security as a service.  We also look into ConnectWise’s value in aiding MSPs’ security management and indicate why critical alerting is a necessary integration.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

695 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