Блог пользователя dush1729

Автор dush1729, история, 9 лет назад, По-английски

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.

  • Проголосовать: нравится
  • -9
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.

  • »
    »
    9 лет назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

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

      • »
        »
        »
        »
        9 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

        • »
          »
          »
          »
          »
          9 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          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.

        • »
          »
          »
          »
          »
          9 лет назад, # ^ |
          Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

          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).

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.

      • »
        »
        »
        »
        9 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.

»
9 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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