String/character input with getline
Hi all,
I'm able to use getline() in my loadLecturers() function if the text file contains one-word names, however, if I want a forename/surname combination it reads nothing and outputs a blank line.
I've tried this using string variables AND char arrays but no luck with either.
I'd be very grateful for any help whatsoever!
Please find my code and text file contents below for your perusal.
Code:
4
2
l4
Joe McDonnell
3
l5
Tom Hunt
5
l6
Craig Gooding
6
l7
Josh Dunbar
Code:
//////////////////////////////////////////////////////////////////////////
// Title : Aggregation and Composition in a University System
// Author :
// Date :
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Pre-processor Directives
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
#define MAXDEPTSPERUNIV 10
#define MAXLECTSPERDEPT 5
#define MAXSTRINGLENGTH 25
//////////////////////////////////////////////////////////////////////////
// Class Definition
//////////////////////////////////////////////////////////////////////////
class lecturerClass
{
private:
string lecturerName;
public:
lecturerClass() { lecturerName = ""; }
string getName() { return lecturerName; }
void setName(string newName) { lecturerName = newName; }
};
//////////////////////////////////////////////////////////////////////////
// Class Definition
//////////////////////////////////////////////////////////////////////////
class departmentClass
{
private:
string departmentName;
int numLecturers;
lecturerClass* lecturer[ MAXLECTSPERDEPT ]; // Aggregation
public:
departmentClass();
string getDepartmentName() { return departmentName; }
void addLecturer( lecturerClass &newLecturer );
void printLecturerNames();
void setDN( string );
};
//////////////////////////////////////////////////////////////////////////
// Member Function Implementations
//////////////////////////////////////////////////////////////////////////
void departmentClass::setDN( string a )
{
departmentName = a;
} // end function setDN
//////////////////////////////////////////////////////////////////////////
void departmentClass::printLecturerNames()
{
for ( int j = 0; j < numLecturers; j++ )
{
cout << "\t" << lecturer[ j ]->getName() << endl;
} // end for loop
} // end function printLecturerNames
//////////////////////////////////////////////////////////////////////////
departmentClass::departmentClass()
{
departmentName = "";
numLecturers=0;
}
//////////////////////////////////////////////////////////////////////////
void departmentClass::addLecturer(lecturerClass &newLecturer)
{
lecturer[numLecturers] = &newLecturer;
numLecturers++;
}
//////////////////////////////////////////////////////////////////////////
// Class Definition
//////////////////////////////////////////////////////////////////////////
class universityClass
{
private:
departmentClass department[MAXDEPTSPERUNIV]; // Composition
int currentNumDepts;
string name;
public:
void addLecturer( lecturerClass &lect, int deptcode );
void intialiseDepartmentNames();
string getUniversityName();
void setUniversityName( string );
void printDeptNames();
void loadDepts();
void loadLecturers();
};
//////////////////////////////////////////////////////////////////////////
// Member Function Implementations
//////////////////////////////////////////////////////////////////////////
void universityClass::loadLecturers()
{
ifstream lectsFile( "D:\\lecturerNames.txt", ios::in );
// exit program if unable to create file
if ( !lectsFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if
// declare two integer and two string variables
char lecturerName[ MAXSTRINGLENGTH ] = "", objectName[ MAXSTRINGLENGTH ] = "";
/* string lecturerName, objectName; */
int header, departmentID;
// read the header value to determine the number of records
lectsFile >> header;
cout << header << endl;
// read records adding each one
while ( header > 0 )
{
// read record
lectsFile >> departmentID;
/* getline( lectsFile, objectName );
getline( lectsFile, lecturerName ); */
lectsFile.getline( objectName, MAXSTRINGLENGTH );
lectsFile.getline( lecturerName, MAXSTRINGLENGTH );
cout << departmentID << endl << lecturerName << endl << objectName << endl << endl;
/*
// create LecturerClass object
lecturerClass objectName;
// set lecturer name
objectName.setName( lecturerName );
// add the lecturer to the appropriate department with departmentID
addLecturer( objectName, departmentID );
*/
// decrement header value
header--;
} // end while loop
} // end function loadLecturers
//////////////////////////////////////////////////////////////////////////
void universityClass::loadDepts()
{
ifstream deptsFile( "D:\\departmentNames.txt" );
if ( !deptsFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if
string a;
int i = 0;
while ( i < MAXDEPTSPERUNIV )
{
deptsFile >> a;
department[ i ].setDN( a );
i++;
} // end while loop
} // end function loadDepts
//////////////////////////////////////////////////////////////////////////
void universityClass::printDeptNames()
{
for ( int i = 0; i < MAXDEPTSPERUNIV; i++ )
{
cout << department[ i ].getDepartmentName() << endl;
department[ i ].printLecturerNames();
} // end for loop
} // end function printDeptNames
//////////////////////////////////////////////////////////////////////////
string universityClass::getUniversityName()
{
return name;
} // end function getUniversityName
//////////////////////////////////////////////////////////////////////////
void universityClass::setUniversityName( string uniName )
{
name = uniName;
} // end function setUniversityName
//////////////////////////////////////////////////////////////////////////
void universityClass::addLecturer(lecturerClass &lect, int deptcode)
{
department[deptcode].addLecturer(lect);
} // end function addLecturer
//////////////////////////////////////////////////////////////////////////
void universityClass::intialiseDepartmentNames()
{
department[0].setDN("Engineering");
department[1].setDN("Computing");
department[2].setDN("Psychology");
department[3].setDN("Divination");
department[4].setDN("Chemistry");
department[5].setDN("Biology");
department[6].setDN("Potions");
department[7].setDN("Economics");
department[8].setDN("Dark Arts");
department[9].setDN("Ninja");
currentNumDepts=10;
}
//////////////////////////////////////////////////////////////////////////
// Main Function Implementation
//////////////////////////////////////////////////////////////////////////
int main(void)
{
universityClass angliaRuskin;
angliaRuskin.intialiseDepartmentNames();
lecturerClass l1, l2, l3;
l1.setName("Ian van der Linde");
l2.setName("Antony Carter");
l3.setName("Mike Smith");
angliaRuskin.addLecturer(l1, 1);
angliaRuskin.addLecturer(l2, 4);
angliaRuskin.addLecturer(l3, 8);
angliaRuskin.setUniversityName( "Anglia Ruskin University" );
cout << angliaRuskin.getUniversityName() << endl << endl;
angliaRuskin.loadDepts();
angliaRuskin.loadLecturers();
// angliaRuskin.printDeptNames();
} // end main