Not Sure How to Get Rid of Error in Assignment

Can someone please take a look at my code and help me figure out what I'm doing wrong? I'm supposed to use arrays in a program that gets 10 grades from the user, averages them, and displays the result. If -1 is entered for a grade, it's not supposed to be counted as it's considered an assignment that hasn't been completed yet. It shouldn't factor into the average at all. If the user enters -1 10 times, the program displays --- instead of the average. For some reason, the averages I'm getting are off by 1. I've tried using floats instead of ints and adding cout.setf(ios::fixed) and cout.precision(0), but that doesn't fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/***********************************************************************
* Program:
*    Assignment 31, Array Design
*    Sister Unsicker, CS124
* Author:
*    Lanie Molinar
* Summary:
*    This program gets 10 grades from the user, averages them, and displays the
*    result.
*
*    Estimated:  2.0 hrs
*    Actual:     1.5 hrs
*      I had some difficulty getting the style checker to not complain about
*      my NUMGRADES constant. First, it didn't like the _ when I tried to
*      name it NUM_GRADES, and when I tried using NUM-GRADES, it complained
*      about there not being white space between operators. It finally stopped
*      complaining when I used NUMGRADES.
************************************************************************/

#include "stdafx.h"
#include <iostream>
using namespace std;

#define NUMGRADES 10

/***********************************************************************
* The function getGrades gets 10 grades from the user, passing them to main().
***********************************************************************/
void getGrades(float grades[], int num)
{
   for (int i = 0; i < num; i++)
   {
      cout << "Grade " << i + 1 << ": ";
      cin >> grades[i];
   }
   return;
}

/***********************************************************************
* The function averageGradesGrades averages the grades inputted by the user
* and returns that value to main().
***********************************************************************/
void averageGrades(float grades[], int num)
{
   float sum = 0;
   int notCompleted = 0;
   int i = 0;
   cout.setf(ios::fixed);
   cout.precision(0);
   for (i = 0; i < num; i++)
   {
      if (grades[i] != -1)
         sum += grades[i];
      else
         notCompleted++;
   }
   int average = sum / (num - notCompleted);
   if (notCompleted != num)
      cout << average;
   else
      cout << "---";
   return;
}

/**********************************************************************
*    The main function calls getGrades and averageGrades and displays the
*   result returned by averageGrades.
***********************************************************************/
int main()
{
   float grades[NUMGRADES];
   getGrades(grades, NUMGRADES);
   cout << "Average Grade: ";
   averageGrades(grades, NUMGRADES);
   cout << "%\n";
   return 0;
}
closed account (SECMoG1T)
If the user enters -1 10 times, the program displays --- instead of the average

1
2
3
4
5
6
 NUMGRADES 10 

if (notCompleted != num)   ///in here is your bug
      cout << average;
 else
      cout << "---";


the averages I'm getting are off by 1 int average = sum / (num - notCompleted); ///when you convert a floating point number to an integer you'll lose some precision and it's not a good idea.
Last edited on
I fixed this and changed my average variable to a float, but averages are still off by one point.
closed account (SECMoG1T)
remove: line 48 and 49
then change:
1
2
int average = sum / (num - notCompleted);//change this to
float average = sum / (num - notCompleted);
I finally figured it out. I didn't need to use floats after all because ints worked well. Also, I was missing an if statement, so my program kept trying to divide 0 by 0 in some cases, which made it crash.
Topic archived. No new replies allowed.