C++ problem with the effects of gravity with a a tower

Hi, I'm trying to write a code to read the input of a towers height and then calculate how long it would take an object to hit the ground. I need to ask the user for what time intervals they would like(i.e every second, half-second, or tenth-second. It will then display the current height of the falling object as well as the time the object has fallen.
This is what I have so far and it doesn't want to compile. Can someone tell me and explain what I am doing wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

int main()
{
	const double gravity=9.80665;
	int tower_height;
	double time;
	double distance=((gravity*time*time)/2);
	double height= tower_height - distance;

	while(distance <= tower_height && distance >=0)
	{
		cout<<"Enter the height of the tower: "<<endl;
		cin>> tower_height;

		height = tower_height - distance;

		cout<<"The height of the object at "<<time<<"/n is"<<height<<'m'<<endl;
		time++;
	}
	system("pause");
}


What it is supposed to look like if
the tower height is 100m
and the time intervals is 1.0s

Time Height
0.00 100.00
1.00 95.10
2.00 80.39
3.00 55.87
4.00 21.55
Last edited on
What are your compile errors? This is the first thing you must tell us when you ask about errors.

I'm actually surprised it doesn't run with undefined behavior. It should run but maybe you compiler is saving you in this case. On line 9, when you initialize distance, what is distance going to be?
double time is not initialized but its used

double distance=((gravity*time*time)/2);

and yeah what are the errors you get?
Last edited on
Yes, why don't you initialize your variables before using them? It seems you are trying to tell the computer what you want it to do instead of how to do it... :)
You need to update distance *inside* the loop...
Last edited on
Topic archived. No new replies allowed.