0

program to count number of +ve, -ve and zeroes in the input.the printf statement in the for loop is executed more than once.how to correct this code.the count is correct but output is not in the expected format.

#include<stdio.h>
main()
{
    int n,pc,nc,zc;
    char s;
    pc=nc=zc=0;
    for(;1;) {
        printf("do you wanna enter: y/n\n");
        s=getchar();
        if(s=='y') {
            printf("enter num:\n");
            scanf("%d",&n);
            if(n>0) {
                pc+=1;
            }
            if(n<0) {
                nc+=1;
            }
            if(n==0) zc+=1;
        }
        if(s=='n') break;
    }
    printf("No.of +ve num: %d \n",pc);  
    printf("No.of -ve num: %d \n",nc);
    printf("No.of zeroes: %d \n",zc);
}

output:

xplorer@kali:~/Desktop/docs/yk/chap3$ ./a.out
do you wanna enter: y/n
y 
enter num:
4
do you wanna enter: y/n
do you wanna enter: y/n
y
enter num:
8 
do you wanna enter: y/n
do you wanna enter: y/n
y
enter num:
-7     
do you wanna enter: y/n
do you wanna enter: y/n
n
No.of +ve num: 2 
No.of -ve num: 1 
No.of zeroes: 0
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
Neo
  • 5
  • 1
  • 1
    Is that how your indentation actually looks in the source file?! – sapi Jun 24 '14 at 06:04
  • i am not particular about indentation.Please enlighten me about it. – Neo Jun 24 '14 at 06:11
  • 1
    Well frankly, your code is absolutely appalling. It is impossible to understand. You have poor variable names, poor structure, inconsistent indentation, inconsistent EVERYTHING. – Miles Rout Jun 24 '14 at 06:21

6 Answers6

2

\n is left behind in input buffer by previous call of getchar because it reads a character at a time. As '\n' is also a character, n next iteration getchar reads that left over \n .
You need to flush the input buffer:

int c;
while((c = getchar()) != '\n' && c != EOF);
haccks
  • 104,019
  • 25
  • 176
  • 264
0
...
{
printf("enter num:\n");
scanf("%d",&n);
...

The 'scanf' in the above snippet of the question code reads an integer from stdin, but only after the user presses return. The return character '\n' is also sent to stdin, however, the above 'scanf' is satisfied with reading only the integer value, and leaves the '\n' character in the stdin stream. The next time any function attempts to read from stdin, the '\n' character will be there to read. This is causing undesirable results in the question code here:

...
{
printf("do you wanna enter: y/n\n");
s=getchar();
....

The 'getchar' function read the '\n' character from stdin, and the program acts on it.

One way to avoid this problem is to have the 'scanf' function read the residual '\n' character from the buffer. This can be done by changing this:

scanf("%d",&n);

To this:

scanf("%d%*c",&n);

The latter reads the integer value from the stdin stream, and then reads (and discards) the residual '\n' character.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
0

After each read from stdin, remove all additional characters from the buffer with:

/* discard (flush) remaining chars in stdin */
while (ch !='\n') ch = getchar();

As discussed in other answers, after you test with scanf or other input utilities, additional characters remain in the input buffer. You cannot reliably flush the input buffer with any standard function such as fflush as they apply to output buffers and not to input buffers. The simple while loop that scans for the newline insures that all characters are read regardless of whether a carriage return is present before the newline and is therefore portable between most OS's.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

If curses.h is available on your system, You can use flushinp function to flush the input. Check C FAQ: http://c-faq.com/stdio/stdinflush2.html

Sanket Parmar
  • 1,477
  • 12
  • 9
0

You have only checked for two conditions i.e 'y' or 'n' and not for the other so It might happen that it takes input other than 'y' or 'n'from previous inputs and loop continues. So just chk by printing if it is taking any other input and make changes accordingly.

user3640507
  • 65
  • 1
  • 7
0

Replace s=getchar(); with

do
{
   s=getchar();
}while (isspace(s));

Things will start working.