435,373 Members | 2,945 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,373 IT Pros & Developers. It's quick & easy.

Check if variable is defined

 P: 2 I need help with this program and I'm not sure how to do something. The program is supposed to take 3 doubles to make a quadratic equation, then calculate and display all real roots. Here's the problem: If one or more of the roots is unreal or complex, it tries to display them anyway because I don't know how to test for that. Here's the relevant parts: Expand|Select|Wrap|Line Numbers double a, b, c, root1, root2;     cout << "Enter the 3 coefficients of the quadratic equation." << endl;     cin >> a >> b >> c;     if(cin)     {         cout << "Last input was successful." << endl;         cout << a << "x" << (char)253 << " + " << b << "x + " << c << endl;         root1 = (-b + sqrt(pow(b,2)+4*a*c)) / (2*a);         root2 = (-b - sqrt(pow(b,2)+4*a*c)) / (2*a);         if(root1 && root2)             cout << "There are two real roots." << endl;         else if(root1 || root2)             cout << "There is one real root." << endl;         else cout << "All roots are complex numbers." << endl;         if(root1)             cout << "Root 1 = " << root1 << endl;         if(root2)             cout << "Root 2 = " << root2 << endl;     }     else         cout << "Last input was unsuccessful. Check your data and try again." << endl; Obviously, those if statements aren't of much use. So, is there some method or something to test the variables to see whether they're defined? Sep 18 '06 #1 