Advertisement
M_A_Tabarani

School Stuff 08

Oct 4th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. //How to put a table
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <fstream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     ifstream infile("taimu.txt",ios::in);
  13.     ofstream outfile("height.out",ios::out);
  14.         const double gravity=9.8;
  15.         double velocity,time,height;
  16.  
  17.     cout<<"This table contain the list of the height of a projectile at specific time given in taimu.txt\n";
  18.     outfile<<"This table contain the list of the height at specific time given in taimu.txt\n\n";
  19.  
  20.     cout<<"Enter the lauch velocity in m/s : ";
  21.     cin>>velocity;
  22.  
  23.     outfile<<"Launch velocity is "<<velocity<<"m/s. \n";
  24.  
  25.     //Read the time from taimu.txt
  26.  
  27.     infile>>time;
  28.  
  29.     while (!infile.fail()) //True if the data is not valid or we misplace the file
  30.     {
  31.         height=velocity*time-gravity*pow(time,2)/2;
  32.  
  33.         outfile<<setw(7)<<time
  34.             <<setiosflags(ios::fixed)<<setprecision(2)<<setw(14)<<height
  35.             <<resetiosflags(ios::fixed)<<endl;
  36.  
  37.         cout<<setw(7)<<time
  38.             <<setiosflags(ios::fixed)<<setprecision(2)<<setw(14)<<height
  39.             <<resetiosflags(ios::fixed)<<endl;
  40.         infile>>time;
  41.     }//Exit the loop if the data is invalid or data has been used to calc
  42.         if(infile.eof())//If the data is valid and come to the end of the file
  43.         {
  44.             if(height<=0)
  45.             {
  46.                 outfile<<"The projectile has hit the ground."<<endl;
  47.             cout<<"The projectile has hit the ground."<<endl;
  48.             }
  49.             else
  50.             {
  51.             outfile<<"The projectile has not yet hit the ground."<<endl;
  52.             cout<<"The projectile has not yet hit the ground."<<endl;
  53.             }
  54.         }
  55.         else
  56.         {
  57.             outfile<<"Invalid data in file."<<endl;
  58.             cout<<"Invalid data in file."<<endl;
  59.         }
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement