Do while not looping?

Asked by At

I am a beginner who has started learning C. I want to write a program using C which helps users to input nos. as much as they want and once they are done, the program shows the no. of positive, negative and zeros entered. I am trying to do this using do-while lopp. It is executing once, but it isnt going on loop. I want to know what am i doing wrong here?

#include <stdio.h>
int main ()
{
    int a,p=0,n=0,z=0;
    char c;

    do
    {
       printf("enter a no.\n");
       scanf("%d", &a);

       if (a>0)
       {
            p++;
       }
       else if (a==0)
       {
            z++;
       }
       else if(a<0)
       {
            n++;
       }

       printf("input more no.? y or n\n");
       scanf("%c\n", &c);
    } while (c=='y');

    printf("the total count of positive, negative and zeros are %d %d %d\n", p,n,z);

    return 0;
}

Expected result is:

enter a no.

4

input more no.? y or n

y

enter a no.

3

input more no.? y or n

y

enter a no.

-1

input more no.? y or n

n

the final count of positive, negative and zeros are 2 1 0

But the actual output coming is:

enter a no.

4

input more no.? y or n

y

the final count of positive, negative and zeros are 1 0 0

2 Answers

2
Some programmer dude On Best Solutions

You problem is a combination of two separate problems.

The first is that any white-space characters in a scanf format string tells scanf to read (and throw away) any consecutive white-space in the input.

But to know where the white-space in the input ends, it needs to see something that is not a white-space. That's why trailing space in the format string forces you (the user) to input something else.

The second problem is that for the previous input operation (scanf("%d", &a)) you probably pressed the Enter key to "send" the input to the program. This Enter key will be put into the input buffer as a newline (which is a white-space character). When you then read your characters with the "%c" format, it will read that newline. You need to skip that newline.

Both problems are solved the same way: Ask scanf to read (and throw away) leading white-space. Which is done by putting the space at the beginning of the format string, as in

scanf(" %c", &c);
//     ^
// Note space here instead

Also note that this explicit skipping of leading white-space is only needed for the "%c" and "%[" formats, all other formats do this implicitly.

0
bruno On

You mix read of a number and read of a character you do not want to get the character enter just after the number being generally a newline, but currently you get that newline.

To bypass the separators (space / newline) just add a space in the format before %c and you do not need to specify a newline : scanf(" %c", &c);

After that :

[email protected]:/tmp $ gcc -pedantic -Wextra -Wall c.c
[email protected]:/tmp $ ./a.out
enter a no.
4
input more no.? y or n
y
enter a no.
3
input more no.? y or n
y
enter a no.
-1
input more no.? y or n
n
the total count of positive, negative and zeros are 2 1 0
[email protected]:/tmp $