Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //How to put a table
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <cmath>
- using namespace std;
- int main()
- {
- ifstream infile("taimu.txt",ios::in);
- ofstream outfile("height.out",ios::out);
- const double gravity=9.8;
- double velocity,time,height;
- cout<<"This table contain the list of the height of a projectile at specific time given in taimu.txt\n";
- outfile<<"This table contain the list of the height at specific time given in taimu.txt\n\n";
- cout<<"Enter the lauch velocity in m/s : ";
- cin>>velocity;
- outfile<<"Launch velocity is "<<velocity<<"m/s. \n";
- //Read the time from taimu.txt
- infile>>time;
- while (!infile.fail()) //True if the data is not valid or we misplace the file
- {
- height=velocity*time-gravity*pow(time,2)/2;
- outfile<<setw(7)<<time
- <<setiosflags(ios::fixed)<<setprecision(2)<<setw(14)<<height
- <<resetiosflags(ios::fixed)<<endl;
- cout<<setw(7)<<time
- <<setiosflags(ios::fixed)<<setprecision(2)<<setw(14)<<height
- <<resetiosflags(ios::fixed)<<endl;
- infile>>time;
- }//Exit the loop if the data is invalid or data has been used to calc
- if(infile.eof())//If the data is valid and come to the end of the file
- {
- if(height<=0)
- {
- outfile<<"The projectile has hit the ground."<<endl;
- cout<<"The projectile has hit the ground."<<endl;
- }
- else
- {
- outfile<<"The projectile has not yet hit the ground."<<endl;
- cout<<"The projectile has not yet hit the ground."<<endl;
- }
- }
- else
- {
- outfile<<"Invalid data in file."<<endl;
- cout<<"Invalid data in file."<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement