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.
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
usingnamespace 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.
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.