help with simple error checking in borland C++ 4
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";
}
}
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement