Help with a simple input file

Hello, I'm very, very new at coding (so no big words please). I'm trying to work on a program for my C++ class which takes data stored in a file in the format:

ticketprice numberofticketssold

and outputs the total tickets sold and the total sale amount with two decimal places. There are four types of tickets and my .txt file is as follows:

250 5750
100 28000
50 35750
25 18750

When I run the program I get some ridiculously long number that isn't even close to what it should be. I've read the chapters relating to this and can't come up with what I'm doing wrong. Any and all feedback is greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	double ticket1Price, ticket2Price, ticket3Price, ticket4Price; 
	double ticket1Sold, ticket2Sold, ticket3Sold, ticket4Sold;
	double totalTicketsSold, totalSaleAmount;
	ifstream ticketData;

	ticketData.open("Ch3Ex5data.txt");

	ticketData >> ticket1Price >> ticket1Sold >> ticket2Price >> ticket2Sold >> ticket3Price >> ticket3Sold >> ticket4Price >> ticket4Sold;

	totalTicketsSold = (ticket1Sold + ticket2Sold + ticket3Sold + ticket4Sold);

	cout << fixed << showpoint << setprecision(2);
	cout << "Total number of tickets sold: " << totalTicketsSold << endl;

	totalSaleAmount = (ticket1Price * ticket1Sold) + (ticket2Price * ticket2Sold) + (ticket3Price * ticket3Sold) + (ticket4Price * ticket4Sold);

	cout << fixed << showpoint << setprecision(2);
	cout << "Total sale amount: " << totalSaleAmount << endl;
}
uhmm, try checking first if the file was successfully opened by :
if ( !ticketData ) { /* some msg */ }
Last edited on
Thanks! You were right - I assumed since I was getting some sort of numerical output the file was opening but something else was wrong. As I said, beginner stupidity. Thanks again.
Registered users can post here. Sign in or register to post.