Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

These products shall be delivered to the instructor electronically via e-mail as

ID: 3859780 • Letter: T

Question

These products shall be delivered to the instructor electronically via e-mail as specified below.
3.1 Sprint Report -- The student shall provide filled out Sprint Report form for instructor approval NLT (Not Later Than) Wednesday, July 12.
3.2 Program source files -- The student shall provide fully tested electronic copies of the .cpp and .h files.

buildLibrary() Function to be Added to the Library Class

getNextLine() Function to be Added to the Library Class

1.0 Overview Madame Pince, the head librarian at Hogwarts has expressed an interest in the new "Computational Magic" to Professor Dumbledore and has asked if it would be possible to obtain a program which would catalog and store information on all of the books in the Hogwarts Library. Professor Dumbledore has requested just such a utility program which will be implemented as a binary tree. 2.0 Requirements The student shall define, develop, document, prototype, test, and modify as required the software system. 2.1 This software system shall define a class, called Library which maintains a Binary Tree of instances of a structure called Book as defined in section 2.3. 2.2 The Library class shall implement all the functionality of a binary tree and shall contain variables and functions as described below: 2.2.1 Member variables:--this class shall contain one private member variable. This shall be a pointer to a Book structure and shall be named m_pRoot. The variable m_pRoot shall point to the first instance of Book in the tree. 2.2.2 Library(), ~Library--these functions are the default constructor and destructor. The destructor function shall delete all instances of Book from the tree by calling the recursive function destroyTree passing in the root. 2.2.3 bool buildLibrary(char *fileName)--this function takes a single argument, a character array giving the name of a data file (provided by the instructor). It will open the data file and read all data from the data file, create Book structures to hold the data, and call the addBook() function to add each book to the database. This function shall be provided by the instructor. This function shall be public. 2.2.4 bool addBook(Book *newBook)--this function takes a single argument, a pointer to a Book structure. It will then add this book to the binary tree of books. This function shall use the book number of the book as the key for insertion into the binary tree. It will return true if the book was successfully added to the tree or false if it failed to add the book. This function shall be public. 2.2.5 Book *removeBook(int bookNum)--this function takes a book identification number as it's only argument. It shall locate the book in the tree of books and remove this book from the binary tree. It will set the left and right pointers of the removed Book structure to NULL and return a pointer to the book. If the item was not found in the tree it shall return NULL. This function shall be public.
Remember that if the node to be removed from the tree has two children it actually get's overwritten. So in this case you will need to create a duplicate Book structure, and copy the data out of the one to be overwritten. Then after completing the deletion for a node with two children return the pointer to the new duplicate Book. 2.2.6 Book *getBookByNumber(int bookNum)--this function takes a book identification number as its' only argument. It will then locate this book in the tree of books and return a pointer to the book. If the book was not found it shall return NULL. You may assume, in this case, that it is OK to return a pointer to a book in the tree without worrying about it giving access to other items in the tree. This function shall be public 2.2.7 Book *getBookByTitle(char *title)--this function takes a book title as a character array as its' only argument. It just calls the private overloaded getbookByTitle() function and returns whatever that call returns. This function shall be public 2.2.8 Book *getBookByTitle(char *title, Book *rt)--this function takes a book title as a character array, and a pointer to the root of a sub-tree as its' two arguments. It will then locate this book in the tree of books and return a pointer to the book. If the book was not found it shall return NULL. This must be a variation on the recursive traversal of a binary tree algorithm. (Note: It will take some careful thought to be sure this one works correctly. We will discuss this function in class.) You may assume, in this case, that it is OK to return a pointer to a book in the tree without worrying about it giving access to other items in the tree. This function shall be private 2.2.9 void printLibrary()--this is a public function which will just call the private function printAll() as defined in section 2.2.10. 2.2.10 void printOne(Book *book)--this function shall take a pointer to a Book structure and print on the screen all information in the structure. This shall include the book identification number, book title, and author. This function shall be private as it should only be called by the printAll() function defined in section 2.2.10. 2.2.11 void printAll(Book *rt)--this shall be a private function which is called by the public function printLibrary(). It takes a single argument, a pointer to the root of a tree. It will perform an in-order recursive traversal of the tree and print all information in each book in the tree. It can call the function printOne() to print the information on the Book passed in as an argument. 2.2.12 bool getNextLine(char *line, int lineLen)-- this shall be a private function which can be called by the buildLibrary() function. It will read the next data line from the file referenced by the inFile argument and place it in the character array pointed to by the line argument. This function will be provided by the instructor. 2.2.13 void destroyTree(Book *rt)--this shall be a private function which can be called by the class destructor. It will recursively traverse the tree and delete all nodes from the tree. 2.3 This software system shall define a structure called Book which contains all information to define a book in the Hogwarts library. Code defining this structure shall be stored in a header file called Book.h. A Book.h file is included in the zip file which can be downloaded from the link given below. 2.3.1 An int, called bookNumber. This variable shall be used as the key. 2.3.2 A character array, called Title capable of holding strings of up to 127 characters. This array shall hold the title of a book. 2.3.3 A character array, called Author capable of holding strings of up to 64 characters. This array shall hold the author's name. 2.3.4 Two pointers, called left and right which shall point to Book structures and allow the building of binary trees of Book structures. 2.4 The Library class file and its' associated header file must be capable of being compiled and linked in with a driver program for testing. 3.0 Deliverables

