Solved

Pascal variable to only enter one letter?

Posted on 2004-10-03
22
355 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
[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
  • 12
  • 6
  • 2
  • +1
22 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12211064
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
ID: 12211393
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
ID: 12211782
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 100

Expert Comment

by:mlmcc
ID: 12212187
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
ID: 12212687
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
ID: 12212718
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
ID: 12212819
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
ID: 12213113
If you want to control entered char data, then use ReadKey function from CRT unit.
0
 
LVL 1

Author Comment

by:kinobestew123
ID: 12218703
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
ID: 12218957
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
ID: 12219226
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
 
LVL 10

Expert Comment

by:For-Soft
ID: 12219361
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
ID: 12220483
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
ID: 12220505
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
ID: 12220608
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
ID: 12220665
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
ID: 12220692
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
ID: 12220714
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
ID: 12220750
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
ID: 12220809
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
ID: 12220929
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
ID: 12220953
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…

696 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