Thread: infinite loop problem

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    33

    infinite loop problem

    I get an infinite loop when I input a char(letter) in grade1, I want to know how do I not infinite loop when I input a char(letter)
    Code:
    #include<stdio.h>                 //important since printf
    #include<conio.h>                 //impportan since clrscr()
    main()
    {
    	clrscr();
    	int G1,G2,G3;
    	float average,total;
    	char a;
    	printf("\nInput Grade1 out of 100:");
    	scanf("%d",&G1);
    	if (G1 > 100)
    	{
    		do
    		{
    		printf("Grade1 is > than 100, Please re-enter:");
    		scanf("%d",&G1);
    		}
    		while(G1 > 100);
    	}
    	if (G1 = a)
    	{
    		printf("Inputed Grade is char not a number, Please re-enter:");
    		scanf("%d",&G1);
    	}
    	printf("\nInput Grade2 out of 100:");
    	scanf("%d",&G2);
    	if (G2 > 100)
    	{
    		do
    		{
    		printf("Grade2 is > than 100, Please re-enter:");
    		scanf("%d",&G2);
    		}
    		while(G2 > 100);
    	}
    	printf("\nInput Grade3 out of 100:");
    	scanf("%d",&G3);
    	if (G3 > 100)
    	{
    		do
    		{
    		printf("Grade1 is > than 100, Please re-enter:");
    		scanf("%d",&G3);
    		}
    		while(G3 > 100);
    	}
    	total = G1+G2+G3;
    	printf("\nYour total grade is:""%f",total);
    	average = total/300;
    	printf("\n\naverage is:""%f",average);
    	getch();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One way is to add a getchar() after each scanf(). That will pull the newline char that is causing the problem, off of the input stream for you.

    Another way is to add a space before the % in scanf(" %d...etc. Test it to be sure it's what you want though.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    first of all i think he should modify the the input method of the grade because he compares a letter with a number in the program may be you should add ''fflush(stdin) '' after the input to remove the infinite problem since it erases the the extra characters input and also the the space before % as @adak said rightly. hope it helps buddy.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    i added getchar() after scanf()
    when i compile i get an error
    wrong number of arguments in call of marcro 'getchar'
    Code:
    clrscr();
    	int G1,G2,G3;
    	float average,total;
    	char a;
    	printf("\nInput Grade1 out of 100:");
    	scanf("%d",&G1);
    getchar("%s",&a);
    	if (G1 > 100)
    	{
    		do
    		{
    		printf("Grade1 is > than 100, Please re-enter:");
    		scanf("%d",&G1);
    		}
    		while(G1 > 100);
    	}
    	if (G1 = a)
    	{
    		printf("Inputed Grade is char not a number, Please re-enter:");
    		scanf("%d",&G1);

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    thanks nyahCheck. no need to reply

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Nyah Check View Post
    first of all i think he should modify the the input method of the grade because he compares a letter with a number in the program may be you should add ''fflush(stdin) '' after the input to remove the infinite problem since it erases the the extra characters input and also the the space before % as @adak said rightly. hope it helps buddy.
    fflush(stdin) is a bad idea. It only works on a few compilers that are specifically rewired to deal with it.
    The actual fflush() function is designed for flushing disk buffers, not the keyboard.

    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    The correct way is to use getchar() in a loop, like this...
    Code:
    scanf("%d",&value);
    while(getchar() != '\n');

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't say you were using Turbo C, so I gave you the modern version:

    Code:
    getchar(); // gives no warnings or errors on modern compilers
    
    int c = getchar(); //the c= part is required to avoid warnings or errors
                             //on older compilers. Note that c is an int, not a char.
    
    /* fflush(stdin) only works sometimes, and has been removed from consideration from the C standard. 
    
    You shouldn't use it, or count on it.
    
    fflush() works on OUTWARD streams only. It's like a toilet, it flushes. Your kitchen sink faucet is 
    an inward stream - it doesn't flush.
    
    Don't try to flush the inward streams (keyboard or file or anything else).
    */

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    CommonTater
    thanks man
    now it loops itself when i input a letter
    thanks for the warning about fflush()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite Loop, problem with while()
    By JonathanS in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 03:47 AM
  3. Infinite loop output problem
    By Jonyb222 in forum C Programming
    Replies: 5
    Last Post: 10-08-2009, 11:18 AM
  4. Infinite loop problem
    By Xanth in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2005, 12:08 AM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 03:24 PM

Tags for this Thread