[# 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 :(
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
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
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