inData/outData

Hey guys, currently learning the basics. Got a project we we have to write for class and I've got stuck. I wanted to improve the base concept and make it more functional. Currently we need to take the # of tickets and number and create a cost. I know I can do it if I write a variable for each line from the inData,but I'd like to write so it could either take 1 line from the inData or infinity. thanks guys.

Heres my code.


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
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{	
	double ticketPrice;
	int numberofTicketsSold;
	double totalTickets;
	int i;// number of time to write to outData
	int k=0;// number of lines inData
	ifstream inData;
	ofstream outData;

	//files to open/close
	inData.open("ticketssales.txt");
	outData.open("ticketsalesreport.txt");
	//loop for var k
	while(!inData.eof())
{k++;
	inData >> ticketPrice >> numberofTicketsSold;
}
	totalTickets = (ticketPrice * numberofTicketsSold);

	cout << ticketPrice << " " << numberofTicketsSold << " " << totalTickets; //used for debugging 

	outData << "#Tickets" << " " << "$Tickets" << " " << "Total" << endl;//header for outdata
	for(i=0; i<k; ++i)//loop for outData
		outData <<  ticketPrice << "	"<< numberofTicketsSold << "	" << totalTickets  << endl;
	
	



	system("pause");
	return 0;
}

========
here is the indata

50 2500
100 1000
2900 200
10000 50
Last edited on
1
2
3
4
5
while(!inData.eof())
{k++;
inData >> ticketPrice >> numberofTicketsSold;
}
totalTickets = (ticketPrice * numberofTicketsSold);

Each time though the loop, you're overwriting the values of ticketPrice and numberofTicketsSold. When you exit the loop, those variables contain only the respective values of the last record read.

1
2
for(i=0; i<k; ++i)//loop for outData
outData << ticketPrice << " "<< numberofTicketsSold << " " << totalTickets << endl;

You're going to print the same (last) value of ticketPrice and numberofTicketsSold each time through the loop.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Last edited on
Sorry fixed the format. But how could I get it to print an outdata for each line?
Thanks for adding code tags.

Move your calculation and print startement inside the loop.
1
2
3
4
5
  while(!inData.eof())
  {  inData >> ticketPrice >> numberofTicketsSold;
      totalTickets = (ticketPrice * numberofTicketsSold);
      cout << ticketPrice << " " << numberofTicketsSold << " " <<   totalTickets << endl;
  }

Last edited on
Wow that was really easy thanks man.
Topic archived. No new replies allowed.