loops

Okay I'm back. I am writing a program on averaging test scores of students. I am trying to use loops to input all of my data, however I am stuck on what that data will be stored as after all of the grades have been entered. For example, say I want to get the average grade of the entire class. If I have grade 1, 2, 3, and 4 entered by 4 different students, how would I input that into the program, as their would be 4 different grade 1's?

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
//06/30/2017
//Average Program
//program averages grades

#include <iostream>
#include <stdio.h>

using namespace std;

int main()

{
					//student 1

	int sID, grade1, grade2, grade3, grade4;

	while (sID >= 0) {
		cout << "Enter the students ID, press 0 if no more students " << endl;
		cin >> sID;

		cout << " Student ID is " << sID << endl;

		cout << "1st test grade" << endl;
		cin >> grade1;
		cout << "2nd test grade" << endl;
		cin >> grade2;
		cout << "3rd test grade." << endl;
		cin >> grade3;
		cout << "4th test grade";
		cin >> grade4;

	}

	cout "Average score for the class is " << (grade1 + grade2 + grade3)/4





	system("pause");
	return 0;
}
Last edited on
You'll have to keep up with them as you go.
You'll have to keep up with the totals for each as they are entered.
If you need the scores of only 1 student you need only 2 variables, one for the total where you add all the grades and one for the number of grades entered.

One way to do 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
#include <iostream>

using namespace std;

int main()
{
  int num_grades = 0; int total_grades = 0;

  cout << "Welcome at the grading program.";
  cout << "\nEnter a number of grades - finish with 0\n";

  while (true)
  {
    cout << "\nEnter grade #" << num_grades + 1 << "\t";
    int temp;
    cin >> temp;
    if (temp == 0)
      break;

    num_grades++;
    total_grades += temp;
  }

  cout << "\n\nNumber of grades entered: " << num_grades;
  cout << "\n\nTotal: " << total_grades;
  cout << fixed;
  cout.precision(2);
  cout << "\n\nAverage grade: " << double(total_grades) / double(num_grades);

  return 0;
}
One way to do 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
#include <iostream>

using namespace std;

int main()
{
  int num_grades = 0; int total_grades = 0;

  cout << "Welcome at the grading program.";
  cout << "\nEnter a number of grades - finish with 0\n";

  while (true)
  {
    cout << "\nEnter grade #" << num_grades + 1 << "\t";
    int temp;
    cin >> temp;
    if (temp == 0)
      break;

    num_grades++;
    total_grades += temp;
  }

  cout << "\n\nNumber of grades entered: " << num_grades;
  cout << "\n\nTotal: " << total_grades;
  cout << fixed;
  cout.precision(2);
  cout << "\n\nAverage grade: " << double(total_grades) / double(num_grades);
 
  return 0;
}
Topic archived. No new replies allowed.