1

getchar() is not working in the below program, can anyone help me to solve this out. I tried scanf() function in place of getchar() then also it is not working.

I am not able to figure out the root cause of the issue, can anyone please help me.

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

        do
        {
                printf("\nEnter a number : ");
                scanf("%d",&x);

                if (x<0)
                        n++;
                else if (x>0)
                        p++;
                else
                        z++;

                printf("\nAny more number want to enter : Y , N ? ");
                ch = getchar();

                i++;

        }while(ch=='y'||ch=='Y');
        printf("\nTotal numbers entered : %d\n",i);
        printf("Total Negative Number : %d\n",n);
        printf("Total Positive number : %d\n",p);
        printf("Total Zero            : %d\n",z);
        return 0 ;
}

The code has been copied from the book of "Yashvant Kanetkar"

Sourav Ghosh
  • 130,437
  • 16
  • 177
  • 247
Piyush Zalani
  • 21
  • 1
  • 6
  • Please throw any books by Kanetkar in the trash where they belong - there is a [good list of quality books on C right here on StackOverflow](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) - pick one or two of these to learn from. – Paul R Dec 17 '15 at 23:21
  • @PaulR You are wrong!!!!! did you even read this book!! I am learning c from Yashvant Kanetkar's book and i don't think that it belongs to trash!!I its a great book. –  Jun 20 '17 at 16:54
  • @SMITPATIL: unfortunately the Kanetkar books are extremely out-of-date (more than 20 years), and full of errors and bad information. For some reason most Indian colleges seem to use them (and the equally obsolete Turbo C), so Indian graduates have to unlearn a lot of bad practices and learn C properly all over again after college. Try a more accurate and up-to-date book from the list I linked to above, if you're serious about learning to be a good C programmer. – Paul R Jun 20 '17 at 17:06
  • @PaulR You are right about turbo c.I hate it to!! I use vim+gcc and teachers are angry on me!I know kanerkars book is not good as your list's books but it's not that bad and karnerkar also don't suggest to use turbo c.infact they told to use netbeans in his book.Any way i'll use your list for learning c. –  Jun 21 '17 at 04:14

5 Answers5

2

I think, in your code, the problem is with the leftover \n from

 scanf("%d",&x);

You can change that scanning statement to

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

to eat up the newline. Then the next getchar() will wait for the user input, as expected.

That said, the return type of getchar() is int. You can check the man page for details. So, the returned value may not fit into a char always. Suggest changing ch to int from char.

Finally, the recommended signature of main() is int main(void).

Sourav Ghosh
  • 130,437
  • 16
  • 177
  • 247
1

When the user inputs x and presses enter,the new line character is left in the input stream after scanf() operation.Then when try you to read a char using getchar() it reads the same new line character.In short ch gets the value of newline character.You can use a loop to ignore newline character.

ch=getchar();
while(ch=='\n')
ch=getchar();
Gaurav Sehgal
  • 7,242
  • 2
  • 17
  • 30
1

That's because scanf() left the trailing newline in input.

I suggest replacing this:

ch = getchar();

With:

scanf(" %c", &ch);

Note the leading space in the format string. It is needed to force scanf() to ignore every whitespace character until a non-whitespace is read. This is generally more robust than consuming a single char in the previous scanf() because it ignores any number of blanks.

Filipe Gonçalves
  • 20,077
  • 6
  • 49
  • 66
1

Replacing ch = getchar(); with scanf(" %c", &ch); worked just fine for me!

But using fflush(stdin) after scanf didn't work.

AdityaSrivast
  • 948
  • 1
  • 8
  • 14
calvesmit
  • 11
  • 1
0

When you using scanf getchar etc. everything you entered stored as a string (char sequence) in stdin (standard input), then the program uses what is needed and leaves the remains in stdin.

For example: 456 is {'4','5','6','\0'}, 4tf is {'4','t','f','\0'} with scanf("%d",&x); you ask the program to read an integer in the first case will read 456 and leave {'\0'} in stdin and in the second will read 4 and leave {''t','f',\0'}.

After the scanf you should use the fflush(stdin) in order to clear the input stream.