I am writing code to have input student and grade objects, sort them, calculate an average gpa, and output them to a file. My current problem is that the output of my student objects are given after I sort them and print them, but the code does not seem to leave the loop once I do that. I have a print function in which I am passing an ofstream. From debugging, I know that the out ofstream object reads everything, but then it seems to freeze, and not output anything else into it. Can somebody tell me if I passed by reference incorrectly, or if I need to do something else? The error where the compiler didn't keep going through is marked in the code.
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <string>
#include "Grade.h"
#include "Student.h"
using namespace std;
const double A_NUM = 4.0, B_NUM = 3.0, C_NUM = 2.0, D_NUM = 1.0, E_NUM = 0.0, B_PLUSSNUM = 3.4, C_PLUSSNUM = 2.4, D_PLUSSNUM = 1.4;
const double A_MINUSNUM = 3.7, B_MINUSNUM = 2.7, C_MINUSNUM = 1.7, D_MINUSNUM = 0.7;
const int FIRST_INDEX= 0;
const int START_NUM = 0;
const int LESS_ONE = 1, ABOVE_ONE = 1;
const int START_GRADE = 0;
void calcgrades(vector<Grade>& grades)//function that calculates gpa based on letter grades
{
const string A = "A", A_MINUS = "A-", B_PLUSS = "B+", B = "B", B_MINUS = "B-", C_PLUSS = "C+", C = "C", C_MINUS = "C-", D_PLUSS = "D+", D = "D", D_MINUS = "D-", E = "E";
int counter = START_NUM;//used to keep track of current student and current total grade
double current_grade = START_NUM;
for(int i = 0; i < grades.size();i++)
{
//while loop to get the student's current total grade if the next student id is different than the first one.
while(i < grades.size()-LESS_ONE && grades[i].getid() == grades[i+ABOVE_ONE].getid())
{
if (grades[i].getgrade() == A)
{
current_grade == A_NUM + current_grade;
}
if (grades[i].getgrade() == B)
{
current_grade == B_NUM + current_grade;
}
if (grades[i].getgrade() == C)
{
current_grade == C_NUM + current_grade;
}
if (grades[i].getgrade() == D)
{
current_grade == D_NUM + current_grade;
}
if (grades[i].getgrade() == E)
{
current_grade == E_NUM + current_grade;
}
if (grades[i].getgrade() == B_PLUSS)
{
current_grade == B_PLUSSNUM + current_grade;
}
if (grades[i].getgrade() == C_PLUSS)
{
current_grade == C_PLUSSNUM + current_grade;
}
if (grades[i].getgrade() == D_PLUSS)
{
current_grade == D_PLUSSNUM + current_grade;
}
if (grades[i].getgrade() == A_MINUS)
{
current_grade == A_MINUSNUM + current_grade;
}
if (grades[i].getgrade() == B_MINUS)
{
current_grade = B_MINUSNUM + current_grade;
}
if (grades[i].getgrade() == C_MINUS)
{
current_grade = C_MINUSNUM + current_grade;
}
if (grades[i].getgrade() == D_MINUS)
{
current_grade = D_MINUSNUM + current_grade;
}
}
if (grades[i+ABOVE_ONE].getid() != grades[i].getid() || i == grades.size()-LESS_ONE)
{
//computes the average if the currentid is not equal to the nextid
double avggpa = current_grade/counter;
grades[i].newgpa(avggpa);
counter = START_NUM;//resets counter for a new student to get his gpa
}
counter++;
}
}
int main(int argc, char* argv[])
{
string student_file, grades_file, idnum, name, address, pnum, course, gletter;
//creates student and grade objects from respective classes
vector<Student> students;
vector<Grade> grades;
student_file = argv[1];
//reads in information from file to import into student objects
ifstream sfile;
sfile.open(student_file);
while(getline(sfile, idnum))//gets the student information from student file
{
getline(sfile, name);
getline(sfile, address);
getline(sfile, pnum);
//creates student objects to import info. into, and inserts into students vector
Student s(idnum, name, address, pnum);
students.push_back(s);
}
sfile.close();
//opens information from the grade file
grades_file = argv[2];
//reads from file to import into grades objects
ifstream gfile;
gfile.open(grades_file);
//gets the grade information from the grades file
while(getline(gfile, course))
{
getline(gfile, idnum);
getline(gfile, gletter);
//creates grade objects to import info, and inserts into grades vector
Grade g(course, idnum, gletter, START_GRADE);
grades.push_back(g);
}
gfile.close();
//reads the querry file
string query_file;
query_file = argv[3];
ifstream qfile;
qfile.open(query_file);
//reads from query file
//creates vector to store each student number
vector<string> query_nums;
string incheck;
while(getline(qfile, incheck))
{
query_nums.push_back(incheck);
}
qfile.close();
//sorts the information for the students
sort(students.begin(), students.end());
//sorts the information for the grades
sort(grades.begin(), grades.end());
ofstream outtxt;
string out_file = argv[4];
outtxt.open(out_file);
//outputs the student information, sorted now.
for(int i = 0; i < students.size();i++)
{
students[i].print(outtxt);
outtxt << "got through it";
}
//compiler did not get past here!
outtxt << "We're here!";
//outputs the grades with student id's, now sorted
for (int i = 0; i < grades.size(); i++)
{
grades[i].print(outtxt);
outtxt << "nn" << "so gay!";
}
//calculates the average gpa for every student
calcgrades(grades);
for(int i = 0; i < query_nums.size(); i++)//goes through each query number to check first
{
for (int j = 0; j < students.size(); j++)//after, goes through each student object
{
if (query_nums[i] == students[j].getid())
{
//finds the gpa in the grades class that matches the student id
for (int k = 0; k < grades.size(); k++)
{
if (grades[k].getid() == query_nums[i])//
{
//outputs the resulting id, avg gpa, and name of matching students
outtxt << grades[i].getid() << "tthere is nothinghere" << grades[i].getgpa() << "t" << students[i].getname();
}
}
}
}
}
outtxt.close();
return 0;
}
student class with print function
class Student
{
private:
string idnum;
string name;
string address;
string pnum;
public:
//defines constructor
Student(string idnum,string name,string address,string pnum);
//prints each student info
void print(ofstream& out);
string getname();
string getid();
bool operator < (Student s) const {
return idnum < s.idnum;
}
};
student cpp file
#include "Student.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//Student constructor, used to make student objects
Student::Student(string id,string nm,string add,string pnumber)
{
idnum = id;
name = nm;
address = add;
pnum = pnumber;
}
void Student::print(ofstream& out)
{
out << name << endl << idnum << endl << pnum << endl << address << endl;
}
string Student::getname()
{
//returns the student name
return name;
}
string Student::getid()
{
return idnum;
}
Aucun commentaire:
Enregistrer un commentaire