Solved

Pascal variable to only enter one letter?

Posted on 2004-10-03
22
328 Views
Last Modified: 2011-09-20
I guess this is really simple but I am unsure how to do it. I am new to pascal (and any programming for that matter so keep it simple please). I want to make a program with a variable that only allows the user to enter one single letter. For example, in my program, i have:

var
   One : char;
   Two : char;
   Three : char;

I have ran this program and it allows me to enter as much as i want (any explination as to why?, i thought the char variable only lets you enter 1 character). I want a variable that will not let me enter more than one character and only lets you enter letters (eg. A B C D...). Thankyou in advance.
0
Comment
Question by:kinobestew123
  • 12
  • 6
  • 2
  • +1
22 Comments
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Hi kinobestew123,

This should work.

If you use ReadLn for instance, you cn enter as much as you want, but the program will only accept the first letter in what the user types.

Regards,
Zyloch
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
To ensure they only enter a single letter, you will need to control the input.  The variable only controls the data or memory space allotted to it.  As Zyloch stated if you read ONE using readln the whole line is read but only the first character is put into ONE the rest of the line is discarded.

There are other methods to use to get a single character but they can be highly dependent on the Pascal you are using.

mlmcc
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
Oh right, I understand now that the ReadLn thing (which I am using) allows the user to enter as much as they want. I think I better tell you what the program is going to do. For college, i must make a program that allows the user to enter three letters and then it will put them into alphabetical order (note, the user enters the characters at seperate times, eg. enter the first letter). At the moment, the user can input like you said, as much as they want. It then takes only the first character, which is what is happening. The problem is as it is suppost to put letters in alphabetical order, as the user can type characters in such as ?><@:}P)(*&, etc, it is not sticking to the brief. I want to stop this from happening, any ideas? Also, it would be nice if I could make it so the user can only enter 1 letter, i am using Turbo Pascal 7.0 if that is what you meant by 'highly dependant on the Pascal you are using'. Thankyou again.
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
Since this is homework, we cannot do the project for you.  However, if you post the code you are using we can help.  What methods of input do you understand?

mlmcc
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
I tried to copy my code by highlighting it all (so the background around the writing went grey) then going on edit/copy. I then came to this box that i am typing in now and I was unable to paste it. I dont think it is copying my code. How do I copy it so that I can post it? Also, what do you mean by inputs, I dont understand? I know that an input is anything that is put into the computer ready to be processed. And that it is done using the keyboard, lol. I bet that isnt what you mean. Please help, thanks.
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
You're opening your code in Notepad? Do Ctrl-A and then do Ctrl-C, come here, and do Ctrl-V
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
Oh kool, i never knew you could open a pascal program in Notepad, I assume that you could also use Notepad to do the pascal language itself (although you wouldn't be able to compile it). Anyway, here is the whole of my pascal code:

program AlphabeticalOrder;
{This program will ask the user to enter three letters and then will display
them in alphabetical order}
uses Crt;

var
   One : Char;
   Two : Char;
   Three : Char;

begin
   ClrScr;
   GoToXY(10,8);
   WriteLn('Please enter the first letter that you want putting into order');
   GoToXY(10,10);
   ReadLns(One);
   ClrScr;
   GoToXY(10,8);
   WriteLn('Please enter the second letter that you want putting into order');
   GoToXY(10,10);
   ReadLn(Two);
   ClrScr;
   GoToXY(10,8);
   WriteLn('Please enter the third letter that you want putting into order');
   GoToXY(10,10);
   ReadLn(Three);
   ClrScr;
   GoToXY(10,8);
   begin
   If (One<Two) and (Two<Three) and (One<Three) then
      WriteLn('The order of your letters is ',One,' ',Two,' ',Three,'.');
   If (One<Two) and (Two>Three) and (One<Three) then
      WriteLn('The order of your letters is ',One,' ',Three,' ',Two,'.');
   If (One>Two) and (Two<Three) and (One<Three) then
      WriteLn('The order of your letters is ',Two,' ',One,' ',Three,'.');
   If (One>Two) and (Two<Three) and (One>Three) then
      WriteLn('The order of your letters is ',Two,' ',Three,' ',One,'.');
   If (One<Two) and (Two>Three) and (One>Three) then
      WriteLn('The order of your letters is ',Three,' ',One,' ',Two,'.');
   If (One>Two) and (Two>Three) and (One>Three) then
      WriteLn('The order of your letters is ',Three,' ',Two,' ',One,'.');
   If (One=Two) or (Two=Three) or (One=Three) then
      begin
      WriteLn('Sorry but you entered the same letter more than once.');
      GoToXY(10,10);
      WriteLn('Please run the program again.');
      end;
   end;
   Delay(1000);
   GoToXY(10,15);
   WriteLn('Thankyou for using this program.');
   Delay(1000);
   GoToXY(10,17);
   WriteLn('Please press any key to exit');
   Readkey;
   end.

PS: sorry, but I didn't put any comments in it and I know I should but I kinda forgot till I got to the end. Hope you can figure out what every part is doing.
0
 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
If you want to control entered char data, then use ReadKey function from CRT unit.
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
OK, so what you are saying is I should change where I have put ReadLn(one); in my code above, I should change it to Readkey?? Then, how do I make it so that it will store what they press as one? You will have to explain better, I dont know anything really about programming. Thankyou
0
 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
ReadKey is a function. Function can be used like a variable. To read a key into variable one, you have to make following statement:

One:=Readkey;

The program will stop waiting for user to press the key. But it will not display the key on the screen.

Now, if you do not want to accept special characters (?><@:}P)(*&,) you can create the loop. If wrong character will be entered program will ignore it and wait for another character until proper key entry.

Repeat One:=Readkey;
until (One<='z') and (One>='a');
WriteLn (One);

The last line WriteLn (One), will display on the screen entered character.
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
OK, thanks a lot! That works, just one more thing if you dont mind, how do I make it so that after the WriteLn(one);, that displays the character entered, they have to press the enter key to go onto the enter next letter part. At the moment you see, it displays the character for a really small amount of time, i know that I can put a Readkey after it so that they must press any key but I would rather they could only press the enter key. I will give you the points weather you answer me or not, i would really like it if you would help me. Thankyou very much.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
Create additional character variable, and use the loop again, but with the additional variable.
Instead of checking range a..z, check if enter key was entered. I'm not sure, but Enter is #10 or #13 character.
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
Right, this doesn't seem to be working any more. I have the following:

GoToXY(10,8);
Repeat One:=Readkey;
until (One<='z') or (One<='Z') and (One>='a') or (One>='A');
WriteLn(One);
Repeat Readkey;
until Readkey='#13');
ClrScr;

