0

I'm currently doing an assignment and I would like to seek for help.

I'm suppose to do a creation system where

Option1:

I would create a binary file(outfile.dat) from infile.txt.
//below is the infile.txt

F Indonesian Michael Chen
F France Alain Pierre
S Tan Eng Soon
S Lee Ang Heng
F Malaysia Tan Eng Ming
F Indonesian Jospeh
F Vietnam Nguyen Min Thach
S Nancy Chan
S Heng Aik Koan
F Japan Yamada Suzuki
S Wong Ah Beng

Option 2:
I would process the text file (infileCA.txt) from the created binary file(outfile.dat)

Option 3:
I would update the student record

Option 9:
I would exit the program

I have a few problems/errors that I encountered.

  1. I'm suppose to loop back to the menu again after entering the option.

2.Problems I encounter in my output of infileCA.txt
//name not read fully, country local singapore & the noAssessment not display
1 Indonesian Michael11
2 France Alain11
3 Tan Eng Soon11
4 Lee Ang Heng11
5 Malaysia Tan11
6 Indonesian Jospeh
F Vietnam Nguyen11 <--- why is it not read as 7?
7 Nancy Chan11
8 Heng Aik Koan11
9 Japan Yamada11
10 Wong Ah Beng11

my output should be like:
1 Indonesian Michael Chen 0.0 0.0 0.0 0.0 0.0 0.0 0 Fail
2 France Alain Pierre 0.0 0.0 0.0 0.0 0.0 0.0 0 Fail
3 Singapore Tan Eng Soon 0.0 0.0 0.0 0.0 0.0 0.0 0 Fail

the third to eigth column is the noAssessment & the ninth colum is for the final mark interger 0.
Below is my code:

include <iostream>

include <fstream>

include <cstdlib>

include <cstring>

using namespace std;

const int MAX = 100;

struct LocalStudent
{
char name[MAX];
int noAssessment;
float mark[MAX];
int final;
char grade[MAX];
};

struct ForeignStudent
{
char country [MAX];
char name[MAX];
int noAssessment;
float mark[MAX];
int final;
char grade[MAX];
};

union AllStudent
{
LocalStudent ls;
ForeignStudent fs;
};

struct Student
{
char type;
AllStudent st;
};

void createBinary (ifstream&, char *, fstream&, char *);
void processText (ifstream&, char *, fstream&, char *);

int main()
{
fstream afile;
ifstream infile;

char in_textFileName [MAX];
char out_binFileName [MAX];
char in_binFileName [MAX];
char out_textFileName [MAX];

int n;


cout << "Welcome to UOWSIM Assessement Creation System" << endl << endl
    << "Here are some of the options" << endl
    << "1. Creation of assessment template (binary file)" << endl
    << "2. Process of assessment template (text file)" << endl
    << "3. Update of student record" << endl
    << "9. Quit" << endl << endl
    << "Your option: ";


cin >> n;

switch(n)
{   
    //1. Creation of assessment template
case 1: cout << "Creation of assessment template" << endl
        << "Enter file name from UOWSIM: ";

    cin >> in_textFileName;

    cout << "Enter file name from binary template: ";

    cin >> out_binFileName;

    createBinary (infile, in_textFileName, afile, out_binFileName);
     break;
                            //2. Process of assessment template
    case 2: cout << "Process of assessment template" << endl
    << "Enter binary file name for procssing: ";

    cin >> in_binFileName;

    cout<< "Enter file name for textfile template: ";

    cin >> out_textFileName;

                    processText(infile, in_binFileName, afile, out_textFileName);
    }

}

//Creation of assessment template (binary file)
void createBinary (ifstream& infile, char *in_textFileName, fstream& afile, char *out_binFileName)
{
infile.open (in_textFileName, ios::in);
afile.open (out_binFileName, ios::out | ios::binary);

if(!infile.good())
{
    cout << in_textFileName << " opened for reading failed" << endl
        << "End of task"<< endl;
    return;
}

cout << in_textFileName << " sucessfully opened for reading" << endl;

if(!afile.good())
{
    cout << out_binFileName << " opened for writing failed" << endl
        << "End of task"<< endl;
    return;
}

cout << out_binFileName << " successfully opened for writing" << endl;

char n;
Student stud;
int noAssessment;

while (infile >> n)
{
    switch (n)
    {
        case 'S': stud.type = n;
                    infile.getline(stud.st.ls.name, MAX);
                    break;

        case 'F': stud.type = n;
                    infile.getline(stud.st.fs.country, MAX,' ');
                    infile.getline(stud.st.fs.name, MAX);
                    break;
    }

    afile.write (reinterpret_cast <const char *> (&stud), sizeof (stud));
}      

cout << "How many assessment components? ";

cin >> noAssessment;                    

infile.close();
cout << in_textFileName << " successfully closed" <<endl;
afile.close();
cout << out_binFileName << " successfully created" << endl;

return;

}

//Process of assessment template (text file)
void processText(ifstream& infile, char *in_binFileName, fstream& afile, char *out_textFileName)
{
infile.open (in_binFileName, ios::binary | ios::in);
afile.open (out_textFileName, ios::out);

if (!infile.good())
{
    cout << in_binFileName << " opened for reading failed " << endl;
    return;
}

cout << in_binFileName << " sucessfully opened for reading" << endl;

    if(!afile.good())
{
    cout << out_textFileName << " opened for writing failed" << endl;
    return;
}

cout << out_textFileName << " successfully opened for writing" << endl;

Student stud;
int i = 0;
int n;

while (true)
{
    infile.read (reinterpret_cast <char *>(&stud), sizeof (stud));

    if(infile.eof())break;    
    afile << ++i
        << stud.st.fs.country
        << (stud.st.ls.name || stud.st.fs.name)
        << (stud.st.ls.noAssessment || stud.st.fs.noAssessment) << endl;

}

infile.close ();
cout << in_binFileName << " successfully closed" << endl;

afile.close ();
cout << out_textFileName << " successfully created" << endl;

}

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by firstPerson
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.