Suppose that you have the following statements: ofstream outfile; double distanc
ID: 3821753 • Letter: S
Question
Suppose that you have the following statements:
ofstream outfile;
double distance = 425;
double speed = 60;
double travelTime;
Write C++ statements to do the following:
a. Open the file travel.dat using the variable outfile.
b. Write the statement to format your output to two decimal places in fixed form.
c. Write the values of the variables day, distance, and speed in the file travel.dat.
d. Calculate and write the travelTime in the file travel.dat.
e. Which header files are needed to process the information in (a) to (d)?
Explanation / Answer
c++ program:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
double distance=425,speed=60;
ofstream outfile;
outfile.open ("travel.dat");
outfile <<"Distance="<<distance<<" ";
outfile <<"Speed="<<speed<<" ";
outfile.precision(3);
outfile <<"TravelTime="<<distance/speed<<" ";
outfile.close();
return 0;
}
travel.dat file:
Distance=425 Speed=60 TravelTime=7.083
a &d need #include <fstream> header file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.