When I run this (with the rest of the program above GoToXY(10,8); and below ClrScr;), it works and allows me to enter only one letter and no characters like I want but when I press the enter key, nothing happens and I am locked into the program. Why is this??
Thanks
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
I have just found out that if I remove the ' around the #13, in until (Readkey='#13'); so it says until (Readkey=#13); it works but I have to press the enter key twice, do you have any fix please?
0
 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
You made a mistake in following line:

until (One<='z') or (One<='Z') and (One>='a') or (One>='A');

You should use:

until ((One<='z') and (One>='a')) or ((One<='Z') and (One>='A'));
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
I have changed this and I still have to press enter twice after I have typed my letter in, why is this? I only want to have to press enter once.
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
Hold on, I have the problem that I can type any character I want in now, what is happening?
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
Oh no, ignore that last comment, its ok now. The only problem I have is I have to press enter twice after inputting the letter.
0
 
LVL 10

Accepted Solution

by:
For-Soft earned 50 total points
Comment Utility
You used ReadKey function twice, and you have to press the key twice:

repeat
until Readkey=#13);

Should work much better.
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
Lol, oh yeah, thanks a lot, you have been great. I would have given you more points but unfortunately, I dont really have many. Maybe I will become good enough to help you someday, lol. Probably Not. Anyway, thankyou v.much.

0
 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
Your program can be much shorter, if you declare a function to enter the character. But this is another story.

Good luck.
0
 
LVL 1

Author Comment

by:kinobestew123
Comment Utility
We haven't been taught about anything like that so I best not attempt it until we are. I think that the assignment wanted us to use If statements so that is what I did. If it is wrong, i dont really care. Thanks a lot anyway, it works exactly how I wanted it to so...

kinobestew123
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now