c program help

This is a discussion on c program help within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to create a program that is supposed to calculate and print the average of several grades ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    c program help

    Hello, I am trying to create a program that is supposed to calculate and print the average of several grades entered by the user.

    The output is supposed to look something like this:
    ----------------------------------------------------------------------
    This program calculates the average of as many grades you wish to enter.



    First, enter the number of grades to process: 4


    Now enter the 4 grades to be averaged.


    Enter grade #1: 90
    Enter grade #2: 80
    Enter grade # 3: -20
    *** Invalid entry. Grade must be 0 to 100. ***
    Enter grade #3: 25
    Enter grade #4: 54


    The average of the 4 grades entered is 62


    You have a letter grade of D-
    ----------------------------------------------------------------------

    Here is what I have so far:
    -----------------------------------------------------------------------
    Code:
    #include <stdio.h>
    int main(void)
    {
    
    /* Output initial greeting and introduce program */
    /* --------------------------------------------- */
    
    printf ("This program caluculates the average of as many grades as you wish to enter.\n");
    
    /* Declare variables. */
    
    int number_of_grades, i, grade;
    int grade_total = 0;
    int invalid_entry = 0;
    float average;
    char letter_grade;
    char grade_A, grade_AB, grade_B, grade_BC, grade_C, grade_CD, grade_D, grade_F;
    
    
    /* Prompt user for number of grades to be entered. */
    /* ------------------------------------------------*/
    
    printf ("First, enter the number of grades to process: ");
    scanf ("%i", &number_of_grades);
    
    /* Output appropriate message based on number of grades entered. */
    /*---------------------------------------------------------------*/
    
    printf ("Now enter the %i grades to be averaged.\n", number_of_grades);
    
    for (i = 1; i <= number_of_grades; i++)
    
    {
    
    /* Prompt user for grades. */
    /* ----------------------- */
    
    printf ("Enter grade #%i: ", i);
    scanf ("%i",&grade);
    
    
    /* If grade is out of range (less than zero, OR greater than
    100), output error message, otherwise output grade entered.*/
    /* ---------------------------------------------------------- */
    
    if (grade > 100 || grade < 0)
    {
    printf ("*** Invaid entry. Grade must be 0 to 100. ***\n");
    i --; 
    }
    
    else
    
    {
    
    /* Assign values */ 
    /* --------------*/
    
    grade_total = grade_total + grade; 
    }
    
    } // end for loop
    
    
    /* Calculate average and display output */
    /* ------------------------------------ */
    
    average = (float) grade_total / number_of_grades;
    printf("\nThe average of the %i grades entered is: %.0f", number_of_grades, average);
    
    /* Assign values to letter grades */
    /* -------------------------------*/
    
    letter_grade = grade_total / number_of_grades;
    
    if ( (char) average >= 93 && <= 100) 
    {
    
    printf ("You have a letter grade of %c\n", grade_A);
    
    }
    
    else ( (char) average <= 92 && >= 88)
    }
    
    printf ("You have a letter grade of %c\n", grade_AB); 
    
    {
    if ( (char) average <= 87 && >= 83)
    }
    printf (" You have a letter grade of \n", grade_B);
    
    {
    
    else ( (char) average <= 82 && >= 78)
    }
    
    printf ("You have a letter grade of\n, grade_BC");
    
    {
    
    if ( (char) average<= 77 && >= 73)
    
    }
    printf ("You have a letter grade of C\n");
    
    {
    
    else ( (char) average <= 72 && >= 68)
    
    } 
    printf ("You have a letter grade of\n", grade_CD);
    
    {
    
    if ( (char) average <= 67 && >= 63)
    
    }
    printf ("You have a letter grade of\n", grade_D);
    
    { 
    
    else ( (char) average <= 62 && >= 0)
    
    }
    printf ("You have a letter grade of\n", grade_F);
    
    {
    
    
    } //end main
    ------------------------------------------------------------------------
    The program isn't working and I can't figure out how to fix it. I am getting errors on lines 76,86,91,98,105,112,119,and 126. Any help would be much appreciated.

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    For starters:

    Code:
    /** Prompt user for grades. */
    printf ("Enter grade #&#37;d: ", i);
    scanf ("%d", &grade);
    I'm still looking at the rest, so sit tight.

    Also, it looks like you don't initialize grade_A, grade_B, etc. You are printing them, but you never assign them a value. If you have to use the grade_A, grade_B char variables, you would have to do something like this:
    Code:
    char grade_A;
    grade_A = 'A';
    After you do that, THEN you can print them using printf.

    Another alternative is to remove all of those char variables.
    Code:
    	if (((char)average >= 93) && ((char)average <= 100)) 
    	{
    		printf ("You have a letter grade of 'A'.\n");
    	}
    	else if (((char) average <= 92) && ((char)average >= 88))
    	{
    		printf ("You have a letter grade of 'AB'.\n");
    	}
                    /** And so on . . . */
    Notice the structure of my if-else statements. They are different than how you did it. Your curly braces were all wonky and you didn't use else if, just else.

    Also, note that you never do anything with your variable letter_grade. You assign it a value, but you do not do anything further. Either remove that variable or you can calculate it as you do and then run a switch statement on that letter_grade (or a series of if-else conditionals).
    Last edited by JDGATX; 03-27-2008 at 12:42 PM.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    average >= 93 && <= 100
    How it comes out to be so different from the correct way you used before:
    grade > 100 || grade < 0
    Code:
    printf ("You have a letter grade of\n", grade_CD);
    What do you mean by that?

    do not forget that main returns int - so return 0; at the end of the function will be suitable addition.

    And of course - proper indentation never hurts...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Thanks for your help guys. I think I figured the program out.

    Code:
    #include <stdio.h>
    int main(void)
    {
    
    /* Output initial greeting and introduce program */
    /* --------------------------------------------- */
    
    printf ("This program calculates the average of as many grades as you wish to enter.\n\n");
    
    /* Declare variables. */
    
    int number_of_grades, i, grade;
    int grade_total = 0;
    int invalid_entry = 0;
    float average;
    char letter_grade;
    char grade_A, grade_AB, grade_B, grade_BC, grade_C, grade_CD, grade_D, grade_F;
    
    
    /* Prompt user for number of grades to be entered. */
    /* ------------------------------------------------*/
    
    printf ("First, enter the number of grades to process: ");
    scanf ("&#37;i", &number_of_grades);
    
    /* Output appropriate message based on number of grades entered. */
    /*---------------------------------------------------------------*/
    
    printf ("\nNow enter the %i grades to be averaged.\n\n", number_of_grades);
    
    for (i = 1; i <= number_of_grades; i++)
    
    {
    
    /* Prompt user for grades. */
    /* ----------------------- */
    
    printf ("Enter grade #%d: ", i);
    scanf ("%d",&grade);
    
    
    /* If grade is out of range output error message, otherwise output grade entered.*/
    /* ----------------------------------------------------------------------------- */
    
    if (grade > 100 || grade < 0)
    {
    printf ("*** Invaid entry. Grade must be 0 to 100.***\n");
    i --; 
    }
    
    else
    
    {
    
    /* Assign values */ 
    /* --------------*/
    
    grade_total = grade_total + grade; 
    }
    
    } // end for loop
    
    
    /* Calculate average and display output */
    /* ------------------------------------ */
    
    average = (float) grade_total / number_of_grades;
    printf("\nThe average of the %i grades entered is: %.0f", number_of_grades, average);
    
    /* Assign values to letter grades */
    /* -------------------------------*/
    
    letter_grade = grade_total / number_of_grades;
    
    if( average <= 62.0 ) 
    { 
        printf( "You have a letter grade of F\n" ) ;  
    } 
    else if( average < 67.0 ) 
    { 
        printf( "You have a letter grade of D\n"  ) ;  
    } 
    else if( average < 72.0 ) 
    { 
        printf( "You have a letter grade of CD\n"  ) ;  
    } 
    else if( average < 77.0 ) 
    { 
        printf( "You have a letter grade of C\n" ) ;  
    } 
    else if( average < 82.0 ) 
    { 
        printf( "You have a letter grade of BC\n" ) ;  
    } 
    else if( average < 87.0 ) 
    { 
        printf( "You have a letter grade of B\n" ) ;  
    } 
    else if( average < 92.0 ) 
    { 
        printf( "You have a letter grade of AB\n"  ) ;  
    } 
    else 
    { 
        printf( "You have a letter grade of A\n"  ) ;  
    }  
    
    
    
    } //end main
    But now I have another question:
    When I run the program it cuts out before showing the average. Is there a problem with my code or is it the compiler I'm using (Dev-C++).

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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