-1
#include <stdio.h>

int main()
{
    char another;
    int num;
    do
    {
        printf("enter the number");
        scanf("%d", &num);
        printf("square of%d is %d\n", num, num * num);
        printf("want to check another number y/n");
        fflush(stdin);
        scanf("%c", &another);
    } while (another == 'y');
    return 0;
}

In the above code, the second scanf() is not getting executed and hence the console is not accepting input.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 7
    `fflush(stdin);` oh no, just no. You need to remove that monstrosity and then `scanf("%c",&another);` -> `scanf(" %c",&another);` – George Mar 20 '17 at 17:33
  • 3
    Always check the result of scanf. *"scanf() is not getting executed"* is most likely not what's actually the problem. – Kijewski Mar 20 '17 at 17:33
  • 1
    Use scanf(" %c",&another) instead of scanf("%c",&another). – msc Mar 20 '17 at 17:36

5 Answers5

3

According to the standard, flushing stdin is undefined behaviour. See Using fflush(stdin) for more information.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
babon
  • 3,615
  • 2
  • 20
  • 20
2

When you enter a number for the first scanf, its always followed by a newline. %d only takes the integer value and the newline is still left in the input buffer. So the subsequent scanf ends up consuming that character and your loop terminates due to another=='y' being false. (another has '\n').

Following is one of the ways to solve the problem. Use a %c along with %d to capture newline and ignore it.

#include<stdio.h>
int main()
{
    char another, nl;
    int num;
    do
    {
        printf("enter the number");
        scanf("%d%c",&num,&nl);
        printf("square of%d is %d\n",num,num*num);
        printf("want to check another number y/n: ");
        //fflush(stdin);
        scanf("%c",&another);
        printf("%c", another);
    }while (another=='y');
    return 0;
}
Shridhar.S
  • 112
  • 2
1

if you add the statement

fseek(stdin, 0, SEEK_END);

it will move the stdin pointer to the end of the file so any extra character will be omitted. then write the second scanf. I mean:

#include<stdio.h>
int main()
{
    char another, nl;
    int num;
    do
    {
        printf("enter the number");
        scanf("%d%c",&num,&nl);
        fseek(stdin, 0, SEEK_END);
        printf("square of%d is %d\n",num,num*num);
        printf("want to check another number y/n: ");
        //fflush(stdin);
        scanf("%c",&another);
        fseek(stdin, 0, SEEK_END);
        printf("%c", another);
    }while (another=='y');
    return 0;
}
Fatemeh Karimi
  • 914
  • 2
  • 16
  • 23
  • 2
    Note that on a Unix system, if standard input is a terminal, the terminal is not a seekable device, so the proposed `fseek()` would do nothing (or, rather, it would fail with error ESPIPE, Illegal seek). If the input device was a file, it would, of course, seek to the end of the file; there'd be no further input unless the file grew between the time of the seek and the time of the `scanf()`. So, on the whole, this is not a portable solution. – Jonathan Leffler Sep 09 '17 at 23:33
  • @JonathanLeffler yes you're right! thanks a lot. – Fatemeh Karimi Sep 10 '17 at 18:13
0

Cause of the char input before scanf dont work remove the fflush and add a space to <<"%c">> like this: scanf(" %c",&another);

#include<stdio.h>

int main(){

    char another;

    int num;

    do
    {
        printf("enter the number ");
        scanf("%d",&num);
        printf("square of %d is %d\n",num,num*num);
        printf("want to check another number y/n ");
        scanf(" %c",&another);


    }while (another=='y');
    return 0;
}

This works great

ChrisK
  • 41
  • 5
-1

First of all fflush; flushes the output buffer of a stream, so you should not be using it here. The reason why the second scanf is not working is because you are trying to read a 1-bit character which in this case always getting the value of \0 after the second printf statement. Hope this helped.

The king
  • 1
  • 1