Public Group

# Problems using sqrt function

This topic is 2810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone,
I started learning C++ 3 days ago. I try and do at least an hour of programming everyday and lots of programming problems. So i tried to solve this problem

Quote:
 Braking distance of some vehicle at speed of 100 km/h is equal to 48 meters. Assuming that other conditions has not been changed (friction, tyres, breaking system and so on) one can estimate that the breaking distance is proportional to the square of the speed of the vehicle. Please compute the vehicle's breaking distance for a given speed X.

by writing this program:
/*	title: C++ programming excercises		date 11/08/2010*/#include<iostream>	#include<cmath>		//lets me use sqrt functionusing std::cin;using std::cout;using std::endl;using std::domain_error;using std::sqrt;int main(){	cout<<"Please enter your current speed"<<endl;	unsigned short int speed;	//tried this as float speed but no luck                	cin>>speed;	//test to see if speed value is correct	if(speed<=0||speed>250){		throw domain_error("not a valid speed");		                       }		float breakingDistance=.0048(*sqrt(speed));	cout<<"Your breaking distance is: "<<breakingDistance<<endl;	system("pause");	return 0;}

I am getting the following errors in VS 2010 professional
Quote:
 Error 1 error C2100: illegal indirection C:\c++programs\acppchapter3\acppchapter3\main.cpp 27 1 acppchapter3Error 2 error C2064: term does not evaluate to a function taking 1 arguments C:\c++programs\acppchapter3\acppchapter3\main.cpp 27 1 acppchapter3 3 IntelliSense: expression must have (pointer-to-) function type c:\c++programs\acppchapter3\acppchapter3\main.cpp 27 25 acppchapter3 4 IntelliSense: more than one instance of overloaded function "sqrt" matches the argument list: c:\c++programs\acppchapter3\acppchapter3\main.cpp 27 32 acppchapter3

I have read over the c++ reference of sqrt and other examples that popped up after a google search. What am i doing wrong?
Thanks.

##### Share on other sites
I think you want .0048 * sqrt(speed). You've got an extra set of parenthesis that are confusing the compiler.

##### Share on other sites
I added those thinking that might solve the problem.
I still get these errors sans parenthesis
Quote:
 Error 1 error C2668: 'sqrt' : ambiguous call to overloaded function C:\c++programs\acppchapter3\acppchapter3\main.cpp 27 1 acppchapter3 2 IntelliSense: more than one instance of overloaded function "sqrt" matches the argument list: c:\c++programs\acppchapter3\acppchapter3\main.cpp 27 31 acppchapter3

##### Share on other sites
There are three versions of sqrt(): one take a float, one taking a double and one taking a long double. Starting from a short int the compiler doesn't know which one to use. Either change your variable type from an int to one of those types or use a cast to tell the compiler specifically which one you want.

##### Share on other sites
sqrt takes either a float or a double. You're passing an unsigned short, so the compiler doesn't know if you want that converted to a float or a double.

Options:
1) Do a manual cast to either float or double
2) read in speed as a float or double

-me

Thanks.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002131
×