These products shall be delivered to the instructor electronically via e-mail as specified below.
3.1 Sprint Report -- The student shall provide filled out Sprint Report form for instructor approval NLT (Not Later Than) Wednesday, July 12.
3.2 Program source files -- The student shall provide fully tested electronic copies of the .cpp and .h files.

buildLibrary() Function to be Added to the Library Class

  Note: In order to use the the function below you will need to         add the following includes and the using namespace std        command to your Library.cpp file.    #include <iostream>  #include <fstream>  #include <string.h>  #include <stdlib.h>    using namespace std;      //-------------------------------------------------------------  // Function: buildLibrary()  // Purpose: Build the library from a data file  // Args: fileName - character array holding the name of the  //         datafile defining the library.  // Returns; True if library was successsfully built.  //-------------------------------------------------------------  bool Library::buildLibrary(char *fileName)  {      ifstream  inFile;      Book *bk;      char line[128];                inFile.open(fileName, ifstream::in);       if(!inFile.is_open())      {          // inFile.is_open() returns false if the file could not be found or          //    if for some other reason the open failed.          cout << "Unable to open file " << fileName << ".  Program terminating... ";          return false;      }        //first time reading file: Set GraphNode ID and Data      while (getNextLine(inFile, line, 127)) //while the next line is readable      {                     bk = new Book();          bk->left = bk->right = NULL;          bk->bookNumber = atoi(line);            // Read the book title          getNextLine(inFile, line, 127);          strcpy(bk->Title, line);            // Read the author          getNextLine(inFile, line, 127);          strcpy(bk->Author, line);            // Add this book to the tree          addBook(bk);      }      return true;  }  

getNextLine() Function to be Added to the Library Class

  //----------------------------------------------------------------  // Function: getNextLine()  // Purpose: Read a line from the data file skipping blank lines  //           and comment lines beginning with #  // Args: inFile - reference argument to an open ifstream object  //         to read from.  //       line - character array into which the data line is read  //       lineLen - maximum number of characters which can be read  //          into the array line  // Returns: True if a successful read was done.  If False is  //       returned then the array line will be zero length.  //----------------------------------------------------------------  bool Library::getNextLine(ifstream &inFile, char *line, int lineLen)   {      int    done = false;      while(!done)      {          inFile.getline(line, lineLen);                  if(inFile.good())    // If a line was successfully read          {              if(strlen(line) == 0)  // Skip any blank lines                  continue;              else if(line[0] == '#')  // Skip any comment lines                  continue;              else done = true;    // Got a valid data line so return with this line          }          else          {              strcpy(line, "");              return false;    // Flag end of file with null string and return false          }      } // end while      return true; // Flag successful read  }  

