how to output more than one solution to an expression

This is my program to input student ids and a group of quiz scores and calculate the average for each student as well as the highest and lowest average of all students. I got my program to run successfully, but when I check it against my test data I run into a problem when two or more students have the same average. My question is how to get my output to display more than one student id?

Here is a sample input:
Stud ID: # of Exams: Scores:
1111 3 20 20 20
1112 2 12 14
1113 4 11 19 13 16
1114 3 20 20 20

This is for a 100 lvl intro to C++ class so I would appreciate any "low skill" help=). The commented out if statements with the variables set to ????????? are where I'm stuck. I appreciate any suggestions or tips.

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
79
80
81
82
83
84
85
86
87
88
89
90
#include <iomanip>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{

  ifstream inFile;
  //open in.data file
  inFile.open("in.data");
  //format output & set decimal precision
  cout.setf(ios::fixed);
  cout.precision(2);
  //declare variables
  int studId, studCount, count, totalQuizCount, minStud, maxStud;
  float avg, quizScore, studTot, howManyQuiz, quizTotal, avgOfAllQuizzes;
  float min = 100.0;
  float max = 0.0; 
  //begin output
  cout << "---- Student Quiz Report ----" << endl << endl;
  cout << setw(20) << left << "Student ID" << setw(10) << "Average of Quizzes" << endl;
  cout << setw(20) << left << "----------" << setw(10) << "------------------" << endl;
  //initialize studCount
  studCount = 0;
  //input studId & howManyQuiz
  inFile >> studId >> howManyQuiz;
  
  while(inFile)
  {
  	//increment studCount & begin running sum for totalQuizCount
	  studCount = studCount++;
    totalQuizCount += howManyQuiz;

	  cout << setw(20) << left << studId;
    //initialize studTot & count
	  studTot = 0;
	  count = 0;

    while(count < howManyQuiz)
    {
    	//input quizScore, begin running sum for quizTotal, & increment count 
  	  inFile >> quizScore;
      quizTotal += quizScore;
  	  count++;
      studTot = studTot + quizScore; 
    }
    //calculate avg & avgOfAllQuizzes
    avg = studTot/howManyQuiz;
    avgOfAllQuizzes = quizTotal / totalQuizCount;
    cout << setw(11) << right << avg << endl;
    //determine max/min average and maxStud/minStud
    if(avg > max)
    {
      max = avg;
      maxStud = studId;
    }

//    if(avg == max)
//    {
//    	max = avg;
//    	maxStud = ??????????????;
//    }

    if(avg < min)
    {
      min = avg;
    	minStud = studId;
    }

//    if(avg == min)
//    {
//    	min = avg;
//    	maxStud = ?????????????;
//    }	
    //repeat loop
    inFile >> studId >> howManyQuiz; 
  } 
  
  cout << endl;
  cout << "Average of all Quizzes: " << avgOfAllQuizzes << endl << endl;
  cout << "Student(s): " << maxStud << " had the highest quiz average of: " << max << endl;
  cout << "Student(s): " << minStud << " had the lowest quiz average of: " << min << endl << endl;;
  cout << "There were " << studCount << " students and " << totalQuizCount << " quizzes in file." << endl << endl;
  //close input file
  inFile.close();

  return 0;
}
You variables were not initialized. For example variable totalQuizCount was not initialized and as the result it has an undefined value after statement

totalQuizCount += howManyQuiz;

You need not to consider condition avg == max because it does not influence on the maximum (or minimum) value of averages.

Last edited on
Topic archived. No new replies allowed.