[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Pascal variable to only enter one letter?

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
kinobestew123
Asked:
kinobestew123
  • 12
  • 6
  • 2
  • +1
1 Solution
 
ZylochCommented:
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
 
mlmccCommented:
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
 
kinobestew123Author Commented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mlmccCommented:
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
 
kinobestew123Author Commented:
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
 
ZylochCommented:
You're opening your code in Notepad? Do Ctrl-A and then do Ctrl-C, come here, and do Ctrl-V
0
 
kinobestew123Author Commented:
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
 
For-SoftCommented:
If you want to control entered char data, then use ReadKey function from CRT unit.
0
 
kinobestew123Author Commented:
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
 
For-SoftCommented:
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
 
kinobestew123Author Commented:
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
 
For-SoftCommented:
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
 
kinobestew123Author Commented:
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
 
kinobestew123Author Commented:
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
 
For-SoftCommented:
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
 
kinobestew123Author Commented:
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
 
kinobestew123Author Commented:
Hold on, I have the problem that I can type any character I want in now, what is happening?
0
 
kinobestew123Author Commented:
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
 
For-SoftCommented:
You used ReadKey function twice, and you have to press the key twice:

repeat
until Readkey=#13);

Should work much better.
0
 
kinobestew123Author Commented:
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
 
For-SoftCommented:
Your program can be much shorter, if you declare a function to enter the character. But this is another story.

Good luck.
0
 
kinobestew123Author Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now