File output is null

So I have a programming assignment from class, I've coded everything how I believe it should be done, the code has no syntax errors, it compiles and runs, yet the output file remains empty. I can't tell if the issue is the data isn't reading in from a file to array or in the fstream out to a file is flaw. Any pointers and criticism would be appreciated. Thanks!
The prompt is for a program intaking students' first and last names, then test score, assigning a letter grade and then outputting the winner, all to a separate file. We're required to use a struct not a class and have only function calls and file management in main.

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

#include <iostream>
#include <fstream>
#include <string>


//declare structure 
struct studentType
{
	std::string studentFname;
	std::string studentLname;
	std::string winner; 
	int testScore;
	char grade;
};

//declare array
studentType students[20]; 

//declare functions
void read_data_into_array();
void assign_grade_to_each_student();
int find_highest_test_score(int winner);
void announce_winner();


int main()
{
	//open files
	std::ifstream infile;
	infile.open("ex2data.txt");
	std::ofstream outfile;
	outfile.open("ex2out.txt");

	//run functions
	void read_data_into_array();
	void assign_grade_to_each_student();
	void announce_winner();
	


	//close files 
	outfile.close();
	infile.close(); 

	system("pause");
	return 0;

}

void read_data_into_array()
{
	int location;
	std::ifstream infile;
	
	
	if (infile.is_open())
	{

		//loop through text file to place in array
		for (int i = 0; i < 20; ++i)
		{
				infile	>> students[i].studentFname
						>> students[i].studentLname
				        >> students[i].testScore;
		}
	}

}

void  assign_grade_to_each_student()
{
	//variable for position in String
	int gradePercent;

	//loop though test scores, assigning letter grade to Char 
	for (int i = 0; i < 20; ++i)
	{
		gradePercent = students[i].testScore;

		if (gradePercent > 100 && gradePercent < 90)
		{
			students[i].grade = 'A';
		}
		else if (gradePercent > 90 && gradePercent < 80)
		{
			students[i].grade = 'B';
		}
		else if (gradePercent > 70 && gradePercent < 60)
		{
			students[i].grade = 'B';
		}
		else if (gradePercent > 50 && gradePercent < 60)
		{
			students[i].grade = 'D';
		}
		else
		{
			students[i].grade = 'F';
		}
	}
}

int find_highest_test_score(int winner)
{
	int currentScore;
	int highestScore;
	std::string score;

	highestScore = 0;

	for (int i = 0; i < 20; ++i)
	{
		currentScore = students[i].testScore;

		//if the score is higher than the last score, copy array component to winner
		if (currentScore > highestScore)
		{
			winner = students[i].testScore;
		}

	}
	return winner; 
}


void announce_winner()
{
	std::ofstream outfile;
	//call function for winner
	int score = find_highest_test_score(score); 

	outfile << "student name" << "Test Score" << "Grade \n";

	for (int i = 0; i < 20; ++i)
	{
		outfile << students[i].studentLname << ", " << students[i].studentFname << students[i].testScore << students[i].grade;
	}

	outfile << "The highest Score is " << score; 
	
	for (int i = 0; i < 10; ++i) 
	{
	
		if (students[i].testScore == score)
		{
			outfile << "The Winning student(s) are: /n";
			outfile << students[i].studentLname << ", " << students[i].studentFname;
			
		}
	}

}
the code has no syntax errors, it compiles and runs


In function 'void read_data_into_array()':
53:6: warning: unused variable 'location' [-Wunused-variable]
In function 'void announce_winner()':
131:43: warning: 'score' may be used uninitialized in this function [-Wmaybe-uninitialized]


Warnings are your friend :+)

Why does find_highest_test_score take an argument?
Hope the following code can give you some hints:
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
// So I have a programming assignment from class, I've coded everything
// how I believe it should be done, the code has no syntax errors,
// it compiles and runs, yet the output file remains empty.
// I can't tell if the issue is the data isn't reading in from a file to array
// or in the fstream out to a file is flaw.
// Any pointers and criticism would be appreciated.
// Thanks!
// The prompt is for a program intaking students' first and last names,
// then test score, assigning a letter grade and then outputting the winner,
// all to a separate file. We're required to use a struct not a class
// and have only function calls and file management in main.
#include <iostream>
#include <fstream>
#include <limits>
#include <string>

//declare structure
struct studentType
{
   std::string studentFname;
   std::string studentLname;
   std::string winner;
   int testScore;
   char grade;
};

//declare array
studentType students[20];

//declare functions
bool read_data_into_array(std::string filename);
void assign_grade_to_each_student();
int find_highest_test_score();
void announce_winner(std::string filename);
void pauseExecution();
void printGlobalArray(); // for debug purposes only

int main()
{
   read_data_into_array("ex2data.txt");
   // delete the following lines after having tested the program:
   printGlobalArray();
   pauseExecution();

   assign_grade_to_each_student();
   // delete the following lines after having tested the program:
   printGlobalArray();
   pauseExecution();

   announce_winner("ex2out.txt");

   // system("pause"); <-- can't use it on Linux machines. Avoid it.
   pauseExecution();
   return 0;

}

bool read_data_into_array(std::string filename)
{
   std::ifstream infile(filename);

   if (infile.is_open())
   {//loop through text file to place in array
      for (int i = 0; i < 20; ++i)
      {
         infile >> students[i].studentFname
                >> students[i].studentLname
                >> students[i].testScore;
      }
      infile.close();
      return true;
   }

   // if we are here, infile was not opened, so we don't need to close() it.

   return false;
}

void  assign_grade_to_each_student()
{
   //loop though test scores, assigning letter grade to Char
   for (int i = 0; i < 20; ++i)
   {
      int grade = students[i].testScore; // 'grade': only a shorter name...

      if (90 <= grade)
      {
         students[i].grade = 'A';
      }
      else if (80 <= grade)
      {
         students[i].grade = 'B';
      }
      else if (60 <= grade)
      {
         students[i].grade = 'B';
      }
      else if (50 <= grade)
      {
         students[i].grade = 'D';
      }
      else
      {
         students[i].grade = 'F';
      }
   }
}

int find_highest_test_score()
{
   int highestScore = 0;
   for (int i = 0; i < 20; ++i)
   {
      if (students[i].testScore > highestScore)
      {
         highestScore = students[i].testScore;
      }
   }

   return highestScore;
}


void announce_winner(std::string filename)
{

   std::ofstream outfile(filename);
   outfile << "student name " << " Test Score " << " Grade \n";

   for (int i = 0; i < 20; ++i)
   {
      outfile << students[i].studentLname << ", " << students[i].studentFname
              << " --> score: " << students[i].testScore
              << "; grade: " << students[i].grade << '\n';
   }

   //call function for winner
   int score = find_highest_test_score();
   outfile << "\nThe highest Score is " << score << '\n';

   for (int i = 0; i < 20; ++i)
   {
      if (students[i].testScore == score)
      {
         outfile << "The Winning student(s) are: \n";
         outfile << students[i].studentLname << ", " << students[i].studentFname;
      }
   }

   outfile.close();
}

void printGlobalArray()
{
   for(int i=0; i<20; i++) {
      std::cout << "\nstudents[" << i << "] --> name: " << students[i].studentFname
                << "; surname: " << students[i].studentLname
                << " --> score: " << students[i].testScore
                << "; grade: " << students[i].grade;
   }
   std::cout << '\n';
}

void pauseExecution()
{
   // can't use system("pause") on Linux machines
   std::cout << "\nPress ENTER to continue...\n";
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


ex2data.txt:
1
2
3
4
5
6
7
8
9
10
Ann Anderson 10 
Bo Brown 20
Carol Clark 30
Donna Davis 40
Elizabeth Evans 50
Frances Foster 60
Gloria Garcia 70
Helen Harris 80
Jennifer Johnson 90
Karen King 100


ex2out.txt:
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
student name  Test Score  Grade 
Anderson, Ann --> score: 10; grade: F
Brown, Bo --> score: 20; grade: F
Clark, Carol --> score: 30; grade: F
Davis, Donna --> score: 40; grade: F
Evans, Elizabeth --> score: 50; grade: D
Foster, Frances --> score: 60; grade: B
Garcia, Gloria --> score: 70; grade: B
Harris, Helen --> score: 80; grade: B
Johnson, Jennifer --> score: 90; grade: A
King, Karen --> score: 100; grade: A
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F
,  --> score: 0; grade: F

The highest Score is 100
The Winning student(s) are: 
King, Karen

Lines 36-38 declare three functions. They don't call them.
Awesome! Thank you all!

Well.... it looked correct To my newb eyes :)
Thanks
Last edited on
@ Enoizat

This prompted me to reread the chapter on functions. Thank you!
Topic archived. No new replies allowed.