Need to finish program! Due soon! Need guidance

I have been trying for a while to figure out how to put my program together where it writes to a file the height of a projectile.

The exact problem is stated as this: Write a program that writes to a file a table that shows the height of a projectile launched straight up for each second from launch time (time zero) until the projectile hits the ground. The last entry in the table should show a projectile height of 0. The height after t seconds is given by: s = (in. velocity)* t - .5gt^2 where g is 9.8m/s^2.
The program should prompt the user for the launch velocity.
I am trying to have it look like this table:

Height     Time
  0.0         0
  55.1        1
 ... and so fourth


I understand that I need a loop in my program, but I am not sure how to create it where it depends on what value you input in for launch velocity. Like if you input 60, it shows seconds and heights for something like 16 second intervals. But if you input another number for launch velocity, you receive more or less table data for heights and time.

So in my case, I need height to show only positive integers based on my initial velocity input. I am just lost on how to correlate these things into a loop that writes it to a file since I have two random variables in time and height that correlate of each other.

Here is my program so far:


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


const double g = 9.8;

int main()
{
ofstream outfil( "table.txt", ios::out);
double launchvelocity, lv;
int time, t;
float height, h;


cout << "Enter speed of launch velocity(m/s)   => ";
cin >> lv;

for ( t >= 0 ;t < h; ){

        h = ((lv * t) - (.5 * g *(t * t)));


outfil << "Height = " << h << "   Time = " << t << endl;
}


outfil.close();

return 0;
}


I have been working on this for a couple of days now and now it is due today. So help will be much appreciated.
Last edited on
you could try a do-while-loop

1
2
3
4
5
6
7
8
h = 0.0;
t = 1;
do {
    h = ....;
    ++t;
} while (h >= 0.0);
if (h < 0.0)
    h = 0;
Last edited on
Ok. I added the do-while loop into it, but in my table.txt file that it outputs the data to, I only get one set of data which is: when i input 60 for initial velocity
height =0 Time =14
I am not sure how to make it fill out a whole table like I wanted in my original post. Can someone explain to me what I need to add or change to my 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
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
using namespace std;


const double g = 9.8;

int main()
{
ofstream outfil( "table.txt", ios::out);
double launchvelocity, lv;
int time, t;
float height, h;


cout << "Enter speed of launch velocity(m/s)   => ";
cin >> lv;

h = 0.0;
t = 1;
do {

        h = ((lv * t) - (.5 * g *(t * t)));
        ++t;
} while (h >= 0.0);
if (h < 0.0)
        h = 0;


outfil << "Height = " << h << "   Time = " << t << endl;



outfil.close();

return 0;
}


well, thats because you just write the values once.

if you want to write all the value pairs, you have to put the outfil << ... stuff back in the loop (as you had before). and put the if-statement in it too, because you can have negative h if you dont check in the loop.

i was just giving an example how to make a working loop, leaving the rest to you ;)

btw, why do you declare launchvelocity, time, height? dont declare variables you dont use
Last edited on
I don't really understand when you say that I just write the values once, any way you can explain further. I understand that I should not declare the other variables and that I need to put the outfil << stuff back into the loop, but I am just confused on what to write into the loop so that it calculates the height at certain second intervals when you input an initial velocity and outputs the data into a txt file. Man am I confused
I don't really understand when you say that I just write the values once

Look at where you're writing to outfil. It's right before you close the file. How many times do you think it's going to be executed?

If you want to write each time,value pair to the file, put the write where mathes told you.
I fixed the outfil part, so it creates a table in a txt file like it is supposed to. But now I don't know how to fix my loop so that it outputs the right numbers. It is outputted the correct set, but the numbers are off. It should be

Height=0 Time=0
55.1 ------ 1
100.4 ----- 2
0 ----------13

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


const double g = 9.8;

int main()
{
ofstream outfil("table.txt", ios::out);
double launchvelocity, lv;
int time, t;
float height, h;


cout << "Enter speed of launch velocity(m/s)   => ";
cin >> lv;

h = 1;
t = 1;
do {
        h = ((lv * t) - (.5 * g *(t * t)));
        ++t;
outfil << "Height = " << h << " Time = " << t << endl;
} while (h >= 0.0);

if (h < 0.0)
        return  h = 0;



outfil.close();

return 0;
}


This is what you get in the txt file:

1
2
3
4
5
6
7
8
9
10
11
12
13
Height = 55.1 Time = 2
Height = 100.4 Time = 3
Height = 135.9 Time = 4
Height = 161.6 Time = 5
Height = 177.5 Time = 6
Height = 183.6 Time = 7
Height = 179.9 Time = 8
Height = 166.4 Time = 9
Height = 143.1 Time = 10
Height = 110 Time = 11
Height = 67.1 Time = 12
Height = 14.4 Time = 13
Height = -48.1 Time = 14
swap line 23 and 24. you calculate h with t, increase t and write all to the file.
so your t is one to high in every line of you output file.

then remove line 27 and 28.
and since you get a negative height in your output file, put the if (h < 0.0) h=0.0; right after your calculation of h

Last edited on
And it should be
while (h > 0.0);
Topic archived. No new replies allowed.