Function returns 0

Hello. I'm new to this forum and to C++ programming as well. I'm also quite new in programming (only had some experience with VBA before).

I just started to learn C++ today. I know it is a really newbish question, but I can't manage to find what's wrong with this code. It's a simple program for calculating the height a free fall object was threw from if it took "t" seconds to reach the ground. I always get the value 0 out of the function, no matter what my input is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

double height(double t)
{
    std::cin >> t;
    return (1/2) * (9.81) * t * t;
}

int main()
{
    double t;
    std::cout << height(t) << std::endl;
    return 0;
}


Thanks,

Nick.
Last edited on
return (1/2) * (9.81) * t * t;

1/2 - these are both integers so you get an integer division result of 0. I'd change to either 1.0/2.0 or just .5
Last edited on
Whoops~ Thanks.
Topic archived. No new replies allowed.