I have, with the vast assistance of Bazzy on cplusplus as well as a few other programmers, made the code to input a file into a vector. Even after looking over the tutorials on algorithms, I still can't figure out how to sort the data.
It's due in 4 hours so I would greatly appreciate any help. Thanks in advance.
Here are the sorting instructions:
The program is to take a text file, students.txt, containing information about the currently-enrolled students in a school. Each
line represents a record for a different student and contains the student's identification number (eight digits), credit hours
completed, and grade point average. A sample line follows:
10082352 66 3.04
Program is supposed to have the identification number and grade point average for each student in the file copied to one of seven (four core or three others if there is an error)
different text files based on the number of credit hours completed:
Less than 32 freshmen.txt
32 to 63 sophomores.txt
64 to 95 juniors.txt
96 or more seniors.txt
Next is to write the identification number and grade point average of each student on a single line (one line per student) in the
appropriate file. For example, since the student mentioned above has completed 66 credit hours, the following would be
added to the file juniors.txt:
10082352 3.04
Also, no student should have a negative number of credit hours, and the maximum number of credit
hours that a student can accumulate is 250. If the program encounters a student record with an erroneous number of
credit hours, write the entire record for that student (identification number, credit hours, and grade point average) to the
file hoursError.txt rather than any of the four aforementioned files. The lowest grade point average a student may have is 0.0, and the highest grade point average a
student may have is 4.0. If the program encounters a student record with an erroneous grade point average, write the
entire record for that student to the file averageError.txt rather than any of the four aforementioned files.As was previously stated, the student's identification number should be eight digits. If the program
encounters a student record with an identification number of fewer or greater than eight digits, write the entire record for
that student to the file identError.txt rather than any of the four aforementioned files.Assume that an identification number error has highest precedence, then the
number of credit hours completed, then the grade point average. Don't write a student record to multiple error files: only
the one that has the highest precedence should receive the record.
Here is the code thus far:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
std::ifstream students("students.txt");
std::string input;
if (students.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}
int count=0;
char carray[1000];
while (!students.eof())
{
students.getline(carray,10000);
count++;
}
ofstream freshmen("freshmen.txt");
ofstream sophomores("sophmores.txt");
ofstream juniors("juniors.txt");
ofstream seniors("seniors.txt");
struct student
{
int id;
int hours;
double grade;
};
std::vector<student> my_student;
while( std::getline(students, input) )
{
std::string input = "12345678 50 3.2";
std::istringstream buffer(input);
int id_number;
int credit_hours;
double grade_points;
if( buffer >> id_number >> credit_hours >> grade_points )
{
student stud;
stud.id = id_number;
stud.hours = credit_hours;
stud.grade = grade_points;
my_student.push_back(stud);
}
else
{
cout<<"Error in processing file.\n";
exit(1);
}
}
/** code to format output to show the gpa in correct format.
std::cout.setf(ios::fixed, ios::floatfield);
std::cout.setf(ios::showpoint);
std::cout.precision(2);
*/
}