Thread: infintite loop

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Question infintite loop

    I have a pice of code

    Code:
    	do
    	{
    		clrscr();
    		printf("\n\n\t\t <<< MAIN MENU >>>");
    		printf("\n\n\t 1. Insert ");
    		printf("\n\n\t 2. Delete ");
    		printf("\n\n\t 0. Exit ");
    		printf("\n\n\t\t Enter your choice  :  ");
    		scanf("%d",&ch);
    		switch(ch)
    		{
    			case 1 : printf("\n Enter data to be inserted : ");
    				scanf("%d",&data);
    				enqueue(&qarr,data);
    				printf("\n\nThe queue is :-\n\n");
    				display(qarr);
    				break;
    
    			case 2 : printf("\n\nBefore Deletion queue is :-\n\n");
    				display(qarr);
    				dequeue(&qarr);
    				printf("\n\nThe new queue is :-\n\n");
    				display(qarr);
    				break;
    
    			case 0 :exit(0);
    
    			default: printf("\n Invalid Choice, try Again !!!");
    		}
    		printf("\n\n\n\n\t\t Press any key to continue...");
    		getch();
    	}
    	while(1);
    if during runtime i input a character into choice instead of an integer the progam goes into infinite loop.

    I know how to rectify the problem. Bt i don't know why the reason due to which the program is going into an infinite loop. Please help

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    char c;
    
    while ((c = getchar()) != EOF && c != '\n');
    This should consume any characters that were not assigned to variables by scanf().

    NB!
    You can get some funky behavior if there are no characters to consume.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Thanks, but can you please tll me why is the program going into an infinite loop in the first place?

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Ah. Sure. Because you never do anything to break out of the do-while loop, except in case 0 where you exit the program. There's still input in the buffer that scanf() can't assign to ch so you get the default case again and do-while loop runs again. Ad nauseam.
    Last edited by msh; 02-11-2011 at 02:36 AM.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int c;
    
    while ((c = getchar()) != EOF && c != '\n');
    Question 12.1

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Thanks. I keep missing that.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread