C++ File io You will write a program that reads a data file. The data file conta
ID: 3567579 • Letter: C
Question
C++ File io
You will write a program that reads a data file. The data file contains ticket IDs and ticket prices. Your job is to read these tickets (and prices). find minimum, maximum and average ticket prices and output a report file. The report file should look exctly (or better than) the one attached (output.txt). I also attached the sample input file you should be reading in. User does not enter any information to your program via keyboard. You read the information inside data.txt file and produce an output file similar to the one provided
below is the data.txt file:
this is copied and pasted from the output.txt file (see below):
Let me know if you have any other questions. Thanks!
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream infile;
infile.open("data.txt");
char ticketId[4];
double price;
double max = 0,min = 999999999,sum = 0;
int count = 0;
while(!infile.eof()){
infile >> ticketId;
infile >> price;
sum += price;
count++;
if(price > max){
max = price;
}
if(price < min){
min = price;
}
}
infile.close();
ofstream outfile;
outfile.open("output.txt");
outfile << "******************************************** ";
outfile << " TICKET REPORT ";
outfile << "******************************************** ";
outfile << "There are " << count << " tickets in the database ";
outfile << "Maximum Ticket price is $" << max << endl;
outfile << "Minimum Ticket price is $" << min << endl;
outfile << "Average Ticket price is $" << sum/count << endl << endl;
outfile << "Tahnsk you for using our ticket system! ";
outfile << "*********************************************** ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.