user1813332
user1813332

Reputation: 55

loop fails to terminate despite providing appropriate input

This program runs into an infinite loop despite providing 'n' as an input,to exit the while loop. What could be the issue ?

#include<stdio.h>
    main()
    {        
  int num,p=0,q=0,r=0;
    char check='y';

   while(check!='n')
     {
   printf("do you want to enter a number y or n");
    scanf("%c",&check);
    getchar();
    printf("enter a number");
     scanf("%d",&num);


    if(num>0)
      p++;
       else if(num<0)
      q++;
     else 
     r++;


    }
     printf("positive=%d\t negative=%d\t zero=%d\t",p,q,r);
        }

Upvotes: 0

Views: 122

Answers (3)

Daniel Fischer
Daniel Fischer

Reputation: 183883

while(check!='n')
{
    printf("do you want to enter a number y or n");
    scanf("%c",&check);
    getchar();
    printf("enter a number");
    scanf("%d",&num);

The scanf("%d", &num); leaves the newline in the input buffer, thus in the next iteration of the loop, that is stored in check. After that, the getchar() consumes the 'n' or 'y' you entered. Then the scanf("%d", &num); skips the newline left in the buffer, scans the entered number, and leaves the newline in the buffer. You need to remove the newline between scanning in the number and querying whether you want a next iteration.

Above that, it would be better to exit the loop immediately after the user entered an 'n', so

while(check!='n')
{
    printf("do you want to enter a number y or n");
    scanf("%c",&check);
    if (check == 'n') {
        break;
    }
    printf("enter a number");
    scanf("%d",&num);
    getchar();  // consume newline

would be better. That would still be open to bad things should the user input not match expectations, so if you want a robust programme, you need to check the return value of scanf to know whether the conversion was successful, and completely empty the input buffer before and after scanning in the number.

Upvotes: 1

hall.stephenk
hall.stephenk

Reputation: 1165

The issue is that the loop isn't exiting until the while condition is re-evaluated at the top of the loop. I'd suggest reworking your loop to something like this.

// we've purposely changed this to an infinite loop because
// we hop out on condition later
while(1)
{
    printf("do you want to enter a number y or n");
    scanf("%c",&check);
    getchar();

    // here's the code that will jump out of the loop early if the user
    // entered 'n'
    if('n' == check)
        break;

    // user didn't enter 'n'...they must want to enter a number
    printf("enter a number");
    scanf("%d",&num);

    if(num>0)
        p++;
    else if(num<0)
        q++;
    else 
        r++;
}

Upvotes: 1

Sudipta Chatterjee
Sudipta Chatterjee

Reputation: 4670

You are not checking the character input. Here is what it should be:

printf("do you want to enter a number y or n");
scanf("%c",&check);
/* This is what you need to add */
if (check == 'y') {
  getchar();
  printf("enter a number");
  scanf("%d",&num);
}

Upvotes: 0

Related Questions