0
#include <stdio.h>
#include <string.h>

int main()
{
        int num;
        char answer[10];
        char affirmation[10]="yes";
        do
        {
                printf("Enter a number : \n");
                scanf("%d",&num);
                if (num % 97 == 0)
                {
                        printf("No. is divisible by 97!\n");
                }
                else
                {
                        printf("No. is not divisible by 97!\n");
                }

                printf("Once More ? [yes/no] \n");
                fgets(answer,sizeof(answer),stdin);
        }
        while(strcmp(affirmation,answer) == 0);
        return 0;
}

I expected from this program to check the divisibility of a provided number by 97 and then to ask if I again want it to check for another number if I input "yes". But it isn't prompting for my input . If anybody can explain the reason behind this problem and suggest some ways to get through, it will be appreciated.The output is given below: This output is for num = 194.

Raja
  • 1
  • 1
  • 1
    Because you can't mix `scanf` and `fgets`. Try using `scanf("%9s", answer)` to read the y/n response. (Actually, I'm exaggerating: you *can* mix them, but you have to worry about several rather crazy, esoteric details.) – Steve Summit May 28 '21 at 15:11
  • 2
    `scanf("%d",&num);` leaves the `` in the input buffer. That `` is the only thing `fgets()` catches. – pmg May 28 '21 at 15:11
  • 1
    Does this answer your question? [fgets doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf) – yano May 28 '21 at 15:12

1 Answers1

0

It is being executed but what it's reading is the newline character in the input stream that wasn't read when you did the scanf.

What your scanf does is to first skip any white space, then read an integer up to but not including the first non-digit character, which is probably the \n generated by the ENTER key.

Then, because fgets reads a line up to the next newline, you get an empty line.

In both the preceding two paragraphs, I'm assuming for simplicity ideal conditions such as ensuring it's a valid integer and that the line is not longer than the size you provided. Obviously, deviating from that could cause unruly behaviour.

It's generally not a good idea to mix the two input types unless you know in great detail what will be in the input stream. It's often better to just use line-based input and then sscanf that into more appropriate variables.

That way, you can be more sure about where your input stream is at any given point.

A quick fix in this particular case is simply to augment your scanf so that it skips to the start of the next line:

scanf("%d",&num);
while (getchar() != '\n') {}

The other problem you'll have after that is the fact that your fgets input will actually be "yes\n" rather than "yes", so that string comparison won't work.

If you're after a fairly robust line input function that takes care of that (and many other things), see here.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953