469,588 Members | 2,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

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:
-----------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4.  
  5. /* Output initial greeting and introduce program */
  6. /* --------------------------------------------- */
  7.  
  8. printf ("This program caluculates the average of as many grades as you wish to enter.\n");
  9.  
  10. /* Declare variables. */
  11.  
  12. int number_of_grades, i, grade;
  13. int grade_total = 0;
  14. int invalid_entry = 0;
  15. float average;
  16. char letter_grade;
  17. char grade_A, grade_AB, grade_B, grade_BC, grade_C, grade_CD, grade_D, grade_F;
  18.  
  19.  
  20. /* Prompt user for number of grades to be entered. */
  21. /* ------------------------------------------------*/
  22.  
  23. printf ("First, enter the number of grades to process: ");
  24. scanf ("%i", &number_of_grades);
  25.  
  26. /* Output appropriate message based on number of grades entered. */
  27. /*---------------------------------------------------------------*/
  28.  
  29. printf ("Now enter the %i grades to be averaged.\n", number_of_grades);
  30.  
  31. for (i = 1; i <= number_of_grades; i++)
  32.  
  33. {
  34.  
  35. /* Prompt user for grades. */
  36. /* ----------------------- */
  37.  
  38. printf ("Enter grade #%i: ", i);
  39. scanf ("%i",&grade);
  40.  
  41.  
  42. /* If grade is out of range (less than zero, OR greater than
  43. 100), output error message, otherwise output grade entered.*/
  44. /* ---------------------------------------------------------- */
  45.  
  46. if (grade > 100 || grade < 0)
  47. {
  48. printf ("*** Invaid entry. Grade must be 0 to 100. ***\n");
  49. i --; 
  50. }
  51.  
  52. else
  53.  
  54. {
  55.  
  56. /* Assign values */ 
  57. /* --------------*/
  58.  
  59. grade_total = grade_total + grade; 
  60. }
  61.  
  62. } // end for loop
  63.  
  64.  
  65. /* Calculate average and display output */
  66. /* ------------------------------------ */
  67.  
  68. average = (float) grade_total / number_of_grades;
  69. printf("\nThe average of the %i grades entered is: %.0f", number_of_grades, average);
  70.  
  71. /* Assign values to letter grades */
  72. /* -------------------------------*/
  73.  
  74. letter_grade = grade_total / number_of_grades;
  75.  
  76. if ( (char) average >= 93 && <= 100) 
  77. {
  78.  
  79. printf ("You have a letter grade of %c\n", grade_A);
  80.  
  81. }
  82.  
  83. else ( (char) average <= 92 && >= 88)
  84. }
  85.  
  86. printf ("You have a letter grade of %c\n", grade_AB); 
  87.  
  88. {
  89. if ( (char) average <= 87 && >= 83)
  90. }
  91. printf (" You have a letter grade of \n", grade_B);
  92.  
  93. {
  94.  
  95. else ( (char) average <= 82 && >= 78)
  96. }
  97.  
  98. printf ("You have a letter grade of\n, grade_BC");
  99.  
  100. {
  101.  
  102. if ( (char) average<= 77 && >= 73)
  103.  
  104. }
  105. printf ("You have a letter grade of C\n");
  106.  
  107. {
  108.  
  109. else ( (char) average <= 72 && >= 68)
  110.  
  111. printf ("You have a letter grade of\n", grade_CD);
  112.  
  113. {
  114.  
  115. if ( (char) average <= 67 && >= 63)
  116.  
  117. }
  118. printf ("You have a letter grade of\n", grade_D);
  119.  
  120.  
  121. else ( (char) average <= 62 && >= 0)
  122.  
  123. }
  124. printf ("You have a letter grade of\n", grade_F);
  125.  
  126. {
  127.  
  128.  
  129. } //end main
  130.  
------------------------------------------------------------------------
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.

Thanks
Mar 27 '08 #1
1 1370
Banfa
9,064 Expert Mod 8TB
Normally I would ask you to post the text of the errors, however

this

Expand|Select|Wrap|Line Numbers
  1. if ( (char) average >= 93 && <= 100)
  2. {
  3.    ...
  4. }
  5.  
should be

Expand|Select|Wrap|Line Numbers
  1. if ( (char) average >= 93 && (char) average <= 100)
  2. {
  3.    ...
  4. }
  5.  
you can't short cut expressions in if statements
Mar 27 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by anuradha.k.r | last post: by
2 posts views Thread by stanlo | last post: by
7 posts views Thread by tyler_durden | last post: by
1 post views Thread by Willing 2 Learn | last post: by
66 posts views Thread by genestarwing | last post: by
12 posts views Thread by asif929 | last post: by
21 posts views Thread by asif929 | last post: by
reply views Thread by ashishbathini | last post: by
9 posts views Thread by C#_Help_needed | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.