div24ever's blog

By div24ever, history, 4 years ago, In English,

I have encountered weird behaviour with character arrays/strings on various websites. For example take these two submissions — First submission Second submission. I have just removed fflush(stdin) from my first submission and got AC. I faced similar issue with this problem. This submission has scanf without fflush(stdin) and it got runtime error but according to previous logic it should have got AC. Now this submission with fflush(stdin) and scanf also got runtime error. But this submission with cin got AC.

So my question is what should i do to prevent such nasty errors during contest? cin is slower than scanf so i prefer scanf.

EDIT — For first problem, i saw that i have used different versions of C++. In C++ 11 it gave WA but in C++ it gave AC.

 
 
 
 
  • Vote: I like it
  • -9
  • Vote: I do not like it

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

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

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

Uhm, what the hell fflush(stdin) is supposed to do?

Nevertheless, cplusplus.com states that behavior is consistent across platforms for null pointers and streams opened for writing. So answer for your question is simple: does not fflush(stdin) ever unless you have a very good idea about what are you doing, why, and what libraries with which behavior (which may actually be undefined) will be used on target platform.

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

    But without fflush also i was getting runtime error. Runtime error Accepted

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

      That is because scanf("%c", &c) reads newline symbol. Better way of skipping whitespace symbols is adding single space before %scanf(" %c", &c).

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

        This will always work? What should we do when i have a character array with space like name?

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

          It is defined in standard that:

          A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.

          Yes it should always work assuming that you want to skip white-space symbols.

          If you have more specific needs you can read all characters using fgetc or fread and then manually parsing it the way you need. There is also fgets for reading a line.

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

          Whenever scanf encounters any whitespace symbol in format specification (like space, tab or newline) it will skip all whitespace symbols from the input till EOF or first non-whitespace. Same thing happens when it encounters number specifies or %s (basically, any specifier that you'll ever use except for %c). So, all of the following will behave same:

          1. scanf("%d%d", &a, &b);
          2. scanf(" %d\n%d", &a, &b);

          And these two as well (they differ from previous because there is extra whitespace in the end):

          1. scanf("%d%d\n", &a, &b);
          2. scanf("%d %d ", &a, &b);

          And even these two:

          1. scanf("%d%c ", &a, &b); — reads number and a character which immediately follows the number, then skips all following whitespaces
          2. scanf(" %d%c ", &a, &b);

          But not these:

          1. scanf("%c", &a); — reads any character, whether it's whitespace or not
          2. scanf(" %c", &a); — reads first non-whitespace character

          As usual, more details are available in the documentation, I'd recommend looking through. Your code pass if I add that extra space in format specification (14575649).

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

      Hi that problem is due to no valid position to posx/posy being assigned. For eg:

      Changing to: scanf("\n%c %c",&x,&y);

      Fixes the issue. Previous '\n' was being entered in x and p in y. Since it cannot find any '\n', it gave such error since you are next accessing an undefined location. I think fflush clears any extra buffer.

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

        That's exactly the problem. With or without fflush i am getting runtime error.

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

      That's not how debugging works: I consider "if I add this line I get expected result hence it's correct thing to do" a very bad argument.

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

By the way, there is a special manipulator for skipping whitespace characters in C++: std::ws (you write using namespace std; in the beginning of the program, so you omit the std:: part). Example:

char x;
cin >> ws >> x; // read all whitespaces to nowhere, then read single char to 'x'

Your submission without C-style I/O: 14575845