Not sure what is wrong with this thing.

I am getting an error on line 31 and I cannot figure out what is wrong. Also when I need to go in a retype something or add something in the middle of a statement it erases what was already there instead of adding the typed letters to it. Please help.
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
54
 This program will use the equation d=1/2gt^2 to calculate 
the falling distance of an object. The function is called 
fallingDistance and accept an objects falling time as an 
argument.*/

#include <iostream>
#include <iomanip>  //Needed for set precision and setw functions.
#include <cmath>    // Needed for the pow function

using namespace std;

//Function Prototype.
double fallingDistance(double time);

int main()         // Main function. The main body of the program.
{
	//Variables.
	double g = 9.8, t = 1, d;
	char again;

	do
	{
		//Cout statements.
		cout << "Falling Distance" << endl;
		cout << "Time      Distance" << endl;
		cout << "-------------------" << endl;

		//For loop to iterate each time to calculate the distance
		for (t = 1; t < 10; t++)
		{
			cout << setw(3) << t << setw(10) << fallingDistance(time) << endl;
		}

		//Asks the user if they want to try again.
		cout << "Would you like to try agian? (y or n)";
		cin >> again;
	} while (again == 'Y' || again == 'y');

	system("pause");
	return 0;
}

/**********************************************************************
*                   fallingDistance Function                          *
* This function will calculate the distance an object has fallen in a *
* a given time. It uses the equation d=(1/2)gt(^2). Where d=distance, *
* g=9.8, and t=time. And returns the distance = d.                    *
**********************************************************************/
double fallingDistance(double time)
{
	double  d, t = 1;
	d = 1 / 2 * 9.8 * pow(t, 2);
		return d;
}
I am getting an error on line 31 and I cannot figure out what is wrong.

What is time? You have not declared any variable in main() with such a name.

Also when I need to go in a retype something or add something in the middle of a statement it erases what was already there instead of adding the typed letters to it.

Look for a key on the keyboard that says "Insert". Press it (You might have to turn off Num Lock first).

I think he is using time in the same context as you would srand, which in that case if you were using that then it expects a function parameter.

http://www.cplusplus.com/reference/ctime/time/

Normally passing a null value like time(0)
Last edited on
I think he is using time in the same context as you would srand, which in that case if you were using that then it expects a function parameter.


Since the function in question takes a double, that doesn't seem likely.
double fallingDistance(double time)

OP should realize that 1/2 in the fallingDistance function is integer math, so d will always be 0. Make it 1.0 / 2.0 * 9.8 .... You may also want to actually use the parameter you pass in.
Last edited on
Peter87 thank you so much!

cire you are awesome thank you for the final fix. Progam does the math correctly. I knew the equation didn't look right...lol
Topic archived. No new replies allowed.