0

I'm trying to execute below program where user enters numbers until he wishes, but my second scanf() is getting skipped. i tried giving scanf("%[^\n]d",&ans) but it doesn't work. Fflush(stdin) doesn't work in netbeans. please help and explain why it is happening.

int Ch5BG()
{
    int number,positive,negative,zero;
    char ans='y';
    positive=negative=zero=0;

    while(ans=='y'||ans=='Y')
    {
        printf("Please enter a number:\n");
        scanf("%d",&number);

        if(number>0)
            positive++;
        if(number<0)
            negative++;
        if(number==0)
            zero++;


        printf("Do you want to continue:");
        scanf("%c",&ans);

    }
    printf("The number of positives entered are:%d",positive);
    printf("The number of negatives entered are:%d",negative);
    printf("The number of zeros entered are:%d",zero);

    return 0;
}

The output would be as below :

Please enter a number:
3
Do you want to continue:The number of positives entered are:1The number of negatives entered are:0The number of zeros entered are:0
RUN SUCCESSFUL (total time: 1s)
  • 2
    `scanf("%c",&ans);` --> `scanf(" %c", &ans);` – BLUEPIXY Nov 06 '17 at 15:15
  • `scanf` is a bit tedious to use for any text, where "enter"/newline has any special meaning. Terminal is such a thing, user submits input one line at a time (not one character at a time). Consider switching to using `fgets` and using `sscanf` and other functions to parse input one line at a time. – hyde Nov 06 '17 at 15:15
  • @BLUEPIXY https://meta.stackoverflow.com/questions/357845/what-to-do-with-all-the-questions-about-scanf – Jean-François Fabre Nov 06 '17 at 15:16
  • In the linked duplicate, scroll down to "When *scanf() does not work as expected". – Lundin Nov 06 '17 at 15:16
  • recommended dupe: https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer – Jean-François Fabre Nov 06 '17 at 15:16
  • @Jean-FrançoisFabre It wasted my time. – BLUEPIXY Nov 06 '17 at 15:19
  • sorry about that. – Jean-François Fabre Nov 06 '17 at 15:20
  • @BLUEPIXY Thanks a lot.. :) Can you point me out to why it happened ? and why the space worked ?It would help me a lot. –  Nov 06 '17 at 15:22
  • @hyde I'm still new and learning. Thanks for letting me know will try it out. –  Nov 06 '17 at 15:23
  • @Jean-FrançoisFabre I don't think that the links from that meta discussion are particularly great for canonical dupes. I posted an alternative to the same meta discussion and used that one as dupe here. – Lundin Nov 06 '17 at 15:27
  • @Lundin that's the spirit (note that I didn't write that one, just know it exists, so if you make it better, it's good for future readers) – Jean-François Fabre Nov 06 '17 at 15:28
  • @MeghanaReddy `%d` does not consume a newline following a number. So it is received at `%c`. Spaces in the format string of `scanf` skip the sequence of white-spaces characters. Please refer to [scanf](http://en.cppreference.com/w/c/io/fscanf) for details. – BLUEPIXY Nov 06 '17 at 15:28
  • @BLUEPIXY Thank you, i will check it out. –  Nov 06 '17 at 16:38

0 Answers0