Hey guys, I've got a bit of a problem. I wrote a program which writes ticket sales information to a file. The file looks something like this:
Movie Name: Movie Name
Adult Tickets Sold: 12
Child Tickets Sold: 12
Gross Box Office Profit: $108
Net Box Office Profit: $21.6
Amount Paid to Distributor: $86.4
Movie Name: Movie Name
Adult Tickets Sold: 13
Child Tickets Sold: 13
Gross Box Office Profit: $117
Net Box Office Profit: $23.4
Amount Paid to Distributor: $93.6
And now I'm writing another program to read the gross profit and net profit from both movies and total them. My problem lies in the fact that I can't figure out a way to read just the numbers I need (in this case, 108, 21.6, 117, and 23.4). I don't know how to read a file in nonsequential order. I'm trying something like
salesFile.open("ticketSales.txt");
salesFile.ignore(1000, '$');
salesFile >> grossProfit1;
salesFile.ignore(1000, '$');
salesFile >> netProfit1;
salesFile.ignore(1000, '$');
salesFile >> grossProfit2;
salesFile.ignore(1000, '$');
salesFile >> netProfit2;
salesFile.close();
but since I'm really new at this I don't know why that isn't working. I was thinking that the ignore commands would just get straight to the $ signs, which is where the information I need is. Any pointers?
Posts
I don't know a lot about STD stream operators and stuff, but you might want to try using getline() instead. Then you can parse the lines individually, if the ignoring stuff doesn't work out right.
Alternately, here's how I did something similar which I got from the Ascii PlayerFiles mod for CircleMUD (which is in C but the logic should work for C++, too). You can standardize the length of your labels, assuming that's not breaking some rule of the assignment such as "you must use these labels". Read in the first <size of label> characters, then run that through a switch. Switch can only compare char and int, so you just compare the first character of the label, if the first character matches, then compare the whole string (in C I used strcomp for this) in some if statements. When the label matches the desired string, read in the rest of the line. Just put this in a loop that reads the whole file line by line and then it doesn't matter what order anything is in. Extend this logic slightly to check for movie name first and then read only until you get to the next "Movie Name" label. In C I used fgets to read the label which allowed me to specify how many characters and would leave a pointer at the point I left off and fscanf to read the rest of the line. From there just strip off any $ and you're good.
Hopefully that makes sense, I imagine it looks like a bit of a clusterfuck the way I wrote it. I'd be happy to post example C code which may help clarify it if you want and the mods are ok with that (I don't know how close to giving the answer away I would be if I did this).
edit - I tried your code, and I had everything working fine. The only problem was that you're reading in gross1, net1, then gross2. There's a third value in there that will offset your numbers (Amount paid to distributors).
See how many books I've read so far in 2010
If I combine both programs it seems to work fine, which I assume is because the text file is right where it should be.
It doesn't make a whole lot of sense to me, but that's probably because I only have a few weeks of C experience.
Yeah, I'll fiddle around with it some more to get past that. Maybe throw in a space next to the $ for the second gross profit, and ignore to the '$ '. Or would that not work, as '$ ' is a string and not a character?
I don't really know how to do that in an if statement.