Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error handling and Integers 1

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
US
My app gets a string from a serial port and takes three characters -- which SHOULD always be numbers -- from the string.

There was an instance where one of the three characters in the string was NOT an Integer and it could not do the StrToInt conversion. A messageBox popped up and the program did not proceed until the messagebox was acknowledged.

I need to find a way to say " if not a valid int then go ahead to the next one" (basically give it the acknowledgement that the messagebox would have).

Does anyone hjave any ideas?

Thanks

fergmj
 
You should put your conversion into a try...catch statement and then output an error message to a MessageBox.
Code:
int ThisIsAnInteger;
String ThisShouldBeAString;
.
.
.

try
{
    ThisIsAnInteger = StrToInt(ThisShouldBeAString);
}
catch (...)
{
    // Something went wrong
    int UserSelects  = Application->MessageBox("Not a valid integer. Go ahead to the next one?", "Bad Number", MB_YESNO);

    // User says no
    if (UserSelects == IDNO)
    {
        // Do some closing stuff
        return;
    }
}

// Go on with processing
.
.
.

In this case, if the convertion fails, a message box pops up with a caption of "Bad Number", a message that says "Not a valid integer. Go ahead to the next one?," and two buttons labeled "Yes" and "No." If the user presses "No" the process will stop (return). Otherwise, you can just go on.

Look carefully at MessageBoxes in your help file. Borland has encapsulated Window's MessageBox API to make it easier to use. The MessageBox can return several values that you can then use to decide what to do.

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top