Don't know why this is "nan"

I have been working on this project for a fair amount of time. And whenever I try to print out my variables so that the output looks right, the variable is "nan". I know that nan is basically the same thing as null. But I don't see why this is happening when I clearly am giving it a value. If anyone can see the problem the help would be greatly appreciated.

Here is my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Program to input two integers
// Calculate and print the sum and product of the integers

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;



int main()
{

        int A;
        int B;
        int C;
        double solution1 = 0, solution2 = 0;
        int num2Solutions = 0;
        char answer = 'y';

        cout<< fixed<< showpoint<< setprecision(3);

        do{    //this line starts your loop off, this is not a while loop but a do while loop

        // cout enter: a, b, c
        // read in a b c like this --- cin >> a >> b >> c;
        // end the line

        // check to see if a = 0
        // if so then print out the error message

        // if a != 0, then use an else statement to keep going in the program

        cout<< "Insert A, B, then C: "<<endl;
        cin>> A >> B >> C;
        cout<<endl;

        solution1 = (((-1 * B) + sqrt(pow(2, B) - (4*A*C))) / (2*A));
        solution2 = (((-1 * B) - sqrt(pow(2, B) - (4*A*C))) / (2*A));
        //use the quadratic formula to find sol_1 and sol_2
        //sol_1 would be when you do -b + .......
        //sol_2 would be when you do -b - .......


        if(A == 0)
        {
            cout<< "The equation is not quadractic because A = 0";//print out the error message saying that a cannot be zero here.
        }

        else
        {

            //rest of the program



            //now you need to check to see if the equation has 2 roots or 1 root
            //the way that you would do this is to use an if else statement like this
            if(solution1 == solution2)  /*sol_1 and sol_2 are the same*/
            {
                cout<< "There is only one root! ";
                cout<< "Root: " << solution1 << endl; //thenn print out that there is one root and print out either sol_1 or sol_2
            }

            else
            {
                cout<< "Root 1 = " << solution1 << endl;
                cout<< "Root 2 = " << solution2 << endl;
                cout<< endl;
                 //state that there are 2 different solutions and print out
                //sol_1 and sol_2 with their respective labeling

                //also, increment the counter that keeps track of how manyt
                //equations input had 2 real solutions
                //that counter is called "num2Solutions"
            }

            cout<<"Another equation? Enter Y or N: "<<endl;
            cin>>answer; // here is where you will ask the user to enter 'y' or 'n' if he would like
            // to enter another equation
            // read in the input using the cin function
            // the name of the variable that tracks this is "answer"

        }

        }while(answer == 'y'); // this line check to make sure that you still want to continue with
                               // the program. If you type in 'y' when it asks if you would like to
                               // do another then it loops through the loop and until you and with
                               // an 'n' or some letter that it not 'y'

        cout<< "There were " << num2Solutions << " equations with 2 real roots." <<endl; // this is where you will print out the number of equations that your entered
        // that had 2 real solutions

    return 0;
}
Two things I noticed are that you calculate solution1 and solution2 before you check whether A==0 and that the quadratic formula admits for complex solutions, but your program does not.

To fix the first problem, test for the condition A==0 before you do the calculations.
For the second, test whether (B2-4AC) is positive or negative. If it's negative, you'll need to use complex numbers.

I think it's in <complex>, but I've never used it, so double check that in your book or Google it.
Topic archived. No new replies allowed.