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.
Auto comment: topic has been updated by dush1729 (previous revision, new revision, compare).
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.But without fflush also i was getting runtime error. Runtime error Accepted
That is because
scanf("%c", &c)
reads newline symbol. Better way of skipping whitespace symbols is adding single space before%
—scanf(" %c", &c)
.This will always work? What should we do when i have a character array with space like name?
It is defined in standard that:
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
orfread
and then manually parsing it the way you need. There is alsofgets
for reading a line.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:scanf("%d%d", &a, &b);
scanf(" %d\n%d", &a, &b);
And these two as well (they differ from previous because there is extra whitespace in the end):
scanf("%d%d\n", &a, &b);
scanf("%d %d ", &a, &b);
And even these two:
scanf("%d%c ", &a, &b);
— reads number and a character which immediately follows the number, then skips all following whitespacesscanf(" %d%c ", &a, &b);
But not these:
scanf("%c", &a);
— reads any character, whether it's whitespace or notscanf(" %c", &a);
— reads first non-whitespace characterAs 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).
Thank you! (:
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.
That's exactly the problem. With or without fflush i am getting runtime error.
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.
By the way, there is a special manipulator for skipping whitespace characters in C++:
std::ws
(you writeusing namespace std;
in the beginning of the program, so you omit thestd::
part). Example:Your submission without C-style I/O: 14575845