MVP_Harry's blog

By MVP_Harry, history, 4 years ago, In English

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

Here

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:

USACO 2015 G DEC USACO 2016 G JAN

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

  • Vote: I like it
  • +6
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +7 Vote: I do not like it

You need to write the output to a file, not stdout.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    That’s what I did. If I didn’t, the error message would be “Your output file missing”

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it +9 Vote: I do not like it

      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);
      	}
      
      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        hmmm but I have submitted other problems using the same setting (you can see me update), am I missing something?

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Oh yeah I realized in both cases, I output the answer in a function in front of the main function where I said ifstream ...

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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:

          1. freopen, as suggested above; you can even conditionally only freopen when the input file exists (that's the if (fopen) part).
          2. Declare your ifstream and ofstream globally, ideally with a different name (fin and fout?). Then, just remember to use the new names.
          3. 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.
          • »
            »
            »
            »
            »
            »
            4 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            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?

            • »
              »
              »
              »
              »
              »
              »
              4 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              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.

              • »
                »
                »
                »
                »
                »
                »
                »
                4 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                Can you explain why sync_with_stdio shouldn't be necessary after a freopen?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it +5 Vote: I do not like it

                  I might be wrong, but I think flushing or buffering is configured to be nicer with file IO.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).