Need to turn program in soon/loop Quest.

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.


My program so far is:



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


const int g = 9.8;

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

cout << "Enter speed of launch velocity(m/s) and receive the height after a certain number of seconds.      => ";
cin >> lv;


        h = ((lv * t) - (.5 * g *(t ^ 2))) << endl;

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


outfil.close();

return 0;
}



I am trying to have it create a txt file called table that shows the different heights and times when you input a specific initial velocity. I am stuck right now on what to do next so any help will be appreciated.
Last edited on
You have some arbitrary " << endl" at the end of the line that doesn't belong there.
Ok. I fixed that foolish mistake. The program is able to compile and run, but just not how it is supposed to.
I am trying to have it create a table that lists all of the seconds from when the initial height is 0 and the ending height is 0. Like this:

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


Can anyone enlighten me?
- line 6: your program will only use the integer portion of 9.8 i.e 9

- line 11: you don't need two variables for launch velocity, choose one -the compiler should give you a warning, same with line 13

- line 13: your calculation results should be in fractional/floating point values, so instead of outputting 9.85 it'll output 9 for a value of h/height

Can anyone enlighten me?

Use loop/control structures: http://www.cplusplus.com/doc/tutorial/control/
Ok. I changed the const int to const double to fix the integer portion for 9.8, changed line 11, and changed the height to float to keep the correct decimal places.

I am still kind of confused on how to do a loop structure to write a file to a table where it only shows the time periods between where the projectile launches off at 0 and when it lands at height=0. My function works where it outputs data to the txt file, but it only does 1 set of units right now. I just seem to not be able to wrap my head around this.

Need help badly!
Last edited on
anybody?
warscoten wrote:
I am still kind of confused on how to do a loop structure to write a file to a table

c'mon, at least you must have done something like this:
1
2
3
4
5
6
int n = 10;
while(n > 0)
{
    cout << "n: " << n << " n -1: " << n - 1 << endl;
    n--;
}
I understand the basic setup of a loop, 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.

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.
Last edited on
And also how do you set up a table from your original program that is like the third post on this forum?

My program so far, but still a lot of errors and unsureness:

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 need help making a loop. My post above this explains more in detail.
Last edited on
anybody there to help?
Bumping your post is generally frowned upon, several of us do look for posts in which no one has responded.

That said you never set 't' to any value so how is your application supposed to know what it is?

You also never change the value of 't' so your loop never ends since t is always the same value compared to 'h'.

Change your for loop to:
 
for(t = 0; t < h; ++t)

WTH is 'h' supposed to be anyway, since you seem to already have a variable for height? Why would this formula be over when 't' is greater then or equal to 'h'? This doesn't make sense to me, maybe recheack your math or else state in terms you understand better what the formula you're trying to use is.
My loop is obviously wrong. I understand the basic setup of a loop, 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.

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.
use break; to get out of the loop if h is less than 0 :\ (sorry, I can't think of another way to end this thread)

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.

next time, simplify your thoughts. stating your case clearly and precisely is a lost art :D
Those variables aren't what I would call "random" I may be being pedantic if that's not really what you meant but you can calculate everything in this problem if you're given initial velocity and you assume the angle of ascent is normal to ground.

I belive your math is wrong, at least it is in the way you are trying to code it. "Time" "Height" and "Velocity" should all change with each iteration of this loop, but you're not doing any of that with what you have here so far.

You will also need a second loop to display your results so you will probably need a vector to store them. I would suggest making a struct to hold the data but that's because I encourage showing off as much as you can :). That is unless you want to simply fill out the table as you calculate the results, which is boring but would still get the job done.
Topic archived. No new replies allowed.