Outputting multiple files in a loop

Hi, I am basically finished with this class assignment the only problem I am facing is with being able to output to different files.

For example, the program will output to a file after the info is collected and calculated. The problem is that the program will ask if the user has more grades to enter. The user will enter the students and and grades as normal but for some reason it won't create a different file for it.

Any help is much appreciated. Yes I am new to C++ and programming in general so I apologize for any deficiencies and ambiguities on my part.

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

const int MAX_HW_SCORES = 10;

void ReadStudentInfo(string& fullname, double hwScores[MAX_HW_SCORES], int& i);
void CalcHomeworkScores(double& maxNum, double& minNum, double& avgNum, char& letterGrade, double grades[MAX_HW_SCORES], int gradesEntered);
void GradeReport(ostream& out, const string fullname, double hwScores[MAX_HW_SCORES], int gradesEntered, const double maxNum, const double minNum, const double avgNum, const char letterGrade);
void WriteLine(int size, char ch, ostream& out);
string FirstName();
string LastName();
double HomeworkScores(double hwScores[MAX_HW_SCORES],  int i);
bool ValidGrade(double score);
bool MoreScores();
bool YesOrNo(char answer);
bool MoreStudents();
double MaxScore(double hwScores[MAX_HW_SCORES], int amountOfGrades);
double MinScore(double hwScores[MAX_HW_SCORES], int amountOfGrades);
double AverageScore(double hwScores[MAX_HW_SCORES], int amountOfGrades);
char LetterGrade(double average);

int main()
{
	ofstream outfile;
	string studentname;
	char studentLetterGrade;
	double studentGrades[MAX_HW_SCORES], maxHwScore, minHwScore, averageHwScore;
	int numberOfGradesEntered;
	do{
		ReadStudentInfo(studentname, studentGrades, numberOfGradesEntered);
		CalcHomeworkScores(maxHwScore, minHwScore, averageHwScore, studentLetterGrade, studentGrades, numberOfGradesEntered);
		GradeReport(cout, studentname, studentGrades, numberOfGradesEntered, maxHwScore, minHwScore, averageHwScore, studentLetterGrade);
		outfile.open(studentname + "'s" +" Homework Grades.txt");
		GradeReport(outfile, studentname, studentGrades, numberOfGradesEntered, maxHwScore, minHwScore, averageHwScore, studentLetterGrade);
	} while (MoreStudents());
	return 0;
}
It would be helpful to see the rest of the code.

But just based on the code above, I would probably check to see if you are opening and closing your .txt files properly. You should open, write, close. Open another one, write, close. etc.

And I'm not completely sure if your filename is legal, with the apostrophe and spacing, but maybe it is.
you didn't test if you've successfully opened the file, you should test it first using is_open() member function, and try removing the space in the filename and see if it works
I would probably check to see if you are opening and closing your .txt files properly. You should open, write, close. Open another one, write, close. etc.


Ugh. Yea that solved the problem. I was able to run the program multiple times without closing and a new file was created for each student. Thank you for your help.

And I'm not completely sure if your filename is legal, with the apostrophe and spacing, but maybe it is.


It's a legal filename. I'm using VS 2012 if that means anything to you.
Topic archived. No new replies allowed.