0

I am trying to read a text file into a cstring array with struct/union implementations. First off, this is what my text file looks like:

F South Korea
Male Psy Park Jae Sang
31 - 12 - 1977
3 CSCI114 55 CSCI103 44 GangNam 100

S Female Super Junior
5 - 8 - 1978 
2 CSCI114 60 CSCI103 80

F People Republic Of China
Unknown James Bond
11 - 12 - 1976
4 CSCI114 54 CSCI124 66 CSCI007 99 CSCI123 28

Now, ignoring the obvious Asian popstar references my lecturer decided to fill this example with - I'd like to be able to read the first number before the CSCI, which is the number or courses and their respective grades and automate(loop) it for all the 3 (or possibly more) students in the text file.

I have not written any code for what I'm trying to describe here but here is what I have so far.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAX = 80;

struct Birthday
{
    char day[MAX];
    char month[MAX];
    char year[MAX];
};

struct Local
{
    char name[MAX];
    char gender[MAX];
    Birthday bd;
    char subjects [MAX];
};

struct Foreigner
{
    char name[MAX];
    char nationality[MAX];
    char gender[MAX];
    Birthday bd;
    char subjects [MAX];
};

union Student
{
    Local     localStudent;
    Foreigner foreignStudent;
};

enum CountryType {S, F};

struct UowStudents
{
    CountryType ct;
    Student st;
};

int fileToArray (fstream& afile, char fileName [], UowStudents& x);

int main ()
{
    srand(time_t(NULL));

    fstream afile;
    UowStudents x [MAX];
    char fileName[MAX];
    cout << "Enter filename: ";
    cin >> fileName;
    int size = fileToArray (afile, fileName, *x);


}

int fileToArray (fstream& afile, char fileName [], UowStudents& x)
{
    afile.open(fileName, ios::in);

    if (!afile)
    {
        cout << fileName << "could not be opened for read" << endl;
        exit (-1);
    }
    //file to array.
    char locale;
    char dateJunk;
    afile >> locale;
    afile.getline(x.st.foreignStudent.nationality, MAX);
    afile >> x.st.foreignStudent.gender;
    afile.getline(x.st.foreignStudent.name, MAX);
    afile >> x.st.foreignStudent.bd.day;
    afile >> dateJunk;
    afile >> x.st.foreignStudent.bd.month;
    afile >> dateJunk;
    afile >> x.st.foreignStudent.bd.year;


    //Tests my cstring arrays to see everything is copied in correctly.
    cout << locale << " " << x.st.foreignStudent.nationality;
    cout << endl;
    cout << x.st.foreignStudent.gender;
    cout << x.st.foreignStudent.name;
    cout << endl;
    cout << x.st.foreignStudent.bd.day << " - ";
    cout << x.st.foreignStudent.bd.month << " - ";
    cout << x.st.foreignStudent.bd.year;
    return 0;
}

This is what my final text file with all the array information processed, would look like:

https://i.stack.imgur.com/4g4CM.jpg

Any help is appreciated, thanks.

timrau
  • 22,578
  • 4
  • 51
  • 64
TL Han
  • 27
  • 1
  • 10
  • are they forcing you to use unions this way? it seems like an arguable design to me – Andy Prowl Jan 05 '13 at 19:17
  • Yes, the header is pretty much pre-formatted this way. – TL Han Jan 05 '13 at 19:21
  • are you allowed to use STL classes such as `string`? your question is tagged as "C++" so I assume the answer is yes, but better asking before I think of a solution you won't be allowed to use – Andy Prowl Jan 05 '13 at 19:24
  • I guess so, I've had trouble between string and char conversions in the past however. Not sure if it applies here but I am allowed, yes. – TL Han Jan 05 '13 at 19:27
  • using strings would help simplify the code a lot, but the instructor might specifically want you using cstrings, make sure you are actually able to use the string library. – Syntactic Fructose Jan 05 '13 at 19:28
  • Hmm it would be easier but he warned us only to use cstrings for this program. – TL Han Jan 05 '13 at 19:33

1 Answers1

0

You can read in each course list this way:

int numOfCourses;
afile >> numOfCourses;
for (int i = 0; i < numOfCourses; i++)
{
    string course;
    afile >> course;

    int grades;
    afile >> grades;

    // Fill in structure from `course` and `grades`...
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Now, what it does is it takes in the course name and score and then in another loop, it overwrites it. Hmm... I think what I need is a 2d cstring for the courses. I'll try writing a loop that way. – TL Han Jan 05 '13 at 19:54
  • `int numOfCourses; afile >> numOfCourses; char course[numOfCourses][MAX]; char grades[numOfCourses][MAX]; for (int i = 0; i < numOfCourses; i++) { afile >> course[i]; afile >> grades[i]; }` This seems to work so far. On with the program, thanks! – TL Han Jan 05 '13 at 20:01
  • sure, i just wrote the skeleton, the comments should have been replaced with the code that handles the creation of a new entry containing those data. had little time so i hoped it would be enough to put a comment :) – Andy Prowl Jan 05 '13 at 20:06