Okay, so the assignment is to take input from a file named tickets.txt and output the total number of tickets sold, as well as the total revenue. The values in the text file are input in two columns (1) number of tickets sold, and (2) price of each ticket category.
The text file looks like:
250 5750
100 28000
50 35750
25 18750
This is the code that I came up with...
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
/* Program Name: Ticket_Sales.cpp
* Date: May 2, 2018
* Purpose: Calculate Total Ticket Sales
*/
int main() {
{
ifstream inFile;
float ticket_cost;
float tickets_sold;
float total_cost ;
float total_sold ;
float ticket_revenue;
/*
* I noticed someone doing something similar to this on StackExchange; seems absolutely brilliant to include
* a manner to check if the program is accessing the file.
*/
inFile.open("C:\\Users\\pinkp\\CLionProjects\\M05_Ticket_Sales\\tickets.txt");
if (inFile.fail()) {
cout << "Failed to open the file." << endl;
cin.get ();
return 1;
}
while(inFile >> tickets_sold >> ticket_cost) {
total_sold += tickets_sold;
total_cost += ticket_cost;
ticket_revenue = (total_cost * total_sold);
}
cout << fixed << setprecision(2);
cout << "Total Tickets Sold: " << total_sold << "." << endl;
cout << "Total Revenue: " << ticket_revenue << "." << endl;
}
/*
* The value continues to return 2 less than the intended output, and I cannot pinpoint the exact cause.
*/
return 0;
}
But my output for ticket_revenue continues to give me 37506248.00, when inputting the numbers into a calculator manually will give you 37506250.00. A two number discrepancy. I thought about just doing a '+2' to get it up to the right number, but I figured my professor would frown on that.
Regardless of the numbers in the tickets.txt (I've changed them around a few times), it will always be two less than what I am expecting. Is this some super easy C++ or programming concept that I've somehow missed?
Thank you!
You failed to initialize ticket_revenue
- and look how it is assigned:
ticket_revenue = (total_cost * total_sold);
This overwrites ticket_revenue
with the product of the total number of tickets sold and the sum of all the prices.
You need something like (untested):
unsigned long total_sold = 0;
unsigned long ticket_revenue = 0;
while (inFile >> tickets_sold >> ticket_cost) {
auto transaction_revenue = tickets_sold * ticket_cost;
total_sold += tickets_sold;
ticket_revenue += transaction_revenue;
}
BTW, don't use floating-point types for currency calculations!
Here's a version of your code with the above errors fixed, and without the external input file (so that it stands as a Minmal, Complete and Verifiable Example):
#include <iostream>
#include <vector>
int main()
{
static const
std::vector<std::pair<int, long>> sales =
{{ 250, 5750 },
{ 100, 28000 },
{ 50, 35750 },
{ 25, 18750 } };
long total_sold = 0;
long ticket_revenue = 0;
for (auto& sale: sales) {
int tickets_sold = sale.first;
long ticket_cost = sale.second;
total_sold += tickets_sold;
ticket_revenue += tickets_sold * ticket_cost;
}
std::cout << "Total Tickets Sold: " << total_sold << ".\n";
std::cout << "Total Revenue: " << ticket_revenue << ".\n";
}
This produces the correct answer:
Total Tickets Sold: 425.
Total Revenue: 6493750.
I suppose that instead
while(inFile >> tickets_sold >> ticket_cost) {
total_sold += tickets_sold;
total_cost += ticket_cost;
ticket_revenue = (total_cost * total_sold);
}
you should write
ticket_revenue = 0;
while(inFile >> tickets_sold >> ticket_cost) {
ticket_revenue += ticket_sold * ticket_cost;
}
Otherwise you sum number of tickets and cost of tickets and you should get a bigger value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With