The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C/C++ Frustration

OutsomniaOutsomnia Registered User regular
edited February 2007 in Help / Advice Forum
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?

Outsomnia on

Posts

  • b0bd0db0bd0d Registered User regular
    edited February 2007
    umm...I dont know anything about that. I'm an assembly programmer. Could you just load the file and compare each char to '$' and when you find it, load that value after that? It'd be a really unoptimized loop tho. Maybe change the other program to output 00.00 format so you always have 4 number digits? I mean, you're goin to need four numberical digits, might as well make the other program do it. There's another ways to do it, but is the text going to be always in that format? Only trouble is, what if a title has $ in the title? Isnt there commands to open a file and read it in squentail order? That's what you need. I mean, you need the data from the 4th line, last 4 digits. I know C has to have something to do that. You kinda got a database file that's extra formatted. Can you change the other program to output something different? Like a string of numbers in a certain order. That'd make things extra easy. Well, I can figure it out in asm but not C. Stupid brain. Why wont you learn c!

    b0bd0d on
  • ecchiecchi Registered User regular
    edited February 2007
    Okay, it isn't working... so what is happening, then? Do you know how to use the debugger? Put a breakpoint after one of the variable assignments and see what it's getting set to.

    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.

    ecchi on
  • Jimmy KingJimmy King Registered User regular
    edited February 2007
    I would read the whole line in, 1 line at a time, and then you should be able to use various string operations to pull the just info you need from the line. I'm not a c++ guy, so I can't tell you exactly what classes/methods to be looking at, but based on the languages I do work with, I would be very surprised if this functionality wasn't there.

    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).

    Jimmy King on
  • john fechonjohn fechon Registered User regular
    edited February 2007
    By not working, what do you mean? Is it reading in the wrong things, seg faulting, or printing errors when you try to compile?

    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).

    john fechon on
  • clsCorwinclsCorwin Registered User regular
    edited February 2007
    Well, you could toss out an if statement to determine if the type of data you're going to pull in is a double (or float, whichever you're using) and have it pull that, and leave the strings alone, maybe?

    clsCorwin on
  • OutsomniaOutsomnia Registered User regular
    edited February 2007
    I think I figured out why it wasn't working. The text file from the first program was put in a separate directory than the second program, thus nothing was input from the file into my variables and they just had arbitrary numbers as their values. I suppose that would explain a total profit of $-19600000000 or so.

    If I combine both programs it seems to work fine, which I assume is because the text file is right where it should be.
    Jimmy King wrote:
    Hopefully that makes sense, I imagine it looks like a bit of a clusterfuck the way I wrote it.
    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.
    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).
    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?
    clsCorwin wrote:
    Well, you could toss out an if statement to determine if the type of data you're going to pull in is a double (or float, whichever you're using) and have it pull that, and leave the strings alone, maybe?
    I don't really know how to do that in an if statement.

    Outsomnia on
  • JHunzJHunz Registered User regular
    edited February 2007
    Outsomnia wrote:
    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).
    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?
    That wouldn't work, for that precise reason. If you really want to use the ignore function, just call it twice.

    JHunz on
    bunny.gif Gamertag: JHunz. R.I.P. Mygamercard.net bunny.gif
Sign In or Register to comment.