Sign in to follow this  
yudhishthir

Problems using sqrt function

Recommended Posts

yudhishthir    100
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 function

using 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 acppchapter3
Error 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 this post


Link to post
Share on other sites
yudhishthir    100
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 this post


Link to post
Share on other sites
no such user    280
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 this post


Link to post
Share on other sites
Palidine    1315
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this