Error in my code

Pages: 12
closed account (D4NbpfjN)
I am having some errors come up in my code. It is saying that my time is ambiguous. Is my code fine except that? I have included the format i have to go by and my code.


Suppose Dave drops a watermelon off a high bridge and lets it fall until it hits the water. If we neglect air resistance, then the distance d in meters fallen by the watermelon after t seconds is d=0.5 * g * t squared., where the acceleration of gravity g = 9.8 meters / second squared. Write a program that asks the user to input the number of seconds that the watermelon falls and the height h of the bridge above the water. The program should calculate the distance fallen for each second from t=0 until the value of t input by the user. If the total distance fallen is greater than the height of the bridge, then the program should tell the user that the distance fallen is not valid.

Sample Run 1:
Please input the time of fall in seconds:
2
Please input the height of the bridge in meters:
100

Time Falling (seconds) Distance Fallen (meters)
***********************************************
0 0
1 4.9
2 19.6

Sample Run 2

Please input the time of fall in seconds:
4
Please input the height of the bridge in meters:
50

Time Falling (seconds) Distance Fallen (meters)
***********************************************
0 0
1 4.9
2 19.6
3 44.1
4 78.4

Warning-Bad Data: The distance fallen exceeds the height of the bridge.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

double time;
const double Gravity = 9.8;

int main()
{
	int i;
	//int height;
	double distance = 0;
	double fallingDistance;


	while (time != -1)
	{

		cout << "Please enter the number of seconds the watermelon falls:";
		cin >> time;

		cout << "Please enter the height of the bridge above the water in meters: ";
		cin >> fallingDistance;


		for (i = 0; i<time; i++)
		{
			time = i + 1.0;
			cout << "Given gravity of, " << Gravity << " meters per second squared, an object will fall "
				<< distance << " meters in " << time << " seconds." << endl;
		}

		distance = 1 / 2 * Gravity * pow(time, 2);
		//distance = (0.5 * gravity * (seconds * seconds));



		cout << "Distance: " << distance << endl;

		if (distance > fallingDistance)
		{
			cout << " Warning Bad Data: The distance Fallen exceeds the height of the bridge. ";
		}


	}

	return 0;

}
closed account (D4NbpfjN)
the only error i have now is that it is saying time redefinition previous definition was function.
I get no such error when I compile what you posted.
closed account (D4NbpfjN)
i am getting the error that time is ambiguous?
Post the exact text of the error.
I suggest you rename your variable time to something else. There is a C standard function named time(), that is defined in the global namespace, and one of the header files you're including probably includes the header that is prototyping this standard function.

By the way if you weren't using those global variables you probably wouldn't be getting this error.


closed account (48T7M4Gy)
Yes, move line 6 inside main()
closed account (D4NbpfjN)
i put double time inside main(). Error i am getting now is uninitialized local variable 'time' used.
My code is posted below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;


const double Gravity = 9.8;

int main()
{
	int i;
	//int height;
	double distance = 0;
	double fallingDistance;
	double time;



	while (time != -1)
	{

		cout << "Please enter the number of seconds the watermelon falls:";
		cin >> time;

		cout << "Please enter the height of the bridge above the water in meters: ";
		cin >> fallingDistance;


		for (i = 0; i<time; i++)
		{
			time = i + 1.0;
			cout << "Given gravity of, " << Gravity << " meters per second squared, an object will fall "
				<< distance << " meters in " << time << " seconds." << endl;
		}

		distance = 1 / 2 * Gravity * pow(time, 2);
		//distance = (0.5 * gravity * (seconds * seconds));



		cout << "Distance: " << distance << endl;

		if (distance > fallingDistance)
		{
			cout << " Warning Bad Data: The distance Fallen exceeds the height of the bridge. ";
		}


	}

	return 0;

}
closed account (48T7M4Gy)
time = 0;
Last edited on
closed account (48T7M4Gy)
Also change time to some other name eg timezzz as suggested earlier
closed account (D4NbpfjN)
my program is not doing the calculation correctly. For example when i enter 4 for seconds and 50 for the height it is just giving me 0 meters in 1 second. I have posted my current code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;


const double Gravity = 9.8;

int main()
{
	int i;
	//int height;
	double distance = 0;
	double fallingDistance;
	double TIME=0;



	while (TIME != -1)
	{

		cout << "Please enter the number of seconds the watermelon falls:";
		cin >> TIME;

		cout << "Please enter the height of the bridge above the water in meters: ";
		cin >> fallingDistance;


		for (i = 0; i<TIME; i++)
		{
			TIME = i + 1.0;
			cout << "Given gravity of, " << Gravity << " meters per second squared, an object will fall "
				<< distance << " meters in " <<TIME << " seconds." << endl;
		}

		distance = 1 / 2 * Gravity * pow(TIME, 2);
		//distance = (0.5 * gravity * (seconds * seconds));



		cout << "Distance: " << distance << endl;

		if (distance > fallingDistance)
		{
			cout << " Warning Bad Data: The distance Fallen exceeds the height of the bridge. ";
		}


	}

	return 0;

}
closed account (48T7M4Gy)
line 36 is in the wrong place. distance is not being continually updated as time goes by.
closed account (D4NbpfjN)
so line 36 needs to be at the end of the program right?
closed account (48T7M4Gy)
Why at the end? Did you try it? What answers do you get?
closed account (48T7M4Gy)
Also use distance = 0.5 * gravity * i * i; because 1/2 = 0 as a result of integer division. That will enable you to also remove the line #include<cmath>
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < TIME; i++)
{
   // 1. Calculate current distance fallen
   distance = 0.5 * Gravity * ???? * ????;
		    
   // 2. Check distance fallen against limit, announce bad data and break out of loop if exceeded
   ???????

   // 3. Display distance fallen
   ???????
}
Last edited on
closed account (D4NbpfjN)
I have entered in the above statements. So let me ask this on # 2 when it check distance fallen i need to enter in a formula for this? And same goes with #3?
closed account (48T7M4Gy)
Show me what you have. I am surprised you can't fill in the ??????'s from what you have already done.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/5158/
closed account (D4NbpfjN)
I have changed my code around some. Here is my current code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<iomanip>
using namespace std;


const double Gravity = 9.8;

int main()
{
	int i;
	//int height;
	double distance = 0;
	double FallingDistance;
	double TIME=0;



	while (TIME != -1)
	{

		cout << "Please enter the number of seconds the watermelon falls:";
		cin >> TIME;

		cout << "Please enter the height of the bridge above the water in meters: ";
		cin >> FallingDistance;
		
	for (int i = 0; i <= FallingDistance; i++)
	{
		distance = 9.8*(i*i) / 2.0;
				
		cout << i << " " << distance << endl;
				
		}
		
			if (distance > FallingDistance)
			{
				
					cout << "Warning-Bad Data: The distance fallen exceeds the height of the bridge." << endl;
			}

		}


	

	return 0;

}
Pages: 12