- #1
CFDFEAGURU
- 783
- 10
Hello all,
First let me state the following:
1. The programs that I write are not production code.
2. I am not a trained developer.
3. I do not work for a software development company.
My question pertains to the use of try catch exception handling vs. using do while loops.
Currently, I screen all user input in the following manner.
Below is an example of a simple user input function named, tube_od(), this function simply asks the user for the tube od in inches, converts the od to meters. If the tube od has been entered as a zero or negative value the output to the user is "Invalid Entry" and the question is repeated until the proper conditions are met.
Here is the function string_number()
The above method works fine but I was wondering if it would be better to use a try catch statement and take advantage of the C++ exception and error handling.
I would appreciate any comments.
Thanks
Matt
First let me state the following:
1. The programs that I write are not production code.
2. I am not a trained developer.
3. I do not work for a software development company.
My question pertains to the use of try catch exception handling vs. using do while loops.
Currently, I screen all user input in the following manner.
Below is an example of a simple user input function named, tube_od(), this function simply asks the user for the tube od in inches, converts the od to meters. If the tube od has been entered as a zero or negative value the output to the user is "Invalid Entry" and the question is repeated until the proper conditions are met.
Code:
void tube_od(){
// Obtain the tube outer diameter in inches
do{
printf("Tube OD (inches) ");
getline(cin,s);
XDO = string_number<double>(s);
XDO = XDO*0.0254; // Convert inches to meters.
if(XDO <= 0) cout << "Invalid Entry" << endl;
}while(XDO <= 0);
}
Here is the function string_number()
Code:
template <typename T>
// Templated function used to obtain the correct user
// input. All user input comes in as a text string and
// then is converted by this function to the appropriate
// typedef.
T string_number (const string &Text)
{
istringstream ss(Text);
T result;
return ss >> result ? result : -1;
}
The above method works fine but I was wondering if it would be better to use a try catch statement and take advantage of the C++ exception and error handling.
I would appreciate any comments.
Thanks
Matt