Solved

Controlling the user

Posted on 1998-01-28
2
138 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

14 Experts available now in Live!

Get 1:1 Help Now