0

I'm currently writing a program that needs to read a input file and populate a set of Student objects. Then it will display the objects, and perform some basic loop and function-based processing. I'm still new to C++, especially with file handling and anything object-oriented. Any step by step help I get will be greatly appreciated.

Input file data:

John Daniel Fuller E23123456034Malic Stephen Browscent W03023142039Richard E. Pollidence E02782635021Frank William Lomo E09912376022Colin Frankson R23234556023James Theodore Jackson D02323245059Dave Gerald Mungbean F12333221042Jim Waymo Hilbert W02785634055Barb C Bowie W02833546030Jim D Carlo S22876543033

Processing the input:

  1. Open the input file. Check for successful open. If the open failed, display an error message and return with value 1.

  2. Use a properly-structured loop to read the input file until EOF.

  3. Use the .read(reinterpret_cast) function to read each record into a character array large enough to hold the entire record.

  4. For each input file record, dynamically allocate and populate a Student variable. Note that to populate some of the Student fields, you'll need to perform some type of conversion. Use a pointer array to manage all the created student variables.

  5. Assume that there will not be more than 99 input records, so the size of the student variable pointer array will be 100. Use a "global" variable to define the size of the pointer array.

Display the Student objects:

When you hit EOF on the input file, close it, and display the entire list of students. Go through the student objects - do not create the report while you're reading input records. Example output is below. Note that the 5th student (Colin Frankson) doesn't have a middle initial/name. Leave it blank in the output. enter image description here

My code so far:

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

const int SIZE = 100;

#ifndef Student_h
#define Student_h

struct Student
{
    char first_name[10];
    char middle_int[10];
    char last_name[20];
    char campus_code;
    char student_ID[8];
    char age[3];
};

#endif

int main()
{
    ifstream inputFile;
    inputFile.open("Students.txt", ios::binary);

    if(!inFile.is_open())
    {
        cout << "Unable to open file!";
        return 1;
    }
    else
    {
        cout << "File successfully open!\n\n";
    }

    Student *arr[SIZE];
    Student *st = nullptr;
    int total = 0;

    Student s;
    inFile.read(reinterpret_cast<char *> (&s), sizeof(s));

        
        // pseudocode:

        while(!inputFile.eof())
        {
            // dynamically allocate space for new student variables
            st = new Student();

            // populate the student variables from the input
            st = &s;
 
            // read the input record
        }

        inFile.close();         
        
        // Display the student variables with appropriate column headers and 
        // formatting to make it readable
        /*
        for(int i = 0; i < total; i++)
        {
            cout << "First Name" << "\t" << "MI" << "\t" << "Last Name" << "\t" << "Campus Code" << "\t" << "Student ID" << "\t" << "Age" << endl;
            cout << "===================================================================================================================" << endl;
        }

        */
 
        return 0;
}
Orion98
  • 91
  • 6
  • *"Use the .read(reinterpret_cast) function"* Why these instruction? :-( (With that, you cannot use directly `std::string` or `short int`) – Jarod42 Sep 19 '20 at 17:25
  • I assume the professor has good reasons to teach it that way. That's not how I'd implement the program (with those constraints). – Eljay Sep 19 '20 at 17:27
  • I assume he does too. I wouldn't use this function either. It's so confusing, that's why I need assistance on this – Orion98 Sep 19 '20 at 17:32
  • @Orion98 The requirements clearly state `read each record into a character array` and `to populate some of the Student fields, you'll need to perform some type of conversion.`. You're not following the instructions you've been given, which is why you are having trouble. Read the data into a char array first, then fill the Student record from the char array. If you don't understand how to do that, then ask a question about it instead of just ignoring it and doing something different. – john Sep 19 '20 at 17:47
  • Also to use `read` on a `std::ifstream` you must open the file in binary mode. `inputFile.open("Students.txt", std::ios_base::binary);` – john Sep 19 '20 at 17:50
  • I updated the code regarding to your suggestions. Am I on the right track? – Orion98 Sep 19 '20 at 18:04
  • @Orion98 What the last comment addressed to me? I think you are closer to a working program, but I still don't think you are following the instructions you have been given. I still don't see you reading into a char array for instance. – john Sep 20 '20 at 19:54

1 Answers1

2
struct Student
{
    std::string firstName; // 10 characters
    char middleInitial; // 1 character
    std::string lastName; // 20 characters
    char campusCode; // 1 character
    std::string studentID; // 8 characters
    short int age; // 3 characters
};

Would be a fine working structure, but with the requirement to use reinterpret_cast, you cannot use std::string, and from comment for age, short int cannot be used either.

struct RawStudent
{
    char firstName[10];
    char middleInitial;
    char lastName[20];
    char campusCode;
    char studentID[8];
    char age[3];
};

With something similar to:

RawStudent s1; // defines as an object from Student
inputFile.read(reinterpret_cast<char *> (&s1), sizeof (s1));
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    *"Assume that there will not be more than 99 input records"*, so `RawStudent s[100]; while inputFile.read(reinterpret_cast (&s[nbStudent++]), sizeof (RawStudent));` – Jarod42 Sep 19 '20 at 19:01
  • what is `nbStudent++`? – Orion98 Sep 19 '20 at 19:18
  • And do I use `RawStudent s[100];` and `inputFile.read(reinterpret_cast (&s[nbStudent++]), sizeof (RawStudent));` to replace what I have in the code? – Orion98 Sep 19 '20 at 19:38
  • 1
    To replace all your variable s1, s2, .., s6. (You have to add (global :/ ) `int nbStudent = 0;`) too – Jarod42 Sep 19 '20 at 19:40