Loop Problems

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
// Function prototypes
void avgGrd(int, int, int, int, int, double&, string&);
int findLow(int, int, int, int);
int findHigh(int, int, int, int);
int qfAvg(double, int, double&);

void main()
{
	// Variable declarations
	ifstream inFile;
	string lastName, firstName, grade, file;
	char end;
	int ID, q1, q2, q3, q4, final, count = 0, searchId;
	double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sumFinal = 0, sumAvg = 0;
	int q1Count = 0, q2Count = 0, q3Count = 0, q4Count = 0, finalCount = 0, avgCount = 0;
	double avg;
	double average;

	
	cout << "Enter file name: "; cin >> file;
	inFile.open(file + ".txt");
	if (!inFile.fail())
	{
		cout << fixed << setprecision(1);
		cout << endl << setw(53) << "Fall 2012 Student Grade Report:" << endl << endl;
		cout << "No.    ID   Last Name    First Name  Quiz1 Quiz2 Quiz3 Quiz4 Final  Avg   Grd" << endl;
		cout << "------------------------------------------------------------------------------" << endl;
		inFile.ignore(80, '\n');
		inFile >> ID;
		while (!inFile.eof())
		{

			count++;
			inFile >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
			sum1 += q1; sum2 += q2; sum3 += q3; sum4 += q4; sumFinal += final; 
			avgGrd(q1, q2, q3, q4, final, average, grade);
			sumAvg += average;
			cout << right << setw(3) << count << " " << setw(6) << ID << "  " << left 
				<< setw(12) << lastName << " " << left << setw(12) << firstName << " ";
				if (q1 > 0)
				{
					cout <<right << setw(3) << q1;
					q1Count++;
				}
				else
					cout << "   ";
				if (q2 > 0)
				{
					cout << right << setw(6) << q2;
					q2Count++;
				}
				else
					cout << "      ";
				if (q3 > 0)
				{
					cout << right << setw(6) << q3;
					q3Count++;
				}
				else
					cout << "      ";
				if (q4 > 0)
				{
					cout << right << setw(6) << q4;
					q4Count++;
				}
				else
					cout << "      ";
			cout << right << setw(5) << final << right << setw(8) << average << "   " << left << setw(7) << grade << endl;
			inFile >> ID;
		}
		inFile.close();
		cout << "                                      ----------------------------------------" << endl;
		cout << "                                       ";
		qfAvg(sum1, q1Count, avg);
		cout << setw(6) << avg;
		qfAvg(sum2, q2Count, avg);
		cout << setw(6) << avg; 
		qfAvg(sum3, q3Count, avg);
		cout << setw(6) << avg;
		qfAvg(sum4, q4Count, avg);
		cout << setw(6) << avg;
		qfAvg(sumFinal, count, avg);
		cout << setw(5) << avg;
		qfAvg(sumAvg, count, avg);
		cout << setw(5) << avg << endl;

		cout << "Enter Student ID (999 to end): ";  cin >> searchId;
		inFile.open(file + ".txt");
		inFile.ignore(80, '\n');
		while (searchId != 999 && searchId != ID)
		{
			for (int i = 0; i <= count; i++)
			{
				inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
				if (searchId == ID)
				{
					avgGrd(q1, q2, q3, q4, final, average, grade);
					cout << "  " << left << setw(12) << lastName << "  " << setw(12) << firstName << ' ' 
						<< setw(3) << average << ' ' << setw(2) << grade << endl;
					cout << "Enter Student ID (999 to end): ";  cin >> searchId;	
				}
			}	
		}cout << "Enter any character: ";  cin >> end;
	}
	else
		cout << endl << "File Open Failed!" << endl;
}

// Function definitions
void avgGrd(int q1, int q2, int q3, int q4, int final, double& average, string& grade)
{	
	int low = findLow(q1, q2, q3, q4);
	int high = findHigh(q1, q2, q3, q4);
	average = ((q1 + q2 + q3 + q4 + final + final) - low + high) / 6.;
	if (average >= 96.66)
		grade = "A+";
	else if (average >= 93.33)
		grade = 'A';
	else if (average >= 90)
		grade = "A-";
	else if (average >= 86.66)
		grade = "B+";
	else if (average >= 83.33)
		grade = 'B';
	else if (average >= 80)
		grade = "B-";
	else if (average >= 76.66)
		grade = "C+";
	else if (average >= 73.33)
		grade = 'C';
	else if (average >= 70)
		grade = "C-";
	else if (average >= 66.66)
		grade = "D+";
	else if (average >= 63.33)
		grade = 'D';
	else if (average >= 60)
		grade = "D-";
	else
		grade = 'F';

}
int findLow(int q1, int q2, int q3, int q4)
{
	int low = q1;
	if (q2 < low)
		low = q2;
	if (q3 < low)
		low = q3;
	if (q4 < low)
		low = q4;
	return low;
}
int findHigh(int q1, int q2, int q3, int q4)
{
	int high = q1;
	if (q2 > high)
		high = q2;
	if (q3 > high)
		high = q3;
	if (q4 > high)
		high = q4;
	return high;
}
int qfAvg(double sum, int Count, double& avg)
{
	avg = sum / Count;
	return avg;
}

