0

Here, user is asked to press respective key to perform particular function, but suppose I pressed any character value such as "g", it goes into an infinite loop.

How to solve this issue?

int item,choice;
clrscr();
while(1)
{
printf("QUEUE SIMULAtOR");
printf("\n1:Insert");
printf("\n2:Delete");
printf("\n3:Display");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:qinsert();
break;
case 2:item=qdelete();
if(item!=-1)
printf("Deleted item is %d",item);
break;
case 3:printf("\nElements in the queue are:");
qdisplay();
break;
case 4:exit(0);
default:printf("\nWrong choice try again:");
}
}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • possible duplicate of [Filtered scanf causing infinite loop](http://stackoverflow.com/questions/12902189/filtered-scanf-causing-infinite-loop) – Anto Jurković Mar 07 '15 at 05:39
  • stop using `scanf()`. It's an inherently flawed function (there's no such thing as "formatted input"). Use `fgets()` to get an entire line, and dedicated parser and conversion functions to parse the line (e. g. `strtok_r()` and `strtol()`, etc.) – The Paramagnetic Croissant Mar 09 '15 at 10:23

2 Answers2

0

Add this after the scanf()

char ch;

while( ( ch = getchar() ) != '\n' && ch != EOF );

This should do the trick.

The problem was caused as scanf() does not store characters ( since you are using %d ) and thus they remain in the input buffer. The next scanf() tries to read this and again, instead of storing it, ignores it and it remains in the buffer. This process repeats, which causes the infinite loop.

The code I gave reads all the characters in the buffer, thus reomoving the infinite loop.

Cheers... Hope this helps you :)

Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • @SiddhantSingh , inside the while loop, there is a `getchar()` which reads a character from stdin ( the buffer ), and it continues to read all the characters until a `'\n'` is encountered. You must also see the semicolon `;` after the while loop, so that nothing beyond it is included in the loop. Read this [Use of getchar()](http://rabbit.eng.miami.edu/class/een218/getchar.html) – Arun A S Mar 07 '15 at 07:44
  • @SiddhantSingh , simply put, this code reads all the values in stdin until `'\n'` is encountered, thus in your case, it will remove the character you entered. There have been many questions related to such issues asked on Stack Overflow, so I believe you can search for it and it would help you way better than I can explain. Read this also [How to flush the console buffer?](http://stackoverflow.com/questions/4573457/how-to-flush-the-console-buffer) – Arun A S Mar 07 '15 at 07:50
-1

This is a really awesome question.You must try to use a break statement inside a if().

Example:

void main()
{

int n;

 printf("Enter the number 5\n");
while(1)
{
      scanf("%d",n);
      if(n==5)
      { break;
                         }
      else
      {
            printf(" enter the correct num again \n");

           }
   }
   printf(" you've entered the right number\n");

   }

There you go this program will run until u enter the number 5

If you want any integer you could use isdigit() function which is under the header file ctype

Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • First of all, its `scanf("%d",&n);`. Secondly, how is this answer trying to solve this question ? I don't see how this is useful. – Arun A S Mar 09 '15 at 09:42