Thread: String/character input with getline

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Wink 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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    The short answer is use getline() for everything, then extract what you need from a string in memory.

    Mixing getline() and >> methods is a recipe for disaster.

    You can hack it to some extent by doing
    fin >> var;
    fin.ignore(); // burn the troublesome newline
    fin.getline();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline(cin,input)
    By Aliaks in forum C++ Programming
    Replies: 13
    Last Post: 07-06-2009, 11:45 AM
  2. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  3. Replies: 5
    Last Post: 04-12-2006, 06:30 PM
  4. cin.getline() needs input twice
    By CtrlAltKick in forum C++ Programming
    Replies: 3
    Last Post: 07-06-2002, 04:56 PM
  5. trapping control character in getline
    By iain in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2001, 12:05 PM