| View previous topic :: View next topic |
| Author |
Message |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Thu Jul 12, 2007 10:05 pm Post subject: [Delphi] [Help] Checking For Certain Characters in a Textbox |
|
|
I'm trying to recreate a program in Delphi that I had previously created in C#. This program is designed to calculate the area of a circle with radius x, where x is user-input, a simple mathematical procedure, no?
In C#, in order to check for people who try to go against the laws of simple math (i.e., trying to calculate the circle with a radius of g9h837), what I did was a simple foreach loop.
| Code: |
foreach (char c in textBox1.ToCharArray())
{
if ((int)c < 48 || (int)c > 57) //48 is the ASCII value for 0,
// and 57 is the ASCII value for 9
{
MessageBox.Show("Invalid number.");
return;
}
}
|
However, foreach is not a valid command in Delphi. I would like to know the HARD way of checking for a number--as I don't want some stupid command that I'll only use for these instances, like
or something, I prefer the code--I learn better from that. An old saying, "Give a man a fish, and he'll eat for a day; teach a man to fish, and he'll eat for a lifetime" fits here.
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Thu Jul 12, 2007 10:08 pm Post subject: |
|
|
| a string is an array of characters, so you can loop through each character using a for loop and do what you did in C#
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Thu Jul 12, 2007 10:24 pm Post subject: |
|
|
Err, I'm kinda confused. Loops usually have some kind of integer condition, like
or something. Are you suggesting something like
| Code: |
var
c : char;
a,b : integer;
begin
a := Length(Trim(RichEdit1.Text));
for b := 0 to (a-1) in RichEdit1.Text
begin
if c < 48 or c > 57
then ShowMessage('Invalid number.');
end;
end;
end.
|
Edit: It's
not
Edit again:
It gives me two errors:
"Missing operator or semi-colon" on the line with "begin" right after the for line.
and
"Operator not applicable to this operand type" on the line with the if command.
Last edited by samuri25404 on Thu Jul 12, 2007 10:36 pm; edited 2 times in total |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Thu Jul 12, 2007 10:34 pm Post subject: |
|
|
| Code: |
procedure dosomething;
const
thestring: PChar = ('hello i am a string');
var
i: integer;
begin
for i := 0 to StrLen(thestring) do
begin
if thestring[i] > chr(48) or thestring[i] < chr(57) then
ShowMessage('Invalid number');
end;
end;
|
delphi is NOT C#, I suggest you learn the syntax before posting anything else.
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Thu Jul 12, 2007 10:38 pm Post subject: |
|
|
| Hmm, very confusing. I think I'll take you up on your advice--and I'd rep you, but I have to wait about 30 minutes before I can rep again.
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Fri Jul 13, 2007 1:51 pm Post subject: |
|
|
You could try a try-catch block, well, try-except in delphi, to catch the exception thrown by the StrToInt function which takes in a string and returns the corresponding integer.
Or, you could use the StrToIntDef function, which returns a default value in addition to the string, and returns the default value if the string is not in a valid number format.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
magicalimp Expert Cheater
Reputation: 0
Joined: 03 Dec 2006 Posts: 105
|
Posted: Sat Jul 14, 2007 5:58 pm Post subject: |
|
|
Appal's code will most likely pop up 'Invalid number' more than once, because it is a for loop. Instead you can:
1. ShowMessage('Invalid number') then Break out of the loop upon detection of a character with ASCII value that is not between 48 and 57;
2. Use a while loop that increments a counter and has conditions that the counter < StrLen(string) AND string[counter] is not a letter
3. Use a boolean variable that is modified within the for loop by the if statement
Just for reference:
048-057 are numbers
065-090 are uppercase letters
097-122 are lowercase letters
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Sat Jul 14, 2007 7:03 pm Post subject: |
|
|
| magicalimp wrote: | Appal's code will most likely pop up 'Invalid number' more than once, because it is a for loop. Instead you can:
1. ShowMessage('Invalid number') then Break out of the loop upon detection of a character with ASCII value that is not between 48 and 57;
2. Use a while loop that increments a counter and has conditions that the counter < StrLen(string) AND string[counter] is not a letter
3. Use a boolean variable that is modified within the for loop by the if statement
Just for reference:
048-057 are numbers
065-090 are uppercase letters
097-122 are lowercase letters |
Or just use the ord function to get the ascii values. Ex:
ord('a') returns 97
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
magicalimp Expert Cheater
Reputation: 0
Joined: 03 Dec 2006 Posts: 105
|
Posted: Sat Jul 14, 2007 10:20 pm Post subject: |
|
|
| DeltaFlyer wrote: | | magicalimp wrote: | Appal's code will most likely pop up 'Invalid number' more than once, because it is a for loop. Instead you can:
1. ShowMessage('Invalid number') then Break out of the loop upon detection of a character with ASCII value that is not between 48 and 57;
2. Use a while loop that increments a counter and has conditions that the counter < StrLen(string) AND string[counter] is not a letter
3. Use a boolean variable that is modified within the for loop by the if statement
Just for reference:
048-057 are numbers
065-090 are uppercase letters
097-122 are lowercase letters |
Or just use the ord function to get the ascii values. Ex:
ord('a') returns 97 |
That's what I was hinting towards with #1.
|
|
| Back to top |
|
 |
|