LESSON 5 CSCI 15A Jim Murphy Let's look at the code on page 92 for the case study on height and distance traveled by a projectile starting at a given velocity and angle in degrees // Given an initial velocity, a takeoff angle, and using // an input value for seconds, this program determines // the height and distance (in meters) of a projectile. #include // for cout and cin #include // for sin, cos, and pow #include "ourstuff.h" // for decimals(cout, 1) int main( ) { // Acceleration due to gravity in meters/sec/sec const double gravity = 9.8; // Conversion factor from degrees to radians const double degToRad = 3.1415926 / 180.0; // Declare Objects double velocity; // Takeoff speed in meters/second double aDegrees; // Angle as input double aRadians; // Angle in its radian equivalent double seconds; // Time in flight in seconds double height, distance; // Compute and display // Input: cout << "Takeoff angle in degrees: "; cin >> aDegrees; cout << " Initial velocity (m/s): "; cin >> velocity; cout << " Seconds into the flight: "; cin >> seconds; // Process: Compute height and distance aRadians = aDegrees * degToRad; distance = (velocity * cos(aRadians)) * seconds; height = -0.5 * gravity * pow(seconds, 2) + (velocity * sin(aRadians)) * seconds; // Output: Display height and distance // First comment out // decimals(cout, 1); cout << endl << "After " << seconds << " seconds, the projectile is at (in meters)"< 0 ) { // Change the output stream to show decimal point and // n decimal places for any subsequent float or double. // Change the output stream to show decimal point and // n decimal places for any subsequent float or double. cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout.precision(n); } // Allow user to get a value to return // output stream to original state return oldFlags; } two other examples of ostream member functions include int ostream::width(int n); sets the next cout to use a minimum of n columns as x = cout.width(12); x then contains the old width The default width is 0 cout << 3.5 << “next” << endl; 3.5next x=cout.width(8); cout << 3.5 << “next” << endl; 3.5next cout.width(0); char ch = cout.fill ( '*' ); sets the leading fill character to * and saves the previous fill character in ch The default fill character is the space Some member functions are operators such as << and >> with different versions for each data type cin is in the istream class which gets input from the keyboard - but it is also possible to get data from a file on disk using the ifstream class which use the same operators as cin and is in the include file #include We can then construct such an object with cout << “try this \n\n” << “and this” << endl; try this and this ifstream inFile = "b:\\mydat\\bank.dat"; or in UNIX ifstream inFile = "~/15a/good.dat"; If the file is in the current directory then ifstream inFile = "good.dat"; // is sufficient Then we can use something like: inFile >> amount >> rate >> length; good.dat 106000 8.5 30 rather than the interactive into we have seen so much The file could have been prepared with an editor like pico good.dat spaces and newlines separate the data items Try modifying a previous program to use file input rather than the keyboard - notice your input section will not need cout prompts for the data and also notice that the data in the file must be in the same order as that expected by the inFile statement in the program