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!
You need to write the output to a file, not stdout.
That’s what I did. If I didn’t, the error message would be “Your output file missing”
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:
hmmm but I have submitted other problems using the same setting (you can see me update), am I missing something?
Oh yeah I realized in both cases, I output the answer in a function in front of the main function where I said
ifstream ...
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 theif (fopen)
part).fin
andfout
?). Then, just remember to use the new names.void go(istream& in, ostream& out)
and put your code there; then, usein
andout
, and pick whether to use cin/cout or an ifstream/ofstream when you call it in main.Thanks! This might be a stupid question, but I thought if I used
ios::sync_with_stdio(false)
, it prohibits redirection withfreopen
. How am I supposed to both usefreopen
and fast input usingcin
?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.
Can you explain why sync_with_stdio shouldn't be necessary after a freopen?
I might be wrong, but I think flushing or buffering is configured to be nicer with file IO.
Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).
Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).