Why is the answer always zero?

This is a textbook project, but isn't a school assignment.

I've included a comment within the program that describes the expectations of the project. Why is the answer always zero?

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
/*...program that allows user to enter a time in seconds and then outputs
how far an object would drop if it is in freefall for that length of time.  
Assumes there is not friction or resistance from the air, and a constant
acceleration of 32 feet per second
uses the equation:
     distance = (acceleration * time^2)/2
*/

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    
   double time = pow(time, 2.0);
   double acceleration = 32; //feet per second
   double distance = (acceleration * time)/2;

      cout << "This is a program that calculates how far an object will fall in" << endl;
      cout << "user specified seconds." << endl;
      cout << "Enter the number of seconds." << endl;
      cin >> time;
      cout << endl << distance;

   char letter;
   cout << endl << "Enter a letter to end the program" << endl;
   cin >> letter;

   return (0);
}


I am working on my analytic/problem solving/critical thinking skills, so if you can resist, don't solve it for me. I'd rather have a hint or suggestion.

Thank you!
Well, you're trying to calculate the result before the input was made, so this can't work.
OMG, duh. Let me move it around and see what happens. I was trying to be so neat with my form . . . I put everything in the wrong place. Thank you!
Last edited on
OK, it seems to work now. Thanks to Athar.

I also made some changes in the expressions to fix the calculations.

Is this good style? I know there would be some opening comments like author/purpose/usage. What comments would be useful additions to the program?

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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    

double acceleration = 32; //feet per second
double time;

       cout << "This is a program that calculates how far an object will fall in" << endl;
       cout << "user specified seconds." << endl;
       cout << "Enter the number of seconds." << endl;
 
       cin >> time;
       
double timePow = pow(time, 2.0);
double distance = (acceleration * timePow)/2;

       cout << endl << distance;

char letter;

       cout << endl << "Enter a letter to end the program" << endl;

       cin >> letter;

return (0);
}
Topic archived. No new replies allowed.