The manager of a football stadium wants you to write a program that calculates t
ID: 3665579 • Letter: T
Question
The manager of a football stadium wants you to write a program that calculates the total ticket sales after each game. There are four types of tickets-box, sideline, premium, and general admission. After each game, data is stored in a file in the following form: ticketPrice numberOfTicketsSold ... Sample data are shown below: 250 5750 100 28000 50 35750 25 18750 The first line indicates that the ticket price is $250 and that 5750 tickets were sold at that price. Output the number of tickets sold and the total sale amount. Format your output with two decimal places. Note that you are required to read from and write to .txt files.Explanation / Answer
code:
#include <iostream>
#include <cstdio>// to use sprintf to output total sales upto two decimal points
#include <fstream>// to read and write to .txt files
using namespace std;
int main()
{
float price, totalSales =0.00;
int ticketCount, totalTickets = 0;
char ts[25];
ifstream in("input.txt"); // open input.txt file as input stream
ofstream out("output.txt"); // open output.txtx as output stream
while(in>>price>>ticketCount) // while the end of file read price and ticket count
{
totalSales = totalSales + (price * ticketCount); // calculate total sale from this tickets and add it to total price
totalTickets = totalTickets + ticketCount; // add the number of tickets of this type to total number of tickets
}
sprintf(ts,"%.2f",totalSales); // use sprintf to copy the total sales with two decimal prcition to a string
out<<ts<<" "<<totalTickets<<endl; // write output to a file
}
input.txt:
250 255
1.2317 5
output.txt:
63756.16 260
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.