What am i doing wrong?

I'm supposed to write a program that inputs various student id numbers from a file, then takes the average of all their quiz scores and outputs their id numbers and avg quiz score. I was told to make a special case of the first record. But I am ridiculously confused. My awful code is below. Its not reading in anything from the file I've inputted

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
  #include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void main()

{
	ifstream fin;
	ofstream fout;

	int idnum, quizgrade, start, end, quizsum = 0, quizscorecount=0;
	float averagequizscore;
	fin.open("C:\\Users\\Gary\\Documents\\Programming 1 files\\Quizzes.doc");

	fout.open("hwcntrl.doc");
	fout.setf(ios::fixed);
	fout.setf(ios::showpoint);
	fout.precision(2);

	fin >> idnum >> quizgrade;
	fout << setw(20) << "ID Number" << setw(40) << "Quiz Score Average" << endl << endl;

	start = 10234;
	end = 10234;

	while (!fin.eof())
	{
		if (idnum==10234)
			quizscorecount++;
		quizsum = quizsum + quizgrade;
		averagequizscore = quizsum/quizscorecount;
		fout << setw(20) << idnum << setw(40)<< "Average Grade" << averagequizscore << endl << endl;

	}
		
	


}
Last edited on
closed account (SECMoG1T)
@gary special case of the first record what is this case u dint tell us.?
Plus i cant see where you checked to confirm if the file successfully openedif(fin.fail ()/ if(! fin) , how are your records arranged in that file.
Last edited on
Like so, first case is the first id num


10234 67
10234 100
10234 53
10234 91
11245 89
11245 46
11245 99
14652 100
14652 56
14652 99
14652 100
14652 96
14652 78
19832 92
19832 78
19832 51
19832 76
19832 89
20014 100
20014 100
20014 100
20014 100
21140 43
21140 56
21140 90
21140 78
21140 63
21140 67
21140 89
22256 10
22256 7
22256 7
22256 2
22256 11
22256 2
22256 2
22256 13
22256 9
27654 83
27654 83
27654 83
27654 83
30021 78
30021 67
30021 92
30021 59
30021 82
30021 58
hwcntrl.doc
doc file (I presume it is Microsoft Word file) are not text file. It is complex binary format which cannot be simply read as text. If you want to use text streams, you should save your file in plain text format.

Also:
1) You are looping on .eof(). Do not do that. Loop on input operation instead.
2) You do not read anything from file after first read. That means if your file contain some information when loop is executed, your program will hang.
closed account (SECMoG1T)
Ohh hadn't seen this hwcntrl.doc too
Last edited on
closed account (SECMoG1T)
Well I can give some bit of advice.
1
2
3
4
5
6
7
8
9
10
11
1.  I guess it's better to pass the filename to the steam constructor.  fstream fin ("file.txt"); 
     I prefer this. the file gets opened and closed after use.
2. It is always better to check if the file opened just fine you can use what
    I gave above. 
3. prefer not to use eof () @minnippa already tould you can use while (fin).

4 . I can't see where your reading the file in your loop,  well you can use getline and read it 
    into a stringstream object or a std::string then parse it to obtain your values. 

5. As i see your record you might want too hold the id no get the score average till the  id no changes.
   hope that helps.
Last edited on

I tried doing some different stuff this time. My program will not read in the file for some reason

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
 //Homework 4
//Gary Walker

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void main()

{
	ifstream fin;
	ofstream fout;

	int idnum, quizscore, quizcount = 0, quizsum = 0, min = 0, max = 0;
	float avg = quizcount / quizsum;

	fout.setf(ios::fixed);
	fout.setf(ios::showpoint);
	fout.precision(2);

		fin.open("C:\\Users\\Gary\\Documents\\Programming 1 files\\Quizzes.doc");

		fout << setw(25) << "Student ID" << setw(45) << "Average Score" << endl << endl;

		fin >> idnum >> quizscore;

		while (!fin.eof())

		{
			for (quizcount = 1; quizcount <= quizsum; quizcount++) 

			{
				fin >> quizscore;

				fout << setw(30) << idnum << setw(50) << avg << endl << endl;
			}



		}




}  
Because you are still reading them from binary file.
Topic archived. No new replies allowed.