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
|
/*This program creates a class that contains the name
of a student, the course, and the course grade.
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <iomanip>
#include <fstream>
#include <string>
const unsigned short int MAXELS = 10;
class course_grade{
private:
string course;
string grade;
public:
course_grade();
course_grade(string courseIn, string gradeIn);
void setCourseGrade(string, string);
void setCourse(string);
void setGrade(string);
void getCourseGrade();
void getCourse();
void getGrade();
};
void OpenFile(ifstream&, string);
void readGrades(ifstream&, course_grade *);
void CloseFile(ifstream&, string);
int main()
{
int n = 0;
ifstream inFile;
string fileName = "students.txt";
course_grade myGrades[MAXELS], *myGradesPtr = myGrades;
//open the file
OpenFile(inFile, fileName);
//read the file into an array
readGrades(inFile, myGradesPtr);
//close the file
CloseFile(inFile, fileName);
return 0;
}
course_grade::course_grade()
//0 parameter constructor
{
setCourseGrade("N/A", "0");
}
course_grade::course_grade(string courseIn, string gradeIn)
{//double parameter contructor
course = courseIn;
grade = gradeIn;
}
void course_grade::setCourseGrade(string inCourse, string inGrade)
{//sets the course and grade for the student
course = inCourse;
grade = inGrade;
}
void course_grade::setCourse(string inCourse)
{//sets the course for the student
course = inCourse;
}
void course_grade::setGrade(string inGrade)
{//sets the grade for the student
grade = inGrade;
}
void course_grade::getCourseGrade()
{//dsiplays the coure and grade
cout << "The grade for the course " << course << "is: " << grade << endl;
}
void course_grade::getCourse()
{//gets the course for the student
cout << "The course is " << course << endl;
}
void course_grade::getGrade()
{//gets the grade for the course
cout << "The grade is " << grade << endl;
}
void OpenFile(ifstream& Infile, string filename)
{ //function that opens file, catches any errors
try //try to open the file
{
Infile.open(filename); // open the file
if (Infile.fail()) //if it fails, thow an exception
{
throw filename;
}
else
{
cout << "The input file has been opened" << endl;
}
}
catch (string filename) //catch the exception
{
cout << "The file '" << filename << "' was not opened.\nPlease make sure the file exists in the directory!\n";
cout << "The program will now terminate. Have a nice day!\n";
system("pause");
exit(1); //exit the program
}
}
void ReadGrades(ifstream& gradeInFile, course_grade *gradeArray)
{ //reads grades from file, assigns them to the array
string name, courseName, score;
cout << "The readGrades funciton is working....\n\n";
gradeInFile >> name >> courseName >> score;
gradeArray->setCourseGrade(courseName, score);
cout << "Here is the entire list for: " << name << endl << endl;
cout << "COURSE NAME SCORE\n";
cout << "------------------------------------------\n";
cout << setw(10) << std::left << name << setw(5) << std::right << score << endl;
gradeArray++;
//read and assign information from file until EOF
while (gradeInFile >> courseName >> score)
{
gradeArray->setCourseGrade(courseName, score);
cout << setw(10) << std::left << name << setw(5) << std::right << score << endl;
gradeArray++;
}
cout << endl;
system("pause");
}
void CloseFile(ifstream& InFile, string FileName)
{//closes the file for future use
InFile.close();
cout << "\nThe file '" << FileName << "' has been closed.\n\n";
system("pause");
}
|