The main part of this code works properly. It outputs everything correctly up until the point where I have to input ID's and get the output. It will only allow me to do it 2 - 3 times and then it just does nothing and I can't get it to output "Record not found" for incorrect ID's without either being in an infinite loop of "Record not found" or having it say Record not found 49 times (number of actual ID's it could be). I'm really stuck on this and have been at it for days! Please Help!
Enter file name: class

                      Fall 2012 Student Grade Report:

No.    ID   Last Name    First Name  Quiz1 Quiz2 Quiz3 Quiz4 Final  Avg   Grd
------------------------------------------------------------------------------
  1  32998  Benway       Eileen        76    98    80    75   95    90.3   A-
  2  15246  Berling      Danielle      85    64          69   85    78.8   C+
  3  94304  Bowdy        James               57    70    92   98    84.5   B
  4  50838  Burr         Jermiah       83    84    94    77   63    80.2   B-
  5  84511  Calluari     Henry         72    84    97    73   74    83.2   B-
  6  77921  Connors      Sarah         52    58    88    61   78    75.2   C
  7  24609  Cooper       Camille       62    93    92    58   66    78.7   C+
  8  28248  Courville    David         81    88    89    64   87    86.8   B+
  9  61084  Cruz         Waldemar      70    68    76   100   86    86.3   B
 10  10364  Denaro       Tony          73    85    55    68   89    81.5   B-
 11  17600  DeNinno      David         62    68    93    84   75    81.3   B-
 12  54321  Doe          John                                 50    16.7   F
 13  61316  Eaton        John          80    51    93    90   69    82.3   B-
 14  44520  Ecklord      Ryan                61    56    98   83    79.8   C+
 15  58751  Flores       Jose                80          87   69    65.3   D
 16  54378  Franco-Banks Nixaliz       99    58    80    63   83    84.5   B
 17  18140  Galotti      Salvator      78    65    90    88   98    90.3   A-
 18  36278  Hartman      Kristine     100    62    71    69   55    75.0   C
 19  85736  Hepburn      Spencer       71          62    52   51    59.7   F
 20  43865  Hildebrandt  Stephenson    97    94    93    82  100    96.8   A+
 21  72054  Houde        Jessica       72    66    71    75  100    82.2   B-
 22  10984  Juste        Kerlande      97    73    98    96   57    83.8   B
 23  55937  Kantzios     Angela        55          78    93   76    78.5   C+
 24  39473  Kenyon       Patricia      65    54    90    94   97    89.5   B+
 25  59640  Knieriem     Brandon       52    87    97    97   56    81.7   B-
 26  80886  Laflamme     Nicole        76    51    71    69   91    79.0   C+
 27  16137  Littlefield  Arionna       71    85    74    69   95    84.2   B
 28  12127  McCabe       Kelly         51    96          54   94    80.8   B-
 29  65328  Middleton    Middle        75          75    75   75    75.0   C
 30  87129  Morang       Nicholas      77    67    57    88   78    79.3   C+
 31  28810  Navin        Joshua        94    75    62    64   94    85.8   B
 32  62631  Niedojadlo   Evan          73    66    83    97   88    87.7   B+
 33  87361  Perlmutter   Diadre        96    60    81    81   74    83.7   B
 34  73984  Phaneuf      Lesley        78    85    57    68   83    80.3   B-
 35  45256  Rodrigues    Joana         58    75    58    59   92    75.2   C
 36  42331  Rose         Nicole        83    50    96    54   93    85.8   B
 37  21705  Roy          Jake          86          89    82   98    90.3   A-
 38  39234  Spaulding    Nicholas      61          76    92   54    71.5   C-
 39  44102  Spence       Arthur        85    84   100    91   62    83.3   B
 40  14508  Stronach     Kurt          80    95    96    78   59    80.8   B-
 41  78575  Suslovic     Vikilynn      65    54    74    61   76    71.0   C-
 42  31571  Toporowski   Crystal       93          77    63   58    73.7   C
 43  99189  Vasquez      Oskar         66    60          63   52    59.8   F
 44  12345  Volkov       George       100   100   100   100  100   100.0   A+
 45  77463  Weber        Jessica       60    56    77    81   69    72.8   C-
 46  52458  Whitten      Sarah         99    58    94    81   70    85.5   B
 47  81984  Williams     Jenny         55    67    54    89   75    75.0   C
 48  37915  Wright       Michelle      98    83    56    63   67    79.3   C+
 49  33580  Ziolko       John          74    64    98    98   79    87.7   B+
                                      ----------------------------------------
                                       76.4  72.1  80.0  77.6  77.9 79.6
Enter Student ID (999 to end): 32998
  Benway        Eileen       90.3 A-
Enter Student ID (999 to end): 12345
  Volkov        George       100.0 A+
Enter Student ID (999 to end): 12127

This is what it looks like when I run it.
Enter file name: class

                      Fall 2012 Student Grade Report:

