Sign in to follow this  
razo

help with simple error checking in borland C++ 4

Recommended Posts

hi everyone.trying to make a temperature converter and i only want to input numbers, no other characters. the whole fuction pretty much works bar this line: if(Edit1->Text != '0-9') <------------------this is wrong!! { MessageBox(0, "That Was Not A Number! Please Enter Numeric Values Only", "OOPS!!", MB_OK); } any help would be very helpful thanks. heres the rest of the function to get an idea of what i am doing: void __fastcall TMain_Form::btnCelsius_To_FahrenheitClick(TObject *Sender) { float Temperature_In_Celsius; float Temperature_Converted_To_Fahrenheit; #define Nine 9 #define Five 5 #define Thirty_Two 32 #define One_Hundred 100 #define One_Hundred_Float 100.0 if(Edit1->Text == "") { MessageBox(0, "Please Enter A Temperature To Convert", "OOPS!!", MB_OK); } if(Edit1->Text != '0-9') { MessageBox(0, "That Was Not A Number! Please Enter Numeric Values Only", "OOPS!!", MB_OK); } else { Temperature_In_Celsius = StrToFloat(Edit1->Text); Temperature_Converted_To_Fahrenheit = Temperature_In_Celsius *Nine / Five + Thirty_Two; Edit2->Text = floor(Temperature_Converted_To_Fahrenheit * One_Hundred) / One_Hundred_Float; Edit2->Text = Edit2->Text + " F"; } }

Share this post


Link to post
Share on other sites
Quote:
Original post by razo
hi everyone.trying to make a temperature converter and i only want to input numbers, no other characters. the whole fuction pretty much works bar this line:

if(Edit1->Text != '0-9') <------------------this is wrong!!
{
MessageBox(0, "That Was Not A Number! Please Enter Numeric Values Only", "OOPS!!", MB_OK);
}

any help would be very helpful thanks.


Hi, I think the (Edit1->Text != '0-9') test is not doing what you want it to do (if Edit->Text is a char (1 byte) it probably doesn't even compile since '0-9' is an int). Explanation: '0-9' is interpreted as a sequence of three bytes, the bytes are the ASCII-values for the characters: 0, -, 9 and the sequence evaluates to a number which is not very relevant in this case.

Instead you probably want to test whether the ASCII-value for the character in Edit1->Text is less than the ASCII for the character 0 or greater than the ASCII for the character 9 (in other words: Edit1->Text is not a digit).

Example:

if(Edit1->Text < '0' || Edit->Text > '9')
{
MessageBox(0, "That Was Not A Number! Please Enter Numeric Values Only", "OOPS!!", MB_OK);
}



\Jimmy H

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this