Getting a C++ double function to report a message instead of returning a numerical when a certain condition is met

Viewed 187

I am very new to C++, and am trying to create a pair of functions that will report two possible real solutions to a quadratic equation. I am also trying to enter a message into these functions that will display in lieu of a solution if no real solution exists.

The functions correctly report real solutions, but when a non-real solution appears, the program will return "The two possible real solutions to this problem are nan and nan," and my error message ("There is no real solution to that quadratic equation. Please enter a different set of numbers") does not show up in the terminal at all.

Here is my code. Thank you in advance for your help!

 #include <iostream>
#include "std_lib_facilities.h" // from a Github page by Bjarne Stroustrup that corresponds with the Programming: Practices and Principles book. Note that I am learning this on my own; it is not for homework.
#include <cmath>


        double quafo1 (double a, double b, double c)
        {

if ((sqrt((b*b)-(4*a*c))) < 0)
{
cout << ("There is no real solution to that quadratic equation. Please enter a different set of numbers;/n");

}
else {
    double x1 = ((b * -1) + sqrt((b * b) - (4 * a * c))) / (2 * a);
            return x1;
            }

    }

        double quafo2 (double a, double b, double c)
        {

         if ((sqrt((b*b)-(4*a*c))) < 0) 
         cout << ("There is no solution to that quadratic equation. Please enter a different set of numbers;");

else {
    double x2 = ((b * -1) - sqrt((b * b) - (4 * a * c))) / (2 * a);

        return x2;
        }


    }

double a;
double b;
double c;
int main()
{
    cout << "This program will solve a quadratic equation as long as one or more real solutions are possible. Please enter values for a, b, and c (separated by spaces and followed by enter, e.g. 3 5 4), after which both possible answers for x will be provided. Please note that a cannot equal 0.\n";
while (cin >> a >> b >> c)
{       
     cout << "The two possible real solutions to this problem are " << quafo1(a, b, c) << " and " << quafo2(a,b,c) << ".\n"; 

}
}
5 Answers

The sqrt function will never return a negative value*, so your test (sqrt((b*b)-(4*a*c))) < 0 will never be TRUE. What you probably want to do is test if the argument to sqrtis negative:

    if (((b*b)-(4*a*c))) < 0) {
        cout << ("There is no solution to that quadratic equation. Please enter a different set of numbers;");
        return nan("");
    }
    else {
        //...

Alternatively, if that argument is negative, the sqrt function will return NaN, which you can test for with the isnan() function:

    if (isnan(sqrt((b*b)-(4*a*c)))) {
        cout << ("There is no solution to that quadratic equation. Please enter a different set of numbers;");
        //...

Feel free to ask for further clarification and/or explanation.

Note: Strictly speaking, any (positive) number has two square roots, one positive and one negative: 2 x 2 = 4 but also -2 x -2 = 4.

I would suggest a few changes to the programme that fix the issues you are having.

#include <iostream>
#include "std_lib_facilities.h" 
// from a Github page by Bjarne Stroustrup that corresponds with the Programming: Practices and Principles book. Note that I am learning this on my own; it is not for homework.
#include <cmath>

double quafo1(double a, double b, double c) {
if ((b*b-4*a*c) < 0)
    return sqrt(-1);
else
    return ((b * -1) + sqrt((b * b) - (4 * a * c))) / (2 * a);
}

double quafo2(double a, double b, double c) {
    if ((b*b-4*a*c) < 0)
        return sqrt(-1);
    else
        return ((b * -1) - sqrt((b * b) - (4 * a * c))) / (2 * a);
}

double a;
double b;
double c;

int main()
{
    cout << "This program will solve a quadratic equation as long as one or more real solutions are possible. Please enter values for a, b, and c (separated by spaces and followed by enter, e.g. 3 5 4), after which both possible answers for x will be provided. Please note that a cannot equal 0.\n";
    while (cin >> a >> b >> c) {    
     double s1 = quafo1(a,b,c),
         s2 = quafo2(a,b,c);
     if (s1==s1 && s2==s2)
         cout << "The two possible real solutions to this problem are " << s1 << " and " << s2 << '.' << endl; 
     else
         cout << "There is no real solution to that quadratic equation. Please enter a different set of numbers." << endl;
    }
} 

First of all, as @Adrian mentioned above, you need to check whether b*b-4*a*c is negative or not and not its square root.

Second, the function must return something in all cases. Hence, you must have a return statement for when there are no real solutions. In those cases, you may simply return NaN. One way to do that would be returning sqrt(-1) as I did. In fact, you can simply return the solution to the quadratic equation, which would be NaN if no real solutions are possible because the square root of b*b-4*a*c is involved.

Now, in main, you may check if the returned value is NaN. To check for that, you can use isnan(), or check if it is equal to itself, which I did over here. I hope this solves your problem.

An indirect answer is that C++ has exceptions, precisely when you cannot return a value. You can define your own exception classes, but std::out_of_range makes perfect sense here.

Exceptions are thrown like throw std::out_of_range("There is no real solution to that quadratic equation."); and caught with try { ... } catch (std::exception& e) { std::cerr << e.what() << " Please enter a different set of numbers";}

cout << ("There is no real solution to that quadratic equation. Please enter a different set of numbers;/n");

This line should be terminated with "\n", not "/n". "<< endl;" could be used

Related