Infinite Loop In My Code

This is a discussion on Infinite Loop In My Code within the C Programming forums, part of the General Programming Boards category; Hi, I am new to programming. I am a self taught programmer wannabe. I have no one to consult except ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    32

    Question Infinite Loop In My Code

    Hi,

    I am new to programming. I am a self taught programmer wannabe. I have no one to consult except books so I think joining this forum would be great.

    Could someone tell me why the program below will continue looping after I entered any number with decimal point?

    Code:
    #include <stdio.h>
    
    int main()
    {
    	float average;
    	float counter, grade, total;
    	total =0;
    	counter = 0;
    
    	printf("Enter grade, -1 to end: ");
    	scanf("%d", &grade);
    
    	while(grade != -1)
    	{
    		total = total + grade;
    		counter = counter + 1;
    		printf("Enter grade, - 1 to end: ");
    		scanf("%d", &grade);
    	}
    
    	if (counter !=0)
    	{
    		average =  total/counter;
    		printf("Class average is %.2f", average);
    	}
    	else 
    		printf("No grades were entered");
    	return 0;
    }


    Also could you let me know whether the MFC features in Visual C++ 6 could be used with C ?


    Is there any IDE software out there that you can use to build GUI forms with C like the drag & drop forms that you see in Visual Basic 6, Visual C++, & JBuilder? If there is none, could you tell me where I can learn (from the internet) writing GUI programs with C?



    Thanks for taking the trouble to reply to me.
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    grade is float,so scanf looks like this
    Code:
    scanf("%f", &grade);

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    6
    Counters should be integers.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>Also could you let me know whether the MFC features in Visual C++ 6 could be used with C ?
    MFC = Microsoft Foundation Classes
    C does not have classes.

    Therefore, no.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 12:21 PM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  4. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 06:15 PM
  5. infinite loop error
    By tsubasa in forum C Programming
    Replies: 7
    Last Post: 05-24-2006, 03:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21