• 04-19-2009
zerkz
Hello, I just finished, well sorta finished a program I have been working on for my last assignment in my COSC C++ class. I've used all of these components in other programs before, but in this specific condition it is giving me a fit.

The goal of the program is to calculate the conditions of the quadratic formula (will the answer be one root? two roots? or imaginary roots?), and provide the roots if it is one or two. I opted to use a switch statement instead of if/else.

However, It seems either my secondary function is severely messed up, or I just have a small typo. However, I cannot find either.

Code:

```#include <iostream> #include <iomanip> #include <cmath> using namespace std; int DetermineRootTypes(int a, int b, int c){         if(b * b - 4 * a * c > 0){                 return 1;         }                if(b * b - 4 * a * c == 0){                 return 2;         }         if(b * b - 4 * a * c < 0){                 return 3;         } return 0; } int main(){         int a, b, c;         float root1;         float root2;         int condition;         cout << showpoint << fixed << setprecision(4);         for(int i = 0;i < 3;i++){                 cout << "Please input the 3 variables of the quadratic equation. (a, b, and c)." << endl;                         cin >> a >> b >> c;         condition = DetermineRootTypes(a,b,c);                 switch(condition){ // Switch statements, in which the roots are computed.                         case 1:                                 root1 = (-1 * b * sqrt(b * b - 4 * a * c)) / 2a;                                 root2 = (-1 * b * ( -1 * sqrt(b * b - 4 * a * c))) / 2a;                                 cout << "The answer are two real roots." << endl;                                 cout << "The first is " << root1 << endl;                                 cout << "The second is " << root2 << endl;                         break;                         case 2:                                 root1 = (-1 * b * 0) / 2a;                                 cout << "The answer is one repeated root." << endl;                                 cout << "The answer is " << root1 << endl;                         break;                         case 3:                                 cout << "The answer is two imaginary roots." << endl;                         break;                         default:                                 cout << "Values entered were not numbers." << endl;                         break;                 }         } }```

Now, for the various errors...

Code:

```(42) : error C2668: 'sqrt' : ambiguous call to overloaded function (42) : error C2059: syntax error : 'bad suffix on number' (42) : error C2146: syntax error : missing ';' before identifier 'a'```
Those pretty much repeat for a few more lines down the code.

Any help would be greatly appreciated. Feel free to flame my horrid coding methods too.
• 04-19-2009
anon
a is not a valid suffix on literal numbers. If you meant to multiply 2 by a you obviously need to use the * operator (also don't forget brackets).
• 04-19-2009
Sebastiani
sqrt is overloaded for different types, and the compiler isn't sure which one you want. A cast will suffice:

Code:

`sqrt(float(b * b - 4 * a * c))`
>> 2a

Did you mean 2 * a, or are you trying to use a variable with that name (which you can't do, of course, since it begins with a digit)?
• 04-19-2009
bithub
There are 3 defined prototypes for sqrt:
Code:

```double sqrt (      double x ); float sqrt (      float x ); long double sqrt ( long double x );```
You are passing an int into the sqrt function. The problem is that none of the overloaded sqrt functions take an int, so it must be converted. But which one is it converted to? This is where the ambiguity comes from. To resolve this ambiguity, a simple cast would suffice.
Code:

`sqrt(static_cast<double>(b * b - 4 * a * c))`
• 04-19-2009
zerkz
thanks all. Yeah, it was meant to be 2 * a. I just changed the outside functions inputs to float, so its not trying to shove ints in there. Thank you again.