### MVP_Harry's blog

By MVP_Harry, history, 2 months ago,

Edit: Problem fixed. It turns out that I output the answer in a separate function before I input and output the file, which causes the problem. Thanks for everyone that helps :)

Edit: I did write output file

I've been doing some past USACO Gold problems lately, but I have met some troubles regarding submission. Although my program outputs the correct answer, it keeps telling me that the answer is incorrect. I've met these kind of troubles before, so I thought its just a format issue. But no matter how I adjust the way I output the answer, it doesn't work.

For example, I have tried cout << ans << "\n"; cout << ans << endl; cout << ans << " "; cout << ans; but non of them works.

Here are two screenshots:

Can someone please help me with that? I just don't want to run into these kind of troubles during real contest lol. Thanks!

• +6

 » 2 months ago, # |   +7 You need to write the output to a file, not stdout.
•  » » 2 months ago, # ^ |   0 That’s what I did. If I didn’t, the error message would be “Your output file missing”
•  » » » 2 months ago, # ^ | ← Rev. 2 →   +9 No you didn't. Notice how right after "Your output file" it has nothing, because you didn't print anything to a file. Instead it says "Your program wrote this on STANDARD OUTPUT: x". Because you wrote to standard output.Put this at the top:  if (fopen("FILENAME.in", "r")) { freopen("FILENAME.in", "r", stdin); freopen("FILENAME.out", "w", stdout); } 
•  » » » » 2 months ago, # ^ |   0 hmmm but I have submitted other problems using the same setting (you can see me update), am I missing something?
•  » » » » 2 months ago, # ^ |   0 Oh yeah I realized in both cases, I output the answer in a function in front of the main function where I said ifstream ...
•  » » » » » 2 months ago, # ^ |   0 In general, this pattern isn't great. You're shadowing the global std::cin variable with your own local variable named cin. There are a few things that could be better: freopen, as suggested above; you can even conditionally only freopen when the input file exists (that's the if (fopen) part). Declare your ifstream and ofstream globally, ideally with a different name (fin and fout?). Then, just remember to use the new names. You could make a function void go(istream& in, ostream& out) and put your code there; then, use in and out, and pick whether to use cin/cout or an ifstream/ofstream when you call it in main.
•  » » » » » » 2 months ago, # ^ |   0 Thanks! This might be a stupid question, but I thought if I used ios::sync_with_stdio(false), it prohibits redirection with freopen. How am I supposed to both use freopen and fast input using cin?
•  » » » » » » » 2 months ago, # ^ |   0 No, those are essentially unrelated. freopen is a lower level change than the cin/cout streams.However, if you set sync_with_stdio, then do any I/O via cin/cout, and then you freopen afterwards, I think things may fall out of sync.Also, sync_with_stdio shouldn't be necessary after you freopen, so you could put that in the else branch.
•  » » » » » » » » 2 months ago, # ^ |   0 Can you explain why sync_with_stdio shouldn't be necessary after a freopen?
•  » » » » » » » » » 2 months ago, # ^ |   +5 I might be wrong, but I think flushing or buffering is configured to be nicer with file IO.
 » 2 months ago, # |   0 Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).
 » 2 months ago, # |   0 Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).