Here is my program so far: #include <iostream> #include <fstream> #include <ioma
ID: 3615158 • Letter: H
Question
Here is my program so far:#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
enum sortType
{
FIRST=0, LAST=1, GPA=2
};
const int SIZE = 1000;
struct studentType
{
string fname, lname, major;
int id;
float gpa;
};
//void sortStudents(studentType student[], const int SIZE, sortTypefield);
int main()
{
ifstream fin("program7.txt");
ofstream fout("output7.txt");
int count;
studentType student[SIZE];
int i=0;
while(i<SIZE && fin >>student[i].fname >> student[i].lname >>student[i].id
>> student[i].major>> student[i].gpa)
{
i++;
}
count = i;
for(i=0;i<count;i++)
{
fout << setw(15)<< student[i].fname
<< setw(15) <<student[i].lname
<< setw(15) <<student[i].id
<< setw(10) <<student[i].major
<< setw(5) <<student[i].gpa << endl;
}
return 0;
}
I am trying to write a sort function that will sort the data byfirst name or last name (if I can figure out how to sort by one theother should follow pretty easily. I am trying to use anfunction similar to this:
void sortStudents(studentType student[], const int SIZE, sortTypefield)
{
int i, j, min;
for(i=0;i<SIZE;i++)
{
min = i;
for(j=i+1;j<SIZE;j++)
{
switch(field)
{
caseFIRST:
if(student[j].fname <student[min].fname)
{
min=j;
}
break;
caseLAST:
if(student[j].lname <student[min].lname)
{
min=j;
}
break;
caseGPA:
if(student[j].gpa <student[min].gpa)
{
min=j;
}
break;
}
}
swap(student[i],student[j]);
}
}
but it seems like I can't get the loop to run to sort mydata. Any help you would appreciated and if you need me toclarify anything I will gladly do so. I know I don't use anycomments or anything in my code but I like to get it done thenexplain things after. Thanks in advance for your help!
Explanation / Answer
Also, here is a link to input file I am using: http://kc-sce-bit.kc.umkc.edu/cs101/assignments/f2009/program7.txt
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.