Toubles with my Velocity program

[# include <iostream.h>
# include <stdlib.h>
# include <math.h>
# include <fstream.h>

float t, s, vo;
const float g = 9.8;


int main()
{
ifstream outputFile;
outputFile.open("e:\\velocity.txt");

cout << "Enter initial velocity in m/s ";
cin >> vo;

t=0;
while (s<0)
{
s = ((vo * t) - ((1/2)* g * t * t));
cout << s << " " << t << endl;
outputFile << s << " " << t << endl; /* there is an error on this line, and I can't figure out why the outputFile won't compile*/
t++;

}
outputFile.close();

return 0;
}
] [/code]

I'm having trouble with this program. It is supposed to calculate the height of a projectile for every second until the height goes back down to 0. It isn't outputting anything to cout or the outputFile. I'm relatively sure the equation is correct, but maybe it's out of order? PLEASE HELP :(
Are you shooting this projectile straight up?
In that case the while loop is probably while (v !=0).

It's been a very long time since my high school physics..

Last edited on
s is undefined.
Sorry my bad - line InLight says - what happens if you initialise s to 0,
so s=0, t=0 ?
Hi twitch88,
The compilation error is due to:
U have created the file using "ifstream" and then u r using the same to write data into that file.
As ifstream is used to read data from a file and ofstream is used to write data into a file and fstream for both.

change the below line
from
ifstream outputFile;
to
ofstream outputFile;
or
 
fstream outputFile;


Hopefully this will work 4 U.
Last edited on
Also regarding logic.
I am confused a little bit.
U have initialized t=0 then ...
the below line
s = ((vo * t) - ((1/2)* g * t * t));
will always result in a value s = 0;

Again If u want to initialize s in loop it self then it will be better to use
1
2
3
4
do{
....
....
}while(s<0);


From the way your program is written (ie enter Vo), I'm assuming you're calculating when a person tosses an object up and it falls down.

Your loop should use this as a condition instead then.

1
2
3
4
5
6
7
S = 0;

//since it has to emerge above the S and come back down below the S.
while( S >= 0 )
{
...
}


You would also want to do a check to make sure the person doesn't type in a negative Vo.

Also g = 9.8 only if your intending to make up being negative and down being positive (which is contrary to how most people see this problem), ideally, g= -9.8
Topic archived. No new replies allowed.