trouble with pow function

Hello, I am CS student at University.

I am trying to write program to find time it takes for metal spheres to reach the ground. The equation is (2h/g)^(1/2)

When I try to use pow function for this equation it does not recognize.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main ()
{
    const float g = 9.81;
    float height_sphere1, height_sphere2, height_sphere3;
    cout << "Enter height for the first sphere: ";
    cin >> height_sphere1;
    cout << "Enter height for the second sphere: ";
    cin >> height_sphere2;
    cout << "Enter height for the third sphere: ";
    cin >> height_sphere3;
    cout << endl;
    cout << "The time for the first sphere to fall is " << ( 2 * height_sphere1 / g ) pow (1/2);

    system ("pause");
    return 0;
}


When I try to build solution I get these errors

1
2
3
1>d:\Nikolay\cs\lab3\lab3\p1.cpp(9): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>d:\Nikolay\cs\lab3\lab3\p1.cpp(18): error C2146: syntax error : missing ';' before identifier 'pow'
1>d:\Nikolay\cs\lab3\lab3\p1.cpp(18): error C2661: 'pow' : no overloaded function takes 1 arguments


I tried include <cmath> and even <math.h> but still does not work.

Please help to tell me what I am doing wrong all help much appreciated
pow is not an operator, it is a function:
http://www.cplusplus.com/reference/clibrary/cmath/pow/

What's more, division of two integers will result in another integer - that means 1/2 will result in 0. So you should either write 0.5 or 1.0/2 or use sqrt instead, which is faster.

Also, it's better to avoid float unless you have special requirements in terms of execution speed and memory usage.
double provides much higher precision than float at little additional cost.
I'm pretty new to this but wouldn't it be far neater to declare a class with each successive sphere being a member of that class? How about something like this (Constructive criticism please - I just started learning):


#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;

const double g = 9.81;

class Sphere {
double dropheight;
public:
Sphere (double);
double falltime ()
{return ( (2/g) * pow (dropheight, (0.5)));}
};

Sphere::Sphere (double a) { dropheight = a;}

int main () {

double height;

cout << "Enter height for the first sphere: ";
cin >> height;
Sphere first (height);
cout << "The time for the first sphere to fall is " << first.falltime() << endl;

cout << "Enter height for the second sphere: ";
cin >> height;
Sphere second (height);
cout << "The time for the first sphere to fall is " << second.falltime() << endl;

cout << "Enter height for the third sphere: ";
cin >> height;
Sphere third (height);
cout << "The time for the first sphere to fall is " << third.falltime() << endl;

return 0;
}

Topic archived. No new replies allowed.