C/C++ Fixing a C++ Program Error: Negative Square Root

AI Thread Summary
To handle cases where a user inputs invalid values leading to the square root of a negative number in a C++ program, it is essential to implement error checking. The discussion highlights the use of the isnan function to determine if the result of a square root operation is not a number. Additionally, it suggests checking the input value before calling the sqrt() function to prevent domain errors. If a negative number is passed, the global variable errno will be set to EDOM, indicating a domain error. Alternatively, wrapping the sqrt() call in a try-catch block can catch exceptions thrown by the function when a negative number is encountered. This approach ensures that the program can display an appropriate error message to the user.
metalmaniac
Messages
4
Reaction score
0
hi there,

i have written a small program in C++ and if the user puts in the wrong values for some of the variables the operation will end up trying to square root a negative number and this returns that the answer is not a number

can anyone show me how to write an if function that if
Code:
double answerplus
or
Code:
double answerminus
are not numbers it will display an error message


thanks
 
Technology news on Phys.org
I think you're looking for isnan. But it's been a while since I looked at that part of the C library.
 
c++ is a strongly typed language, so if you input a double, it is only going to assign a floating point value to the input variable.

According to the C++ reference library:

If the argument is negative, a domain error occurs, setting the global variable errno to the value EDOM.

http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

So you check that value after processing each square root in your function to ensure that the sqrt() function returned a number, either that or you could check the value before invoking the sqrt() function.
 
Last edited by a moderator:
Or you could call sqrt() in a try : catch{} block - sqrt will throw an exception with a negative number
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top