fstream question in program

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:


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

const g=9.8;

int main()
{
ofstream outfile( "table.txt", ios::on ); // is this supposed to be on or out?
double launch velocity, lv;
double time, t;
double 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 ^ t)))


outfile << h;


cout << "The position of the projectile during second intervals is .....";


outfil.close();

return 0;
}


I want the txt file that will be created to be called table. My problem is that I am not sure of what I need to add to have my equation write data to a txt file. And also if you see any other mistakes that I have made in the program or from the directions please tell me. I am a beginner so I might not understand all the lingo and more advanced stuff.

Please excuse the format of my program because I am not too sure how to put it in code form like it is supposed to, so if you can tell me that would be great.

Thank you
Last edited on
See http://www.cplusplus.com/reference/iostream/ofstream/ for the flags in the constructor. You might need only one parameter. Depends on what you want.

h = ((lv * t) - (.5 * g *(t ^ 2))) ; is missing. ^ is bit wise or. What you need is pow() http://www.cplusplus.com/reference/clibrary/cmath/pow/

[code]Your code[/code]
Last edited on
What you need is pow()


In this case, don't even bother. Just use t * t, pow is for tricky stuff like 2^(7.3)
Ok. I fixed the t in the equation. I already looked at those links, but since I am a beginner, they didn't make too much sense to me. I understand what the fstream function does, but I am confused on how to implement into the program to use the velocity input and equation to write to a file. Any one else want to help?
Anybody want to answer this question?
how to implement into the program to use the velocity input and equation to write to a file.

First figure out what data(s) you want to output, then figure out how you want to present it to the user, e.g taking newton's F = ma, we want to present it to the user how the F changes with the increase of mass and ask user for a starting value of m, and use a = 10m/s2:

Enter a start value for m: //this is std::cout
5
For m = 5 until m = 20, using a = 10ms^(-2): //std::cout until the end of loop
 m      F
 5     50
10    100
15    150
20    200

How you write it into your file is how you write it to screen using std::cout
closed account (zb0S216C)
const g=9.8;
This isn't quite complete.

double launch velocity, lv;
Something is missing between launch and velocity.

ofstream's are structures that output data to a file as standard. You could remove ios::on / ios::out and see if anything new arises.
The problem is I don't know how to write it onto a file. Can anyone help me with the questions in my main post and not just vaguely explain roundabout answers?
...vaguely explain roundabout answers?

1. "How you write it into your file is how you write it to screen using std::cout" I think this is pretty direct.

2. You've been here long enough already to know
- that people who respond to your posts will never give a complete solution to assignment questions
- there'll always be a reminder to post your codes to show what you've done/already knew

3. here's a (much) less roundabout way to explain #1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//using iostream
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello world" << endl; //print text to screen, start the next print at the next line
    cout << 123456789 << endl; //print numbers to screen
    return 0;
}

//using fstream
#include <fstream>
using namespace std;
int main()
{
    ofstream fout("out.txt"); //default mode is enough for most cases
    fout << "Hello world" << endl; //print text into file, start the next print at the next line
    fout << 123456789 << endl; //print numbers into file
    fout.close();
    return 0;
}

Last edited on
Topic archived. No new replies allowed.