New Here Need Help with Coding

Hey everyone, due to circumstances I wasn't able to attend my class and have fallen behind. I'm not able to build this, this is the practice assignment we need to turn in and im not sure whats going on exactly...i got to line 24 and think the output would be -8.1 after that...im lost and i cant build the program because sqrt on line 36 keeps erroring out...

in a nutshell, can anyone help me understand lines 30 and 36 and help get it to compile and run so i learn myself. thank you.

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
// Lab 3 tryIt3A -- Evaluating arithmetic expressions
// Determine what each output statement will print. Then run 
// the program to see if you are correct. If any of your answers 
// are wrong, figure out why the program behaved as it did.
#include <iostream>
#include <cmath>
using namespace std;

9 int main()
10 {  int someInt,
11        w = 5, x = 9, y = 2, z = 7;
13    char someChar = 'A';
15    cout << "tryIt3A output \n";
17    z += 3;
18    cout << z << "  " << z % w << endl;
20    z *= w + y;
21    cout << z << endl;
23    z -= 60.1;
24    cout << z << endl;
26    cout << (x-1) / (x-w) * y << endl;
28    cout << (x-1) / ((x-w) * y) << endl;
30    cout << static_cast<double>(x) / y << endl;
32    cout << x / y << endl;
34    cout << (w + x % 7 / y) << endl;
36    cout << (abs(y - w) + sqrt(x)) << endl;
38    someInt = someChar;
39    cout << someChar << "  "
40         << someInt << endl;
41    
42    return 0;
43 }



here are the error messages
1
2
3
4
Error	2	error C2668: 'sqrt' : ambiguous call to overloaded function	c:\...\...\...\lab3\lab3\lab3.cpp	36

3	IntelliSense: more than one instance of overloaded function "sqrt" matches the argument list:	c:\...\...\...\lab3\lab3\lab3.cpp	36
Last edited on
There are multiple versions of sqrt. Each take a different type.
One takes a double, another takes a float, and another takes a long double.

You are giving it 'x', which is an int. Since there is no version of sqrt which takes an int, the compiler does not know which version you want to call. The float version? The double version?

To fix this, you either need to give it one of the known types (ie: make 'x' a double instead of an int). Or you need to explicitly tell it which version you want to call (by casting 'x' to a double: sqrt((double)x)
Topic archived. No new replies allowed.