Explanation / Answer

HogwartsSWW.cpp
------------------------------
#include "HogwartsSWW.h"
#include <iostream>
#include <stdio.h>

using namespace std;

//-----------------------------------------------------
// Default constructor
//-----------------------------------------------------
HogwartsSWW::HogwartsSWW()
{
   m_pGryffindor = new House();
   m_pGryffindor->SetHouseName("Gryffindor");
   m_pRavenclaw = new House();
   m_pRavenclaw->SetHouseName("Ravenclaw");
   m_pHufflepuff = new House();
   m_pHufflepuff->SetHouseName("Hufflepuff");
   m_pSlytherin = new House();
   m_pSlytherin->SetHouseName("Slytherin");
}

//-----------------------------------------------------
// Destructor
//-----------------------------------------------------
HogwartsSWW::~HogwartsSWW()
{
   delete m_pGryffindor;
   delete m_pRavenclaw;
   delete m_pHufflepuff;
   delete m_pSlytherin;
}

//-----------------------------------------------------
// AddStudent()
// Purpose: Add a student to a house
// Args: stu - pointer to a Student object
// Returns: bool - true if add was successful
//-----------------------------------------------------
bool HogwartsSWW::AddStudent(Student *stu)
{
   char house[64];
   stu->getHouse(house);

   if(strcmp(house, "Gryffindor") == 0)
       return m_pGryffindor->AddStudent(stu);
   else if(strcmp(house, "Ravenclaw") == 0)
       return m_pRavenclaw->AddStudent(stu);
   else if(strcmp(house, "Hufflepuff") == 0)
       return m_pHufflepuff->AddStudent(stu);
   else if(strcmp(house, "Slytherin") == 0)
       return m_pSlytherin->AddStudent(stu);
   else
       return false;
}

//-----------------------------------------------------
// RemoveStudent()
// Purpose: Remove a student from a house
// Args: house - Name of the student's house
//       stu - pointer to a Student object
// Returns: bool - true if removal was successful
//-----------------------------------------------------
bool HogwartsSWW::RemoveStudent(char *house, int studentID)
{
   if(strcmp(house, "Gryffindor") == 0)
       return m_pGryffindor->RemoveStudent(studentID);
   else if(strcmp(house, "Ravenclaw") == 0)
       return m_pRavenclaw->RemoveStudent(studentID);
   else if(strcmp(house, "Hufflepuff") == 0)
       return m_pHufflepuff->RemoveStudent(studentID);
   else if(strcmp(house, "Slytherin") == 0)
       return m_pSlytherin->RemoveStudent(studentID);
   else
   {
       cout << "Unknown house given in RemoveStudent() function. ";
       return false;
   }
}

//-----------------------------------------------------
// FindStudent()
// Purpose: Find a student in a house
// Args: house - Name of the student's house
//       stu - pointer to a Student object
// Returns: Pointer to a clone of the student record
//-----------------------------------------------------
Student *HogwartsSWW::FindStudent(char *house, int studentID)
{
   if(strcmp(house, "Gryffindor") == 0)
       return m_pGryffindor->FindStudent(studentID);
   else if(strcmp(house, "Ravenclaw") == 0)
       return m_pRavenclaw->FindStudent(studentID);
   else if(strcmp(house, "Hufflepuff") == 0)
       return m_pHufflepuff->FindStudent(studentID);
   else if(strcmp(house, "Slytherin") == 0)
       return m_pSlytherin->FindStudent(studentID);
   else
   {
       cout << "Unknown house given in FindStudent(char *, int) function. ";
       return NULL;
   }
}

