Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Controlling the user

Posted on 1998-01-28
2
Medium Priority
?
149 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 400 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

609 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