Quick question about adding a second error message

This is a discussion on Quick question about adding a second error message within the C Programming forums, part of the General Programming Boards category; i have written this program to calculates grades. Its works well but i need to add a error message if ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    20

    Quick question about adding a second error message

    i have written this program to calculates grades. Its works well but i need to add a error message if atleast two grades are not added and i am kinda stuck. here is my code any help would be appreciaed.

    Code:
    #include <stdio.h>
    int main(void)
    {
    
    /* Declare variables. */
    
    int number_of_grades, i, grade;
    int grade_total = 0;
    int invalid_entry = 0;
    float average;
    char letter_grade;
    
    /* Output initial greeting and introduce program */
    /* --------------------------------------------- */
    
    printf ("This program calculates the average of as many grades as you wish to enter.\n\n");
    
    /* 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 ("\n Now 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\n\n", number_of_grades, average);
    
    /* Assign values to letter grades */
    /* -------------------------------*/
    
    letter_grade = grade_total / number_of_grades;
     if( average <= 63.0 ) 
    
    { 
        printf( "You have a letter grade of F\n" ) ;  
    } 
     else if( average <= 66.0 ) 
    { 
        printf( "You have a letter grade of D\n"  ) ;  
    } 
     else if( average <= 69.0 ) 
    { 
        printf( "You have a letter grade of D+\n"  ) ;  
    } 
     else if( average <= 73.0 ) 
    { 
        printf( "You have a letter grade of C-\n" ) ;  
    } 
     else if( average <= 76.0 ) 
    { 
        printf( "You have a letter grade of C\n" ) ;  
    } 
     else if( average <= 79.0 ) 
    { 
        printf( "You have a letter grade of C+\n" ) ;  
    } 
     else if( average <= 83.0 ) 
    { 
        printf( "You have a letter grade of B-\n"  ) ;  
    } 
     else if( average <= 86.0 ) 
    { 
        printf( "You have a letter grade of B\n"  ) ;  
    }
     else if( average <= 89.0 ) 
    { 
        printf( "You have a letter grade of B+\n"  ) ;  
    }
     else if( average <= 93.0 ) 
    { 
        printf( "You have a letter grade of A-\n"  ) ;  
    }
     else 
    { 
        printf( "You have a letter grade of A\n"  ) ;  
    }  
    
     
    } //end main

  2. #2
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    You can use a counter, say, int num_invalid_grades = 0 and increment it (num_invalid_grades++) every time someone doesn't input a valid grade and test:

    Code:
     
     if(num_invalid_grades >= 2) 
     {
        printf(%d grades are invalid", num_invalid_grades);
        exit(0);
      }
    exit() is declared in <stdlib.h> and it means you want to close the program (probably because of an error).

    Edit:

    Another way is using mod (the operation returns the remainder of the division):

    Code:
    num_invalid_grades = total_grades % num_of_grades;
    if(num_invalid_grades >= 2) 
    { 
       printf("More than %d grades are invalid\n", num_invalid_grades);
       printf("Exiting the program...");
       exit(0);
    }
    Last edited by thames; 11-04-2012 at 08:35 AM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    i do not want to the program to exit it want it to tell the user they must enter atleast two grades. i tried a if else loop but i keep getting errors, i thought it would be the same as when i have the user enter a grade between 0-100. if i put a negative grade it prompts the user to put in the proper values.

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Quote Originally Posted by fenway81 View Post
    want it to tell the user they must enter at least two grades.
    Have you tried the logic I posted ?

    Edit:

    I noticed some problems in your code: @line 47 the correct decrement is i-- not i -- (notice the whitespace between i and '--') however, you don't need to decrement i because you're incrementing it in the for loop. @line 117 you forgot to write return 0 (int main expects you return 0):

    Code:
     
     int main(void) 
    { 
       
       return 0;
    }
    consider writting main like that as the first thing you will do in your code

    Compare your code with the below one:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    
    /* Declare variables. */
    
    int number_of_grades, grade_total, i, grade, num_invalid_grades;
        num_invalid_grades = grade_total = 0;
    
    float average;
    
    
    /* Output initial greeting and introduce program */
    /* --------------------------------------------- */
    
    printf ("This program calculates the average of as many grades as you wish to enter.\n\n");
    
    /* 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 ("\n Now 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");
       num_invalid_grades++;
    }
    
     else
    {
    
    /* Assign values */ 
    /* --------------*/
    
    grade_total = grade_total + grade; 
    
    }
    }  // end for loop
    
    if(num_invalid_grades >= 2) 
    { 
      printf("There are more than %d invalid grades\n", num_invalid_grades);
      exit(0);    
    }     
    
    /* Calculate average and display output */
    /* ------------------------------------ */
    
    average = (float) grade_total / number_of_grades;
    printf("\nThe average of the %i grades entered is: %.0f\n\n", number_of_grades, average);
    
    /* Assign values to letter grades */
    /* -------------------------------*/
    
    
     if( average <= 63.0 ) 
    
    { 
        printf( "You have a letter grade of F\n" ) ;  
    } 
     else if( average <= 66.0 ) 
    { 
        printf( "You have a letter grade of D\n"  ) ;  
    } 
     else if( average <= 69.0 ) 
    { 
        printf( "You have a letter grade of D+\n"  ) ;  
    } 
     else if( average <= 73.0 ) 
    { 
        printf( "You have a letter grade of C-\n" ) ;  
    } 
     else if( average <= 76.0 ) 
    { 
        printf( "You have a letter grade of C\n" ) ;  
    } 
     else if( average <= 79.0 ) 
    { 
        printf( "You have a letter grade of C+\n" ) ;  
    } 
     else if( average <= 83.0 ) 
    { 
        printf( "You have a letter grade of B-\n"  ) ;  
    } 
     else if( average <= 86.0 ) 
    { 
        printf( "You have a letter grade of B\n"  ) ;  
    }
     else if( average <= 89.0 ) 
    { 
        printf( "You have a letter grade of B+\n"  ) ;  
    }
     else if( average <= 93.0 ) 
    { 
        printf( "You have a letter grade of A-\n"  ) ;  
    }
     else 
    { 
        printf( "You have a letter grade of A\n"  ) ;  
    } 
    return 0; 
    } //end main
    Last edited by thames; 11-04-2012 at 10:27 AM.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by fenway81 View Post
    i do not want to the program to exit it want it to tell the user they must enter atleast two grades
    Do you mean that you want to repeat the input if the user enters a number less than two at this point?
    Code:
    printf ("First, enter the number of grades to process: ");
    scanf ("%i", &number_of_grades);
    Then I suggest a do-while-loop around it.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. directinput question, error message
    By WaterNut in forum C Programming
    Replies: 3
    Last Post: 06-22-2011, 12:56 AM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  3. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 04:54 PM
  4. question about an error message.
    By revelation437 in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2003, 11:17 PM
  5. Question about error message...
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2002, 05:52 PM

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