Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Delphi] [Help] Checking For Certain Characters in a Textbox

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Thu Jul 12, 2007 10:05 pm    Post subject: [Delphi] [Help] Checking For Certain Characters in a Textbox Reply with quote

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

Code:

isNumeral


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
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Thu Jul 12, 2007 10:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Thu Jul 12, 2007 10:24 pm    Post subject: Reply with quote

Err, I'm kinda confused. Loops usually have some kind of integer condition, like

Code:

for i := 1 to 5


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

Code:

c < 48 or c > 57


not

Code:

c < 48 || c > 57


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
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Thu Jul 12, 2007 10:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Thu Jul 12, 2007 10:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
DeltaFlyer
Grandmaster Cheater
Reputation: 0

Joined: 22 Jul 2006
Posts: 666

PostPosted: Fri Jul 13, 2007 1:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
magicalimp
Expert Cheater
Reputation: 0

Joined: 03 Dec 2006
Posts: 105

PostPosted: Sat Jul 14, 2007 5:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
DeltaFlyer
Grandmaster Cheater
Reputation: 0

Joined: 22 Jul 2006
Posts: 666

PostPosted: Sat Jul 14, 2007 7:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
magicalimp
Expert Cheater
Reputation: 0

Joined: 03 Dec 2006
Posts: 105

PostPosted: Sat Jul 14, 2007 10:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites