holes, wrong answer.

int main()
int count = 0, counter = 0;
char input;
scanf("%d", &count);
for (; count != 0; count–)
scanf("%c", &input);
while (input != ‘\n’)
scanf("%c", &input);
if (input == ‘A’||input == ‘D’||input == ‘O’||input == ‘P’||input == ‘Q’)
else if (input == ‘B’)
counter + 2;
printf("%d\n", counter);
return 0;
can anyone tell wats wrong wit this code. input takes in ‘\n’ after i enter the counter value.

You forgot about R. It has a hole in it as well.

alt text

But that’s not all that’s wrong with your code. First let’s explain about the buffering. When you use your first scanf() , you read an integer and then press Enter ( or Return ). Now when you press that, a newline character ( ‘\n’ ) is left in the input buffer ( because scanf() skips whitespaces like newline and spaces when using the %d type specifier ) , and in your next call to scanf() ( which reads a single character ) , you read that character with the scanf() outside the while loop, but your while loop condition is while (input != ‘\n’) , and due to that, the entire loop is skipped as input has the newline character, and thus you will miss one input.

To fix that, you’ll need to rearrange your loop a bit. Firstly , in your first scanf(), add a space as indicated

scanf( "%d ", &count ); 
          ^ a space here

That space should read that newline character.

Next is a very simple mistake. In your else if you have

else if (input == 'B') 
  counter + 2;
          ^ Forgot the = , you are not changing anything here

You just have to change that to

counter =+ 2;

Also, don’t forget about adding the condition for R

You also forgot to reset counter to zero. You should reset it to zero at the beginning of your for loop.

With that, the errors that I saw are explained. You should fix those and try it.

If you are unable to fix it, then you can refer this fixed version of your code.

With all that said, you can actually solve this problem with a much simpler algorithm ( you can use arrays ). But, I’m not going to discuss about that.

So, Happy coding… :slight_smile:


scanf in C has an attribute of line buffering. Let me explain this via an example -

scanf("%c", &value); //would accept a character value and place it in “value” but when you press enter in the console, the new line character is also part of the line buffer. So now,
scanf("%c",&input): //the debugger does not stop at this line for the user to enter an input because it has already placed a new line character ("\n") in the input variable.
To avoid this what you could do is -
scanf("%c",&value) // for the value
scanf("%c",&newLine) // a dummy variable for new line character
scanf("%c",&input) // for the input
scanf("%c",&newLine) //using the same dummy variable for the new line character after entering the input.

+1 for the pic


Well, I was really bored and the answer seemed too plain, so I thought " Why not make it a bit more interesting " and that’s why I did that.

Glad that you all liked it. :slight_smile: