Flavanoid's blog

By Flavanoid, history, 3 years ago, In English

Here is my code. When compiling using g++ 12.1.0 on my local machine or using GNU G++ 20 on the judging machine, it always stop the input after the first loop of the first loop. Like it showns here. With the debug code, it output something like this where lines start with > is my input.

But when compiling with Clang++ on my local machine or on judge machine, it works well.

P.S. please ignore the WA on clang++, it is caused by making too many queries, which can be solved using binary search but I've not implement that yet.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I'm not sure what's wrong, but I recommend you to use cout.flush() instead of ... << flush

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

    Also don't use scanf/printf with cin/cout.

»
3 years ago, # |
Rev. 3   Vote: I like it +2 Vote: I do not like it

You have array overflow and undefined behavior right here:

    char c;
    // ....
    scanf("%s", &c);

%s is for reading C-style strings. It needs an array of characters to write the string and then the zero terminator. You have allocated space for a single character only.

Try compiling with -Wall -Wextra -Werror -fsanitize=address -fsanitize=undefined locally.

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

    Yeah, I've tried that. At first I used cin>>c, but it seems that instead of the character, the \t\n is read into the char. So I have to use scanf. Is there any way of avoiding that?

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

      You can also use scanf(" %c", &c) instead of scanf("%s", &c). The space means to ignore whitespace characters. See more at cppreference.