No.    ID   Last Name    First Name  Quiz1 Quiz2 Quiz3 Quiz4 Final  Avg   Grd
------------------------------------------------------------------------------
  1  32998  Benway       Eileen        76    98    80    75   95    90.3   A-
  2  15246  Berling      Danielle      85    64          69   85    78.8   C+
  3  94304  Bowdy        James               57    70    92   98    84.5   B
  4  50838  Burr         Jermiah       83    84    94    77   63    80.2   B-
  5  84511  Calluari     Henry         72    84    97    73   74    83.2   B-
  6  77921  Connors      Sarah         52    58    88    61   78    75.2   C
  7  24609  Cooper       Camille       62    93    92    58   66    78.7   C+
  8  28248  Courville    David         81    88    89    64   87    86.8   B+
  9  61084  Cruz         Waldemar      70    68    76   100   86    86.3   B
 10  10364  Denaro       Tony          73    85    55    68   89    81.5   B-
 11  17600  DeNinno      David         62    68    93    84   75    81.3   B-
 12  54321  Doe          John                                 50    16.7   F
 13  61316  Eaton        John          80    51    93    90   69    82.3   B-
 14  44520  Ecklord      Ryan                61    56    98   83    79.8   C+
 15  58751  Flores       Jose                80          87   69    65.3   D
 16  54378  Franco-Banks Nixaliz       99    58    80    63   83    84.5   B
 17  18140  Galotti      Salvator      78    65    90    88   98    90.3   A-
 18  36278  Hartman      Kristine     100    62    71    69   55    75.0   C
 19  85736  Hepburn      Spencer       71          62    52   51    59.7   F
 20  43865  Hildebrandt  Stephenson    97    94    93    82  100    96.8   A+
 21  72054  Houde        Jessica       72    66    71    75  100    82.2   B-
 22  10984  Juste        Kerlande      97    73    98    96   57    83.8   B
 23  55937  Kantzios     Angela        55          78    93   76    78.5   C+
 24  39473  Kenyon       Patricia      65    54    90    94   97    89.5   B+
 25  59640  Knieriem     Brandon       52    87    97    97   56    81.7   B-
 26  80886  Laflamme     Nicole        76    51    71    69   91    79.0   C+
 27  16137  Littlefield  Arionna       71    85    74    69   95    84.2   B
 28  12127  McCabe       Kelly         51    96          54   94    80.8   B-
 29  65328  Middleton    Middle        75          75    75   75    75.0   C
 30  87129  Morang       Nicholas      77    67    57    88   78    79.3   C+
 31  28810  Navin        Joshua        94    75    62    64   94    85.8   B
 32  62631  Niedojadlo   Evan          73    66    83    97   88    87.7   B+
 33  87361  Perlmutter   Diadre        96    60    81    81   74    83.7   B
 34  73984  Phaneuf      Lesley        78    85    57    68   83    80.3   B-
 35  45256  Rodrigues    Joana         58    75    58    59   92    75.2   C
 36  42331  Rose         Nicole        83    50    96    54   93    85.8   B
 37  21705  Roy          Jake          86          89    82   98    90.3   A-
 38  39234  Spaulding    Nicholas      61          76    92   54    71.5   C-
 39  44102  Spence       Arthur        85    84   100    91   62    83.3   B
 40  14508  Stronach     Kurt          80    95    96    78   59    80.8   B-
 41  78575  Suslovic     Vikilynn      65    54    74    61   76    71.0   C-
 42  31571  Toporowski   Crystal       93          77    63   58    73.7   C
 43  99189  Vasquez      Oskar         66    60          63   52    59.8   F
 44  12345  Volkov       George       100   100   100   100  100   100.0   A+
 45  77463  Weber        Jessica       60    56    77    81   69    72.8   C-
 46  52458  Whitten      Sarah         99    58    94    81   70    85.5   B
 47  81984  Williams     Jenny         55    67    54    89   75    75.0   C
 48  37915  Wright       Michelle      98    83    56    63   67    79.3   C+
 49  33580  Ziolko       John          74    64    98    98   79    87.7   B+
                                      ----------------------------------------
                                       76.4  72.1  80.0  77.6  77.9 79.6
Enter Student ID (999 to end): 32998
  Benway        Eileen       90.3 A-
Enter Student ID (999 to end): 12345
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
Record not found!
  Volkov        George       100.0 A+
Enter Student ID (999 to end):

And here I changed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "Enter Student ID (999 to end): ";  cin >> searchId;
		inFile.open(file + ".txt");
		inFile.ignore(80, '\n');
		while (searchId != 999 && searchId != ID)
		{
			for (int i = 0; i <= count; i++)
			{
				inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
				if (searchId == ID)
				{
					avgGrd(q1, q2, q3, q4, final, average, grade);
					cout << "  " << left << setw(12) << lastName << "  " << setw(12) << firstName << ' ' 
						<< setw(3) << average << ' ' << setw(2) << grade << endl;
					cout << "Enter Student ID (999 to end): ";  cin >> searchId;	
				}
				else cout << "Record not found!" << endl;
			}	

And that's what happened. So clearly there is something not right.
Topic archived. No new replies allowed.