//-----------------------------------------------------
// FindStudent()
// Purpose: Find a student in a house
// Args: house - Name of the student's house
//       fname - Student's first name
//       lname - Student's last name
// Returns: Pointer to a clone of the student record
//-----------------------------------------------------
Student *HogwartsSWW::FindStudent(char *house, char *fname, char *lname)
{
   if(strcmp(house, "Gryffindor") == 0)
       return m_pGryffindor->FindStudent(fname, lname);
   else if(strcmp(house, "Ravenclaw") == 0)
       return m_pRavenclaw->FindStudent(fname, lname);
   else if(strcmp(house, "Hufflepuff") == 0)
       return m_pHufflepuff->FindStudent(fname, lname);
   else if(strcmp(house, "Slytherin") == 0)
       return m_pSlytherin->FindStudent(fname, lname);
   else
   {
       cout << "Unknown house given in FindStudent(char *, char *, char *) function. ";
       return NULL;
   }
}


//-----------------------------------------------------
// PrintHouses()
// Purpose: Print all information on all students in
//               all houses
// Args: None
// Returns: void
//-----------------------------------------------------
void HogwartsSWW::PrintHouses()
{
   cout << "Students at Hogwarts ";
   m_pGryffindor->PrintHouseList();
   cout << " Press any key to see the next house listing...";
   getchar();
   cout << " ";
   m_pRavenclaw->PrintHouseList();
   cout << " Press any key to see the next house listing...";
   getchar();
   cout << " ";
   m_pHufflepuff->PrintHouseList();
   cout << " Press any key to see the next house listing...";
   getchar();
   cout << " ";
   m_pSlytherin->PrintHouseList();
}
---------------------------------------------------
HogwartsSWW.h
------------------------------
#define HOGWARTSSWW_H

#include "House.h"
#include "Student.h"

class HogwartsSWW
{
   private:
       House   *m_pGryffindor;
       House   *m_pRavenclaw;
       House   *m_pHufflepuff;
       House   *m_pSlytherin;

   public:
       HogwartsSWW();                                   // Default constructor
       ~HogwartsSWW();                                   // Destructor
       bool AddStudent(Student *stu);                   // Add a student to a house
       bool RemoveStudent(char *house, int studentID); // Remove a student given an ID
       Student *FindStudent(char *house, int studentID);// Find a student given the student ID
       Student *FindStudent(char *house, char *fname,   // Overloadef find function. Find a student
                       char *lname);                     //   given the first and last names
       void PrintHouses();
};
#endif
---------------------------------------------------
House.cpp
------------------------------
#include "Student.h"
#include "House.h"
#include<string.h>
#include<iostream>
using namespace std;

House::House()
{
head = NULL;
}

House::~House()
{
Student *temp;
  
   if(!(head==NULL))
       {
           temp = head;
           while (head != NULL)
           {
               temp = head;
               head = head->m_pNext;
               delete temp;
           }
       }

}

bool House::AddStudent(Student *stu)
{
  
  
   if(head == NULL)
   {
       head = stu;  
       return true;
   }
  
   else if((stu->getStudentID)<(head->getStudentID))
   {
       stu->m_pNext = head;
       head = stu;
       return true;
   }
  
   else if((stu->getStudentID)>(head->getStudentID))
   {
       Student *tempStu = head;
       Student *backStu = NULL;
      
       while((tempStu!=NULL)&&((tempStu->getStudentID)<(stu->getStudentID)))
       {
       backStu=tempStu;
       tempStu = tempStu->m_pNext;
       }
       backStu->m_pNext = stu;
       stu->m_pNext=tempStu;
   }
   return true;
}

bool House::RemoveStudent(int studentID)
{
   if (head==NULL)
   {
       return false;
   }

   Student *tempStu = head;
   Student *backStu = NULL;
  
   while((tempStu!=NULL)&&((tempStu->getStudentID)!=(studentID)))
       {
       backStu=tempStu;
       tempStu = tempStu->m_pNext;
       }

   if (backStu==NULL)
   {
       head = head->m_pNext;
       delete tempStu;
       return true;
   }

   else if ((tempStu!=NULL)&&((tempStu->getStudentID)==(studentID)))
   {
       backStu->m_pNext = tempStu->m_pNext;
       delete tempStu;
       return true;
   }

}

