hi guys im new to c++ can i have some assist ?

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
250 5750
100 28000
50 35750
25 18750

output the number of tickets sold and the total sale amount. Format your output with two decimal places.

my current file for SampleData.txt
250 5750
100 28000
50 35750
25 18750

my file for ReformatData.dat
Number of tickets sold = 0
Sale amount = $0

Now i couldnt get the numbers of tickets sold and sale amount. may i have some assist on where should i change ? thanks in advanced!

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{

 double tPrice;
 int numOfTicketsSold=0;
 int ticketsSold;
 double totalSale=0;

 ifstream inFile;
 ofstream outFile;

 inFile.open("SampleData.txt");
 outFile.open("ReformatData.dat");

outFile << fixed << showpoint << setprecision(2);
 cout << "wait " << endl;
 for (int i=0; i<4; i++)
 {
     inFile >> tPrice >> ticketsSold;

     totalSale=tPrice*ticketsSold;
     numOfTicketsSold=numOfTicketsSold+ticketsSold;

 }

 outFile << "Number of tickets sold = "  << numOfTicketsSold << endl;

 outFile << "Sale amount = $" << totalSale << endl;

 inFile.close();
 outFile.close();

 return 0;
}

Simple two-dimensional matrix. First column is the ticket price. Second is the number of tickets were sold at that price. To compute the income, iterate through the matrix, row by row, multiplying the price by number of tickets. Got it? If not, go back to your basic maths.

And no, I am not going to correct your code. This is too simple of a problem, and you should have no problem solving it!

commented: Not helpful -3

No need for an array, just read each line from the file, calculate the price x num tickets, add that to a total that you initialise before starting to read the file,

I don't know if you guys noticed i post my code up there is to see where my mistake went. and this isn't about using array. the reason why im posting this is because the calculation of it does not went into my "FormatData.dat" file. therefore correcting where my mistakes are in the code is more useful than criticizing me.

One of your problems is here:

 totalSale=tPrice*ticketsSold;

You're overwriting totalSale instead of adding to it:

 totalSale += tPrice * ticketsSold;

closed thread. solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.