I am doing a program to get a sample infile.txt & ouput to outfile.txt
I encountered alot of probs which I can't solve now. I am having issues reading from infile.txt & display the correct result in outfile.txt Pls enlighten me further. The program have to submit this Tue.
My code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int MAX = 50;
enum SEX {Male, Female};
struct MaleStudent
{
char name;
int mark1;
int mark2;
int mark3;
};
struct FemaleStudent
{
char name;
int mark1;
int mark2;
int mark3;
int mark4;
};
union Student
{
MaleStudent m;
FemaleStudent f;
};
struct UOWSIM
{
SEX gen;
Student st;
int result;
char grade [MAX];
};
int FileToArray (fstream&, char [], UOWSIM []);
void arrayToOutfile (fstream&, char [], UOWSIM [], int);
int main ()
{
fstream afile;
UOWSIM p [MAX];
UOWSIM stu;
int size = FileToArray (afile, "infile.txt", p);
arrayToOutfile (afile, "outfile.txt", p, size);
}
int FileToArray (fstream& afile, char fileName [], UOWSIM p [])
{
afile.open (fileName, ios::in);
if (!afile.good ())
{
cout << "File opening for writing failed" << endl;
return 1;
}
afile.close ();
}
int findGrade (int marks)
{
if (marks < 50)
{
cout << "Fail";
}
else if (marks <= 64)
{
cout << "Pass";
}
else if (marks <= 74)
{
cout << "Credit";
}
else if (marks <= 84)
{
cout << "Dist";
}
else
{
cout << "HDist";
}
}
void arrayToOutfile (fstream& afile, char fileName [], UOWSIM p [], int size)
{
afile.open (fileName, ios::out);
for (int i = 0; i < size; i++)
{
int result;
int mark1, mark2, mark3, mark4;
result = (mark1 + mark2 + mark3) / 3;
afile << "Student " << i + 1 << endl;
if (p[i].gen == Male)
{
afile << "Name: " << p [i].st.m.name << endl;
afile << "Sex: Male" << endl;
afile << "Assignment 1 " << p [i].st.m.mark1 << endl;
afile << "Assignment 2 " << p [i].st.m.mark2 << endl;
afile << "Assignment 3 " << p [i].st.m.mark3 << endl;
afile << "Result " << p [i].result.m.MaleStudent << endl;// Have a problem with the result in outfile.txt
afile << "Grade " << endl; // Have a problem with the grade display in outfile.txt
}
else
{
afile << "Name: " << p [i].st.f.name << endl;
afile << "Sex: Female" << endl;
afile << "Assignment 1 " << p [i].st.f.mark1 << endl;
afile << "Assignment 2 " << p [i].st.f.mark2 << endl;
afile << "Assignment 3 " << p [i].st.f.mark3 << endl;
afile << "Assignment 4 " << p [i].st.f.mark4 << endl;
afile << "Result " << p [i].result.f.FemaleStudent << endl; // Have a problem with the result in outfile.txt
afile << "Grade " << endl; // Have a problem with the grade display in outfile.txt
}
afile << "------------------------------------------------" << endl;
}
afile.close ();
}
sample infile.txt info
[output]
F Mary 70 60 89 77
M Andy 70 89 65
M David 89 88 56
M Michael 70 60 89
F Sally 69 75 66 80
[/output]
Outfile.txt should have the following info:
[output]
Student 1
Name: AAAAAAA
Sex: Male
Assignment 1 67
Assignment 2 88
Assignment 3 78
Result: 78 // Add the 3 assignments / 3
Grade: Dist
------------------------------------------------
Student 2
Name: BBBB
Sex: Female
Assignment 1 89
Assignment 2 70
Assignment 3 60
Assignment 4 77
Result: 74 // Add the 4 assignments / 4
Grade: Credit
------------------------------------------------
[/output]