Thread: Loop Error with program HELP!!!!!

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    2

    Exclamation Loop Error with program HELP!!!!!

    the loop I have for this program I need to create averaging grades (0-100) input by the user (min: 0 and max:10) is giving an incorrect number of loops when the user inputs the amount of grades to be processed. Please tell me what's incorrect about my loop and the avg calculations at the end.


    Code:
    #include <stdio.h>
    
    
    int main(void)
    
    
    {
    
    /* Declaration of Variables */
    /*--------------------------*/
    
        int    grade_counter = 0, counter[11], n = 0, i = 2;
        float  grade[100], sum = 0.0, average;
        char c;
    
    /* Output purpose of program being to calculate averages of user input grades */
    /*----------------------------------------------------------------------------*/
    
    printf("\nThis program will calculate the average of the grades you enter!\n");
    
    /* Prompt user to enter total number of grades user wishes to average */
    /*--------------------------------------------------------------------*/
    
    
        while(grade_counter <1 || grade_counter >10)
        {
            counter[i] = -1;
    printf("\nTo begin, please enter the total number of grades to process (1-10): ");
            scanf("%d", &grade_counter);
            while((c = getchar() != '\n') && c != EOF);
    
    /* Ensure grad is between 1 and 100. If not reprompt. */
    /*----------------------------------------------------*/
    
            if(grade_counter <1 || grade_counter >10)
    printf("*** Invalid Total Entered ***\n");
            else
                ++counter[grade_counter];
        }
    
    
    /* Instruct user to enter user specified number of grades and then prompt for grade to be averaged */
    /*-------------------------------------------------------------------------------------------------*/
    
    printf("\nNow enter the %i grades to be averaged.\n", grade_counter);
    
    printf("\nEnter grade #%i (0-100): ",n+=1);
        scanf("%f", &grade[n]);
        while(( c=getchar() != '\n') && c != EOF);
    
    
        while(n = 1, n = grade_counter, ++n, grade_counter++ < 10)
        {
            sum += grade[n];
    printf("\nEnter grade #%i (0-100): ", i++);
            scanf("%f", &grade[n]);
            while(( c=getchar() != '\n') && c != EOF);
    
    /* Ensure grad is between 1 and 100. If not reprompt. */
    /*----------------------------------------------------*/
    
            if(grade[n] <0 || grade[n] >100)
            {
    printf("*** Invalid Grade Entry ***.\n");
                continue;
            }
    
            grade_counter++;
    
        }
    
        average = sum / grade_counter;
    printf("The average of the %i grades entered is %f.", grade_counter, average);
    
        if(average >= 90)
    printf("You have a letter grade of A.\n");
        else if (average >=80)
    printf("You have a letter grade of B.\n");
        else if (average >=70)
    printf("You have a letter grade of C.\n");
        else if (average >=60)
    printf("You have a letter grade of D.\n");
        else
    printf("You have a letter grade of E.\n");
    
        getchar();
    }


    Output:




    This program will calculate the average of the grades you enter!


    To begin, please enter the total number of grades to process (1-10): 7


    Now enter the 7 grades to be averaged.


    Enter grade #1 (0-100): 90


    Enter grade #2 (0-100): 98


    Enter grade #3 (0-100): 78
    The average of the 12 grades entered is 0.000000.You have a letter grade of E.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2017, 08:05 PM
  2. do while loop error
    By nikiknight001 in forum C Programming
    Replies: 2
    Last Post: 02-19-2015, 10:27 AM
  3. Program using while loop, swtich, and for loop
    By Tre Holland in forum C Programming
    Replies: 5
    Last Post: 11-04-2014, 12:03 AM
  4. Replies: 3
    Last Post: 08-21-2012, 11:50 PM
  5. Replies: 10
    Last Post: 09-24-2010, 01:09 AM

Tags for this Thread