Update (for those who read this much of my dumb topic)
I managed to get something that works after working with a buddy half the nite.
Code:
#include <fstream.h>
#include <string.h>
#include <ctype.h>
#include <iomanip.h>
struct studentrec
{
char name[21];
int exam;
char gender;
char id[8];
};
void main()
{
ifstream in("rh1165.dat");
ofstream out("out6.txt");
studentrec student[36];
char valid[36][20];
//function prototypes
void header(ofstream &out);
void page1(ifstream &in, ofstream &out, studentrec student[]);
void page2(ofstream &out, studentrec student[]);
void page3(ofstream &out, studentrec student[], char valid[36][20]);
void page4(ofstream &out, studentrec student[], char valid[36][20]);
header(out);
page1(in, out, student);
header(out);
page2(out, student);
header(out);
page3(out, student, valid);
header(out);
page4(out, student, valid);
}
void header(ofstream &out)
{
out << " Name Exam Gender Student ID";
}
void page1(ifstream &in, ofstream &out, studentrec student[])
{
int count = 0;
//read in information
while(!in.eof())
{
in.get(student[count].name, 21);
in >> student[count].exam;
in >> student[count].gender;
in.get(student[count].id, 8);
in >> ws;
count++;
}
//print out information
out << endl << endl;
for(int i = 0; i < 36; i++)
{
out << setw(20) << student[i].name << " "
<< setw(3) << student[i].exam << " "
<< setw(1) << student[i].gender << " "
<< setw(8) << student[i].id << endl;
}
out << '\f';
}
void page2(ofstream &out, studentrec student[])
{
studentrec temp;
//perform bubble sort
for(int i = 0; i < 36; i++)
{
for(int j = 0; j < 35; j++)
{
if((strncmp(student[j].name, student[j + 1].name, 20)) > 0)
{
temp = student[j];
student[j] = student[j + 1];
student[j + 1] = temp;
}
}
}
//print out information
out << endl << endl;
for(i = 0; i < 36; i++)
{
out << setw(20) << student[i].name << " "
<< setw(3) << student[i].exam << " "
<< setw(1) << student[i].gender << " "
<< setw(8) << student[i].id << endl;
}
out << '\f';
}
void page3(ofstream &out, studentrec student[], char valid[36][20])
{
char valid_code[][20] = {
"Invalid Name",
"Invalid Score",
"Invalid Gender",
"Invalid Student ID"
};
for(int i = 0; i < 36; i++)
strncpy(valid[i], "Valid", 20);
for(i = 0; i < 36; i++)
{
//check name validity
for(int j = 0; j < 20; j++)
{
if(!isalpha(student[i].name[j]) && !isspace(student[i].name[j]))
{
strncpy(valid[i], valid_code[0], 20);
break;
}
}
}
//check score validity
for(i = 0; i < 36; i++)
{
if(student[i].exam < 0 || student[i].exam > 100)
{
strncpy(valid[i], valid_code[1], 20);
}
}
//check gender validity
for(i = 0; i < 36; i++)
{
if(student[i].gender != 'F' && student[i].gender != 'M')
{
strncpy(valid[i], valid_code[2], 20);
}
}
//check student ID validity
for(i = 0; i < 36; i++)
{
if(!isupper(student[i].id[0]) || !isupper(student[i].id[1]) ||
!isdigit(student[i].id[2]) || !isdigit(student[i].id[3]) ||
!isdigit(student[i].id[4]) || !isdigit(student[i].id[5]) ||
!isdigit(student[i].id[6]))
{
strncpy(valid[i], valid_code[3], 20);
}
}
//print out information
out << " Valid" << endl << endl;
for(i = 0; i < 36; i++)
{
out << setw(20) << student[i].name << " "
<< setw(3) << student[i].exam << " "
<< setw(1) << student[i].gender << " "
<< setw(8) << student[i].id << " "
<< valid[i] << endl;
}
out << '\f';
}
void page4(ofstream &out, studentrec student[], char valid[36][20])
{
out << " Valid" << endl << endl;
int count = 0;
studentrec valid_sorted[36];
//refill data with only valid records
for(int i = 0; i < 36; i++)
{
if((strncmp(valid[i], "Valid", 20)) == 0)
{
valid_sorted[count] = student[i];
count++;
}
}
//shell sort
//print out information
for(i = 0; i < count; i++)
{
out << setw(20) << valid_sorted[i].name << " "
<< setw(3) << valid_sorted[i].exam << " "
<< setw(1) << valid_sorted[i].gender << " "
<< setw(8) << valid_sorted[i].id << " "
<< endl;
}
out << '\f';
}
So basically this program (so far) displays the array, sorts it, looks for invalid data, removes it...all that fun stuff. And I have guidelines, so I had to use a bubble sort at least once, things like that. Void main....yes its evil (although I don't know why yet). I may still fix that.
Todd