Student *House::FindStudent(int studentID)
{
   if(head == NULL)
   {
       return NULL;
   }
  
   Student *tempStu = head;
  
   while(tempStu!=NULL)
   {
       if((tempStu->getStudentID)!=(studentID))
       {
           tempStu = tempStu->m_pNext;
       }
          
       else if((tempStu->getStudentID)==(studentID))
       {
           return tempStu->Clone;
       }
   }

   if (tempStu == NULL)
   {
       return NULL;
   }
}
  

Student *House::FindStudent(char *fname, char* lname)
{
   if(head == NULL)
   {
       return NULL;
   }
  
   Student *tempStu = head;
   char tempFname[65], tempLname[65];

   while(tempStu!=NULL)
   {
       tempStu->getName(tempFname, tempLname);
       if(strcmp(tempLname, lname)!=0)
       {
           tempStu = tempStu->m_pNext;
       }  
       else if((strcmp(tempLname, lname)==0)&&(strcmp(tempLname, lname)==0))
       {
           return tempStu->Clone;
       }
   }

}

void House::PrintHouseList()
{

   cout<<"House ";
   for (int i = 0;i = strlen(hName);i++)
   {
       cout<<hName[i];
   }

   cout<<endl<<"*************************"<<endl<<endl;
   Student *tempStu = head;
   while(tempStu!=NULL)
   {
       tempStu->printStudentInfo;
       tempStu=tempStu->m_pNext;
       cout<<endl<<"*************************"<<endl<<endl;
   }
}
      
void House::SetHouseName(char *name)
{
   strcpy(hName, name);
}

char *House::GetHouseName()
{
   return hName;
}
---------------------------------------------------
House.h
------------------------------
#pragma once

#include "Student.h"

class House
{
   private:
       Student *head;
       char hName[];

   public:
       House();
       ~House();
       bool AddStudent(Student *stu);
       bool RemoveStudent(int studentID);
      
       Student *FindStudent(int studentID);
       Student *FindStudent(char *fname, char* lname);
      
       void PrintHouseList();
      
       void SetHouseName(char *name);
       char *GetHouseName();


};
---------------------------------------------------
Student.cpp
------------------------------
#include"Student.h"
#include<string.h>
#include<iostream>
using namespace std;


//Default Constructor
Student::Student()
{
   //set all Class variables to NULL;
   m_iStudentID = 0;
   for (int i=0; i < int(strlen(m_sMagicalName));i++)
       {
           m_sMagicalName[i]=NULL;
       }
  
   for (int i=0; i < int(strlen(m_sWizardFamilyName));i++)
       {
           m_sWizardFamilyName[i]=NULL;
       }
  
   for (int i=0; i < int(strlen(m_sHouse));i++)
       {
           m_sHouse[i]=NULL;
       }
  
   for (int i=0;i<8;i++)
       {          
           m_sClasses[i]=NULL;  
       }
  
   for (int i=0;i<8;i++)
       {
           m_iGrades[i]=0;
       }
}

//Parametrized Conststructor sets Class variables to StudentID, Name, and House, other variables set to Nul
Student::Student(int iID, char *mName, char *wName, char *hName)
{
   m_iStudentID = iID;
   strcpy(m_sMagicalName, mName);
   strcpy(m_sWizardFamilyName, wName);
   strcpy(m_sHouse, hName);
  
   for (int i=0;i<8;i++)
   {  
       m_sClasses[i]=NULL;  
   }
  
   for (int i=0;i<8;i++)
   {
       m_iGrades[i]=0;
   }
}

//Class Destructor
Student::~Student()
{
  
   //Clear dynamically allocated memory for array of pointers to Class Name character arrays
   for(int i = 0;i<8;i++)
   {
       if(m_sClasses[i]!=NULL)
       {

       delete[] m_sClasses[i];
       }
   }

}

Student *Student::Clone()
{
  
   Student *tempStu = new Student;
   tempStu->setStudentID(m_iStudentID);
   tempStu->setName(m_sMagicalName, m_sWizardFamilyName);
   tempStu->setHouse(m_sHouse);
  
   for(int i=0;i<8;i++)
   {
       if(m_sClasses[i] != NULL)
       {
       tempStu->setClass(i, m_sClasses[i]);
       tempStu->setGrade(i, m_iGrades[i]);
       }
   }
   return tempStu;
  
}

