Why does this fail? how to fix this?

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
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

bool quadratic(int, int, int, double, double);

int main()
{
int a, b, c;
double r1, r2; 
cout << "Enter values for a, b, and c to complete the quadratic formula: \n";
cin >> a;
cin >> b;
cin >> c;


if (quadratic(a, b, c, r1, r2))
{
cout << "Solution found: " << r1 << ", " << r2 << endl;
}
else
{
cout << "No real solution for this…sorry!\n";
}

return 0;
}
bool quadratic(int a, int b, int c, double & r1, double & r2)
{
		bool valid = true;
		if (b * b - 4 * a * c < 0)
		{
		valid = false;
		}
		else
		{
		r1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
		r2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
		}
	return valid;
}



this is my question for this Program:
Implement the quadratic function (from the notes!!) Your main() function should ask for the values of a, b,and c and pass them along to the function and then either report the solutions, or an error if the quadratic function returns false... let me say this one more time... this is from the notes!!


Its says
error C2668: 'sqrt' : ambiguous call to overloaded function
How can I fix this
It can't tell which overload of the sqrt function you want to use, because integers can be upgraded to floats, doubles, etc... and it doesn't know which one you want. You should cast to float or double so it can pick one.
Okay I would most likely want it as a double would I static_cast it for each a,b,and c or the whole equation and if not what should it be equal to or just squeezed into the equation?
1
2
r1 = static_cast <double>(b*b-4*a*c);
		r2 = static_cast <double>(b*b-4*a*c);


or like this?
1
2
r1 = (-b +  sqrt(static_cast <double> (b * b - 4 * a * c)) / (2 * a);
r2 = (-b - sqrt(static_cast <double> (b * b - 4 * a * c)) / (2 * a);


help?
You could just do
1
2
		r1 = (-b + sqrt(double(b * b - 4 * a * c))) / (2 * a);
		r2 = (-b - sqrt(double(b * b - 4 * a * c))) / (2 * a);
Why is this still failing?

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

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

bool quadratic(int, int, int, double, double);

int main()
{
int a, b, c;
double r1, r2; 
cout << "Enter values for a, b, and c to complete the quadratic formula: \n";
cin >> a;
cin >> b;
cin >> c;


if (quadratic(a, b, c, r1, r2))
{
cout << "Solution found: " << r1 << ", " << r2 << endl;
}
else
{
cout << "No real solution for this sorry!\n";
}

return 0;
}
bool quadratic(int a, int b, int c, double r1, double r2)
{

		bool valid = true;
		if (b * b - 4 * a * c < 0)
		{
		valid = false;
		}
		else
		{
		r1 =((-b + sqrt(double(b * b - 4 * a * c))) / (2 * a));
		r2 =((-b - sqrt(double(b * b - 4 * a * c))) / (2 * a));
		
		}

	return valid;
}
Is it the same problem? Or something different now?
The problem might be that it also exists in the std namespace. so write either std::sqrt(...) or ::sqrt(...) (the cast isn't needed)
@jet0717

I noticed that you don't need to pass r1 and r2. Especially since they don't have a value. I put the results of r1 and r2 in the function instead, and only print the failure.
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
// Quadriatic.cpp : Defines the entry point for the console application.
//

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

bool quadratic(int, int, int );

int main()
{
int a, b, c;
double r1, r2; 
cout << "Enter values for a, b, and c to complete the quadratic formula: \n";
cin >> a;
cin >> b;
cin >> c;


if (quadratic(a, b, c))
{
cout << "\n\nThank you..\n";
}
else
{
cout << "\n\nNo real solution for this sorry!\n";
}

return 0;
}
bool quadratic(int a, int b, int c)
{
double r1, r2;
		bool valid = true;
		if (b * b - 4 * a * c < 0)
		{
		valid = false;
		}
		else
		{
		r1 =((-b + sqrt(double(b * b - 4 * a * c))) / (2 * a));
		r2 =((-b - sqrt(double(b * b - 4 * a * c))) / (2 * a));
		cout << "Solution found: " << r1 << ", " << r2 << endl;
		}

	return valid;
}


It works when I inputted a = 1, b = 2 and c = -8
According to your notes,
Implement the quadratic function (from the notes!!) Your main() function should ask for the values of a, b,and c and pass them along to the function and then either report the solutions, or an error if the quadratic function returns false...
, you report a solution after passing the a,b and c variables, but only had to report an error if it returned false. I think this does.
Last edited on
YESS THANK YOUUUU !!!
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
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

bool quadratic(int, int, int, double &, double &);

int main()
{
int a, b, c;
double r1, r2; 
cout << "Enter values for a, b, and c to complete the quadratic formula: \n";
cin >> a;
cin >> b;
cin >> c;


if (quadratic(a, b, c, r1, r2))
{
cout << "Solution found: " << r1 << ", " << r2 << endl;
}
else
{
cout << "No real solution for this…sorry!\n";
}

return 0;
}
bool quadratic(int a, int b, int c, double & r1, double & r2)
{
		bool valid = true;
		if (b * b - 4 * a * c < 0)
		{
		valid = false;
		}
		else
		{
		r1 = (-b + sqrt(double(b * b - 4 * a * c))) / (2 * a);
		r2 = (-b - sqrt(double(b * b - 4 * a * c))) / (2 * a);

		}
	return valid;
}

Try with this
Topic archived. No new replies allowed.