• 11-04-2012
fenway81
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```
• 11-04-2012
thames
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); }```
• 11-04-2012
fenway81
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.
• 11-04-2012
thames
Quote:

Originally Posted by fenway81
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```
• 11-04-2012
AndiPersti
Quote:

Originally Posted by fenway81
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