PROG
Request values of x distance and y distance
If both positive, compute total distance using Pythagorean
Theorem
Otherwise, give error and make user reenter numbers
PED
to learn how to make nice menus for programs
CONCEPTS
while (user hasn't asked to stop);
while (user still hasn't entered the correct data)
get new data
validate new data
end;
process the data input
ask user if they wants to enter another set of data
end;
SF
Must have "uses crt;" right after "program statement"
gotoxy(x,y) moves cursor to position x and y. (1,1) is upper left hand corner
clrscr; will clear the screen
clreol; erases to end of line
In most interactive programs that will be used by several people, we desire to see a nice menu structure with error-validation. The system should display questions neatly on the screen in a nice spatial arrangement. Each set of new data should be on a full screen by itself.
Turbo Pascal provides a limited set of such procedures to do this sort of task. Real programs of this nature would be done with various screen programs that provide detailed error-validation, protected fields and the like. In addition, these systems will allow the user to move around on the screen and correct whatever data he or she wanted to. When the user had the data correctly entered, the user would hit enter and the new data would be given to the computer to be found.
However, to get a feel for the issues involved in such programs we will look at several primitives
gotoxy(m,n)
clrscr
clreol
gotoxy will move the "cursor" to a given place on the screen. The cursor is where the next characters are to be output. If input were to occur, when the user types in the answer, this is where what the user types would be displayed back.
One decides where each prompt or message should be on the screen. Prior to the write statement for the prompt or message, the programmer should issue a gotoxy command to move to the place where the first character of that message should appear. PASCAL numbers the upper left hand position (1,1). The lower right-hand position would be (80,24) on normal screens.
The utility clrscr can be used to clear the screen. Generally, this will be called for a new set of data.
Sometimes, when one writes a line, one discovers that the previous information on the line gets in the way--the previous information must be erased. If you are outputing 20 characters of information on a line that already contains 30 characters, you would see the last 10 lines of the previous contents of the line. You should insert the clreol; command before the statement producing output that is getting mixed with old stuff.
Please don't forget to include the "uses crt" line after the "program" statement. That brings in the routines for gotoxy, clrscr and clreol.
There are probably as many ways to set up a user interface as there are programmers. This is called the look-and-feel of the program. You will find that you will end up doing much experimentation and make substantial modification to the program as you are developing the look-and-feel that you want for your program.
However, there is one pattern of programming that comes up quite a bit. The user has several sets of data that need to be processed in some way. Perhaps, the user wants to enter in the parameters of several mortgages and get out data on payment rates and average interest. For each mortgage, the data would have to be read in. Then the program would eliminate obviously incorrect data--for example, mortgages are not for amounts less than a dollar and interest rates would be between 5 and 20. In the event that there is an error, an appropriate message would be displayed to the user. The system would return to the first prompt and retrieve the information again. (If we were using a more sophisticated screen package, the user could bounce around and correct the data in the order they chose. The user would not be forced to reenter the data that was already correct.)
When the user has finally entered the data correctly, the system would display the answer or otherwise process the data. Then the user would be asked if there were more data to be processed. If so, the screen would be cleared. And we would start all over again at the first prompt.
Our sample program takesin values for an x-distance and a y-distance. A distance must be positive. Thus, if either distance is negative, the user would have to reenter both pieces of data. The program will do this for each value of input until the user answers a question that he doesn't have any more data.
The distance is computed by the formula:
dist = SQRT { {x sub dist sup 2} + {y sub dist sup 2}}
(1)Since the two distances should be positive, logically, we want to complain and ask what is going on whenever the user enters an obviously incorrect value for these numbers.
The outer loop from lines 9 to 46 processes each set of correct data. The variable, endflag, will be set to one in line 43 when the yuser finally indicates that there is no more data to be processed. Liens 12 to 35 will process each attempt by the user to get the data correct. errorflag is set to 1 when there is no correct data yet. We set this flag to one, so the loop will execute once--the first time there is an error, nothing has been typed in yet. Line 13 will go to the upper left hand ocrner and output 'please enter x distance' The value of x, the x distance, will be read in. Then the prompt for the y value will appear on line 3, starting at column 1. The y value will be read in. If either is negative, we will then display either the message 'I am sorry but both distances must be postive' or 'I am sorry but they distance must be positive' on line 5 starting at column 1. A little bit further down will be a request to renter both values. If x>=0 and y>=0, errorflag would end up 0 and we would leave the inner loop.
When we leave the inner loop, that means x and y contain valid
data. We comptue the distance in line 36 and display it in lines
37 and 38. Note that the distance is displayed on line 5 of the
screen. Also note that we use the "10:2" to get the right display.
Lines 39 to 40 ask the user if more data is requested. We set
endflag appropriately in lines 43 to 45.