//get StudentID returns student ID no. variable
int Student::getStudentID()
{
   return m_iStudentID;
}

//set Student ID sets student ID class variable to passed argument
void Student::setStudentID(int iID)
{
   m_iStudentID = iID;
}

//get Name retrieves class variable in form of char arrays, for student's first and last name, copies to character array arguments
void Student::getName(char *mName, char*wName)
{
   strcpy(mName, m_sMagicalName);
   strcpy(wName, m_sWizardFamilyName);
}

//set Name function copies char array argument to the appropriate variable in the Student Class
void Student::setName(char *mName, char*wName)
{
   strcpy(m_sMagicalName, mName);
   strcpy(m_sWizardFamilyName, wName);
}

//get house function copies class variable for student's house name to argument pointing to character array
void Student::getHouse(char *hName)
{
   strcpy(hName, m_sHouse);
}


//set house function copies argument pointing to character array to Student class variable for House Name
void Student::setHouse(char *hName)
{
   strcpy(m_sHouse, hName);
  
}

//get Class copies char array for student's class at argument: idx out of pointer array: m_sClasses[] to argument: character array className
void Student::getClass(int idx, char *className)
{
   if((idx>=0)&&(idx<8))
   {
       if(m_sClasses[idx]!=NULL)
           {
           strcpy(className, m_sClasses[idx]);
           }
   }
}

//set Class copies char array argument className for student's class to argument: idx out of pointer array: m_sClasses[]
void Student::setClass(int idx, char *className)
{
   if((idx>=0)&&(idx<8))
   {
       if(m_sClasses[idx]!=NULL)
           {
               delete m_sClasses[idx];
           }
       m_sClasses[idx] = new char[strlen(className)+1];
       strcpy(m_sClasses[idx], className);
   }
}

//get Grade function gets student's integer grade at appropriate index of array, copies to argument iGrade, if statements check ranges to determine letter grade and set to char cGrade
void Student::getGrade(int idx, int &iGrade, char &cGrade)
{
   if((idx>=0)&&(idx<8))
   {
       iGrade = m_iGrades[idx];
       if (iGrade>=95)
       {
           cGrade='O';
       }
       else if ((iGrade>=90)&&(iGrade<95))
       {
           cGrade='E';
       }
       else if ((iGrade>=80)&&(iGrade<90))
       {
           cGrade='A';
       }
       else if ((iGrade>=70)&&(iGrade<80))
       {
           cGrade='P';
       }
       else if ((iGrade>=60)&&(iGrade<70))
       {
           cGrade='D';
       }
       else if(iGrade<60)
       {
           cGrade='T';
       }
   }
}

//set Grade function sets student's grade at appropriate index of m_iGrades array
void Student::setGrade(int idx, int grade)
{
   if((idx>=0)&&(idx<8))
   {
       m_iGrades[idx] = grade;
   }
}

//prints all student info in appropriate format
void Student::printStudentInfo()
{
   //Print Student ID no.
   cout<<"Student ID#:   "<<m_iStudentID<<endl;
  
   //Print Student Name: Last, First
   cout<<"Student Name:   ";
   for (int i=0; i < int(strlen(m_sWizardFamilyName));i++)
       {
           cout<<m_sWizardFamilyName[i];
       }
  
   cout<<", ";
  
   for (int i=0; i < int(strlen(m_sMagicalName));i++)
       {
           cout<<m_sMagicalName[i];
       }
  
   cout<<endl;
  
   //Print Student House name
   cout<<"Student House:   ";
   for (int i=0; i < int(strlen(m_sHouse));i++)
   {
       cout<<m_sHouse[i];
   }
  
   //Print Student Classes and Grades
   char tempS[65];//character array for printing each class
   char charGrade;//variable to hold each letter grade
   int intGrade;//variable to hold each class's integer grade
   cout<<endl<<endl<<"CLASSES       GRADES"<<endl;
  
   for (int i=0; i<8; i++)
       {
           //output student's classes
           if(m_sClasses[i]!=NULL)
           {
               strcpy(tempS, m_sClasses[i]);
               for (int j=0;j<strlen(tempS);j++)
               {
                   cout<<tempS[j];
               }
               //output students integer and letter grade
               getGrade(i, intGrade, charGrade);
               cout<<"       "<<intGrade<<"   "<<charGrade<<endl;
           }
       }

}
---------------------------------------------------
Student.h
------------------------------
#pragma once

