I have an assignment due this Friday that I've committed to completing tonight. I've been working at it for about two hours now and I seem to be stumped. I'm meant to write a program which prompts the user to enter an expression in the form (value operator value) e.g (1 + 3)(1 * 6) and then evaluate that expression and push it into an output file.
I'm stumped because in my code I can't seem to figure out why the echo of the user input isn't outputted to the file "prog3_002out.txt" within the switch statements. That's the last part I need to figure out.
That's so weird, "2 + 35" was printed in the output file? Everytime I build and run the program through and then open the text document/output file it only has the identifcation block that I put before the switch statements. I don't get any of the actual expression
"Tommy Clark "
"C.S.1428.002"
"Lab Section: L11"
"10/04/17"
I think I know what is happening. You need to check what is in the file after it has been closed. The file is closed either when explicitly closed such as fout.close(); or when the fout stream goes out of scope at the end of main(). But ... you have this system("PAUSE>NULL"); which holds the file open, because it has not yet been closed in either of the ways mentioned. Meanwhile, the contents of the fout buffer may not yet have been written to the file.
The simple solution. Move that pause statement so it is after the close() statement.
Ironically I was noticing the over-generous use of endl in the earlier parts of the program. endl has the effect of first outputting a newline and then flushing the buffer. That would cause its contents to be written to the file immediately.
Funny enough I had the thought that it would be exactly that, immediately fixed the problem I've been trying to solve for a while also the endl explanation was helpful to see why I could see the first line of output but not the last.
Thank you so much for the help, also the formatting this professor wants precisely is still strange to me and I've found the easiest way to match it is to just endl pretty much everything. What suggested alternatives would you have though?
Well, it was more a matter of reducing the calls to flush the buffer - flushing the buffer no less than seven times in a single statement just strikes me as unnecessary. At this point in time, with the type of program you are writing, it really doesn't matter, but if you were to later write a program which generated many thousands of lines, the use of a plain '\n' would be quicker than endl. It depends of course on the requirements.
... but I noticed the date was different in the original - lines 26 and 39 have two different dates ... so my suggestion is either an improvement as it keeps them the same, or a disadvantage as you want them to be different.
Note I left a single endl right at the end of each output.