Hi everyone:
I have never dealt with errors before in C++. I would like to write code for identifying and dealing with an error, and I would very much appreciate any suggestions about the best way to go.
Here's what I would like to do in more detail:
I have written a function for performing a simple calculation. I want to:
1) Compare the result of the calculation with a certain value (let's call it x). If the result is the same, everything has worked. If the result is not the same, there has been a problem and I'd like to get an error message + end the program run.
2) Here's a complication... The calculation result might differ from x by some small value. For example, x might be 1 and the calculation result might be 1.1. The calculation is still OK. It has only failed if the difference is greater than some value (let's call it double tooMuch).
Here's what I have tried so far. If anyone could comment on whether there is a better way to go, that would be great.
double tooMuch = 0.2; // If the difference is <= 0.02, the calculation has worked.
double calculated = ... ; // The calculated value (I left out the actual calculation)
// Let's say the result is 1.1.
double goalValue = 1.0;
cout << "Did this work?\n";
if ( calculated == goalValue )
{
cout << "Yes!\n";
}
// Here I have tried to say, if the calculated and goal values differ by less than tooMuch, that's OK. But it's not OK if the difference is more.
if ( (( calculated - tooMuch) > goalValue ) || (( calculated + tooMuch) > goalValue))
{
cout << "Something is wrong!\n";
}
Thanks for any help!
I have never dealt with errors before in C++. I would like to write code for identifying and dealing with an error, and I would very much appreciate any suggestions about the best way to go.
Here's what I would like to do in more detail:
I have written a function for performing a simple calculation. I want to:
1) Compare the result of the calculation with a certain value (let's call it x). If the result is the same, everything has worked. If the result is not the same, there has been a problem and I'd like to get an error message + end the program run.
2) Here's a complication... The calculation result might differ from x by some small value. For example, x might be 1 and the calculation result might be 1.1. The calculation is still OK. It has only failed if the difference is greater than some value (let's call it double tooMuch).
Here's what I have tried so far. If anyone could comment on whether there is a better way to go, that would be great.
double tooMuch = 0.2; // If the difference is <= 0.02, the calculation has worked.
double calculated = ... ; // The calculated value (I left out the actual calculation)
// Let's say the result is 1.1.
double goalValue = 1.0;
cout << "Did this work?\n";
if ( calculated == goalValue )
{
cout << "Yes!\n";
}
// Here I have tried to say, if the calculated and goal values differ by less than tooMuch, that's OK. But it's not OK if the difference is more.
if ( (( calculated - tooMuch) > goalValue ) || (( calculated + tooMuch) > goalValue))
{
cout << "Something is wrong!\n";
}
Thanks for any help!