grade system to input and avg output

The
Last edited on
Because of floating point precision issues you probably want to make grade an int instead of a double (otherwise line 23 may not work the way you want)

line 26 could be: totalgrade1 += grade; // extra assignment is redundant (same with line 46)
line 27 could be: count++; // works fine as is though

As for entering in multiple sessions you will probably need to make another sentinel to check if the user wishes to enter another set of grades:
1
2
3
4
5
6
7
8
char enter = 'Y';
while(enter != 'N' && enter != 'n')
{
 // initialize session variables
 // do session...
  cout << "Would you like to enter another session? (Y/N): ";
  cin >> enter;
}
I don't think you need that count = count + 1 in your if (totalgrade > 0) statement. If I enter 100 for my score, count becomes 1 when it gets into that second while loop. If I then hit -1, count gets increased again in the if statement, leaving me with an average of 50.

Later down the line you increase count again, this is also going to mess things up.
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
#include <iostream>

using namespace std;

int main()
{
  double total = 0.0, average = 0.0; // our floating point values, initialized
  int grade = 0, section = 1, count = 0; // our integer values, initialized

  char more = 'Y'; // sentinel between sessions
  while(more != 'N' && more != 'n') // while user doesn't hit 'n' continue
  {
    while(grade != -1) // sentinel to stop entering grades
    {
      cout << endl << "Enter the Next Grade for Section " << section << " (-1 to stop): ";
      cin >> grade;
      if(grade >= 0) // to catch sentinel between iterations
      {
        total += grade; // sum grades
        count++; // increment grade count
      }
    }
    if(total) // just in case user didn't enter any grades
    {
      average = total / count; // calculate grade average
      cout << "Average Grade for Section " << section << ": " << average << endl; // display average
      cout << "Total number of grades: " << count << endl; // display grade count
    } else {
      cout << "No grades entered!" << endl;
    }
    cout << "Would you like to enter another session? (Y/N): ";
    cin >> more;
    grade = count = total = average = 0.0; // re-initialize variables
    section++; // increment session number
  }
  return 0;
}
Texan 40, Thank you I enjoyed you putting your comments in the code. Because I am new to this stuff (C++) it helps me understand as to what happens at what stage. Thank you again.
Topic archived. No new replies allowed.