I am trying to do an insertion sort that reads from a file it has it own class but i can not get the program to do anything it says no errors but crashes when i try to run
#include <string>
using namespace std;
class Student {
public:
string lastname;
string firstname;
string credittakings;
string creditearned;
string gradepoint;
Student * next;
private:
Student(string lname,string fname,string credittakings,string creditearned,string gradepoint);
void set_next(Student *);
Student* get_next();
//bool operator > (Student& op1);
void print_output();
};
void Student::print_output()
{
cout <<" ******************************** " << endl;
cout << "LastNAME " << " " << lastname << endl;
cout << "FirstName" << " " << firstname << endl;
cout << "CreditTakings" << " " << credittakings << endl;
cout << "CreditEarned " << " " << creditearned << endl;
cout << "GradePoint " << " " << gradepoint << endl;
}
#include <iostream>
#include "string"
#include "student.h"
#include <fstream>
using namespace std;
Student* list[20];
int main()
{
string lastname;
string firstname;
string credittakings;
string creditearned;
string gradepoint;
//fstream inFile("students.txt");
ifstream inFile("students.txt");
//3 ofstream outFile; //output file stream variable
//outFile.open("output10p1.txt"); //open outfile
//check if file opened
if (!inFile)
{
cout<< "Can not open file student.txt"<<endl;
return 0;
}
//declare variables
char str[128];
int i=0, c;
while (!inFile.eof())
{
inFile.getline(str, 128);
string line = str;
int pos1 = line.find(",");
string lastname = line.substr(0,pos1);
int pos2 = line.find(",",pos1+1);
string firstname = line.substr(pos1+1,pos2-pos1-1);
int pos3 = line.find(",",pos2+1);
string credittakings = line.substr(pos2+1,pos3-pos2-1);
string creditearned = line.substr(pos3+1,line.length()-pos3-1);
//int pos4=line.find(",",pos3+1);
//string gradepoint = line.substr(pos4+1,line.length()-pos4-1);
//string gradepoint = atof(grade.c_str());
//list[i] = new Student;
list[i] ->lastname = lastname;
list[i] ->firstname = firstname;
list[i] ->credittakings = credittakings;
list[i] ->creditearned = creditearned;
list[i] ->gradepoint= gradepoint;
i++;
}
// SAVE THE CURRENT VALUE OF THE LOOP COUNTER. IT'S THE STUDENTS COUNT
int count = i;
// loop will keep function until user enters "0"
while (true)
{
//(0 to quit)
cout<<"Which column would you like to sort by (0 to quit) ==> "<<endl;
cout<<"1) Last Name"<<endl<<"2) first Name"<<endl;
cin>>c;
// if "0" is entered loop will terminate
if (c == 0)
break;
// Loop Runs Til Count == Number Of Students Read
for (i=0; i < count; i++)
{
for (int j=i+1; j < count; j++)
{
switch (c)
{
case 1: if (list[i]->lastname > list[j]->lastname){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
case 2: if (list[i]->firstname > list[j]->firstname){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
}
}
}
cout <<"+-----------+-------------+---------------------+------------------------------------------------+"<<endl;
cout <<"|Last Name | First Name | CreditTakings | CreditEarned | GradePoint|"<<endl;
cout <<"+-----------+-------------+---------------------+------------------------------------------------+"<<endl;
// statement to output sorted table
for (i=0; i< count; i++)
{
cout <<list[i]->lastname<<"\t | "<<list[i]->firstname<<"\t | "<<list[i]->credittakings<<"\t| ";
cout <<list[i]->creditearned<<"\t |"<< list[i]->gradepoint<<"\t |"<< endl;
}
cout <<"+-----------+-------------+---------------------+--------------------+"<<endl;
}
return 0;
}
the text file is
Johnson Christy 12 85 2.5
Lewis Breana 15 45 3.2
Adams Brittany 13 25 4.0
Mcallister Terrance 16 90 3.7
Johnson Darius 14 25 2.0
<< moderator edit: added code tags: [co[u][/u]de][/co[u][/u]de]
>>