class Student
{
private:
   int m_iStudentID;//Student ID number
   char m_sMagicalName[65];//char array for student's first name
   char m_sWizardFamilyName[65];//char array for student's last name
   char m_sHouse[65];//char array for student's house name
   char *m_sClasses[8];//array of pointers to char arrays for student's classes
   int m_iGrades[8];//array of integers for student's classes

public:
   Student *m_pNext;
  
  
   Student();//default constructor
   Student(int iID, char *mName, char *wName, char *hName);//paremetrized constructor
   ~Student();//destructor
  
   Student *Clone();
   //get and set student id variable
   int getStudentID();
   void setStudentID(int iID);
  
   //get and set first and last name variables
   void getName(char *mName, char*wName);
   void setName(char *mName, char*wName);
  
   //get and set house name array
   void getHouse(char *hName);
   void setHouse(char *hName);
  
   //get and set classes from an array of eight pointers
   void getClass(int idx, char *className);
   void setClass(int idx, char *className);
  
   //get and set integer and letter grades for each class
   void getGrade(int idx, int &iGrade, char &cGrade);
   void setGrade(int idx, int grade);
  
   //print all student info
   void printStudentInfo();
};
---------------------------------------------------
TestInDaHouse.cpp
------------------------------
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "HogwartsSWW.h"
#include "Student.h"
#include "House.h"

using namespace std;

// Prototype functions to read lines from the file and build the houses
bool getNextLine(char *line, int lineLen);
void buildStudentLists(HogwartsSWW *h);

ifstream   inFile;   // File to read from

// Define the name of the file to read from
#define STUDENT_FILE "students.txt"

//*/========================================================
//
//2. Add the following functions below main()
//
//*/========================================================
void buildStudentLists(HogwartsSWW *h)
{   
   char line[128];// Declare a character array to hold lines read from file
   Student *s;
   char fname[32];
   char lname[32];
   int numClasses;

   // Open the data file
   inFile.open(STUDENT_FILE, ifstream::in);
   if(!inFile.is_open())
   {
       cout << "Failed to open file: " << STUDENT_FILE << " application terminating. ";
       exit(0);
       }
   while(getNextLine(line, 128))
   {
       s = new Student();       // Create a new student
       s->setStudentID(atoi(line));   // Set the ID

       // Read and parse student name
       getNextLine(line, 128);
       sscanf(line, "%s %s", fname, lname);
       s->setName(fname, lname);

       // Read and parse house name
       getNextLine(line, 128);
       s->setHouse(line);

       // Read all classes
       getNextLine(line, 128);   // Read and parse number of classes
       numClasses = atoi(line);
       for(int i=0; i<numClasses; i++)
       {
           getNextLine(line, 128); // Get name of class
           s->setClass(i, line);
           getNextLine(line, 128); // Get grade
           s->setGrade(i, atoi(line));
       }

       // Add this student to the school
       h->AddStudent(s);
   }
}

//-------------------------------------------
// GetNextLine()
// Read the next line from the file.
//-------------------------------------------
bool getNextLine(char *line, int lineLen)
{
    int    done = false;

    while(!done)
    {
        inFile.getline(line, lineLen);
      
        if(inFile.good())    // If a line was successfully read
        {
           if(strlen(line) == 0) // Skip any blank lines
                continue;
            else if(line[0] == '#') // Skip any comment lines
                continue;
            else done = true;    // Got a valid data line so return
                                 // with this line
        }
        else
        {
            strcpy(line, "");
            return false;
        }
    } // end while
    return true;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote