Why will this not create an outfile.

It's not showing any errors but it will not put the id, testave etc into the outfile also. The file is created but only has the header not the rest of the information.

The infile is this:

123456789 77.2 88.3 22 28 35 45 33 35 40
234567890 97.5 90 25 30 38 48 34 35 50
345678901 82.4 77.5 22.5 27 35.5 44 35 33 48
456789012 65.5 67 20 25 28 40 27 25 35
567890123 79.5 82 25 30 32 47 30 33 46
678901234 90 86.5 25 30 40 46 34 35 50
789012345 77 82.2 25 30 36 46 33 32.5 47
890123456 67.7 72.5 23 28 35 44 30 30 44
901234567 76.5 83.4 25 29 38 49 35 35 49



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
  #include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;

struct SStudent
{
	int id;
	double test[2], hw[7], hwAve, testAve, finalScore, grade;
};

int getInput(SStudent [], int);
void process(SStudent [], int);
void display(const SStudent [], int);

int main()
{
	int totalStudents, i;
	SStudent student[30];

	totalStudents = getInput(student, 30);
	process(student, totalStudents);
	display(student, totalStudents);

	_getch();
}

int getInput(SStudent students[], int size)
{
	int i = 0;
	ifstream inFile;
	inFile.open("scores.txt");
	if(!inFile)
	{
		cout << "Can't open input file\n";
		_getch();
		exit(1);
	}

	for(int i = 0; i < size; ++i)
	{
		inFile >> students[i].id;

		for(int j = 0; j < 2; ++j)
			inFile >> students[i].test[j];

		for (int k = 0; k < 7; ++k)
			inFile >> students[i].hw[k];

		if(!inFile) break;
	}

	return i;
}

void process(SStudent students[], int size)
{
	for(int i = 0; i < size; ++i)
	{
		for(int j = 0; j < 2; ++j)
			students[i].testAve += students[i].test[j];

		for(int k = 0; k < 7; ++k)
			students[i].hwAve += students[i].hw[k];

		students[i].hwAve = students[i].hwAve / 265;
		students[i].testAve = students[i].testAve / 200;
		students[i].finalScore = (students[i].hwAve * 0.40) + (students[i].testAve * 0.60) * 100;

		if (students[i].finalScore < 60)
			students[i].grade = 'E';

		else if (students[i].finalScore > 60 && students[i].grade <= 69)
			students[i].grade = 'D';

		else if (students[i].finalScore > 69 && students[i].grade <= 79)
			students[i].grade = 'C';

		else if (students[i].finalScore > 79 && students[i].grade <= 89)
			students[i].grade = 'B';

		else
			students[i].grade = 'A';
	}
}

void display(const SStudent students[], int size)
{
	ofstream outFile;
	outFile.open("grades.txt");
	outFile << fixed << showpoint;  
    outFile << setprecision(1);

    outFile << "STUDENT ID\tHW AVE\t\tTEST AVE\tFINAL SCORE\tGRADE" << endl;
	for(int i = 0; i < size; ++i)
		cout << students[i].id << "\t" << students[i].hwAve << "\t\t" << students[i].testAve << "\t\t" << students[i].finalScore << "\t\t" << students[i].grade << endl;


	cout << "Output file created.";
}
Maybe change line 97 to use outFile instead of cout.
It still doesn't work
closed account (N36fSL3A)
I'm not going to read this, mainly because I do not know where to look. Comment your code while you're writing. This will help you find future bugs like this. ;)
Well since its not displaying, im guessing its in the display function or I didn't call it right in main
It still doesn't work


Change display to the following, then track down your problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void display(const SStudent students[], int size)
{
    std::cerr << "In display, size is " << size << ".\n" ;

    ofstream outFile("grades.txt") ;
    outFile << fixed << showpoint << setprecision(1);
    outFile << "STUDENT ID\tHW AVE\t\tTEST AVE\tFINAL SCORE\tGRADE" << endl;
    for(int i = 0; i < size; ++i)
        outFile << students[i].id << "\t" << students[i].hwAve << "\t\t" << students[i].testAve << "\t\t" << students[i].finalScore << "\t\t" << students[i].grade << endl;

    if ( outFile )
        cout << "Output file created.\n";
    else
        cout << "Output file not created.\n" ;
}
Last edited on
I did and size is equal to 0. So now its running the for loop because I isn't less than size. I thought my size equaled how many students it read in.
Function getInput() will always return 0 as the value of totalStudents.
That's because int i = 0; is defined at line 31. Then a new variable int i is declared inside the for loop at line 41.

Just put for(i = 0; i < size; ++i) without the keyword int so as to use the previously defined i.
Topic archived. No new replies allowed.