[prog] Another C++ problem

Sue Stones suzo at bigpond.net.au
Tue Apr 22 23:44:47 EST 2003


Dear Robert,

Thanks for your comments about the function that I posted.  I have printed 
them off so that I can go through them carefully.  In the meantime I started 
this one on reading from a file, which I thought woudl be simple, but has 
given me no end of trouble.  I have searched google, over and over, only to 
come to the conclution that this should work but it doesn't.  There are 4 
files.

The aim is to read a file with words on one line that are "synonyms" looking 
for a particular word.  If the word is found we want to return the whole 
line.  ie like looking something up in a thesaurus.  Pretty basic.

The current problem is that the getline member function (I'm not sure if that 
is the right termanology) doesn't seem to be returning a line.  If you could 
give some pointers that would be helpfull.

(A friend has just offered to lend me "C++ for dummies" which he used in the 
past, so I am hopeing that it may be better for studying alone than teh books 
that I have.)

sue



/**************************************
 *      thesaurus.h
 **************************************/
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

using std::string;

class Thesaurus
{
        public:
                Thesaurus(char *fileName);      // Default constructor
					// the word we are wearching for
                string findWord(string word);   

        private:
                std::fstream inFile;    // the object for the input file
                char lineBuffer[1024];  // allocate a buffer for a line
                                        // assuming line length < 1024
};      //class

/***********************************
 *      thesaurus.C
 *
 ************************************/
#include <stdlib.h>
#include "Thesaurus.h"

using namespace std;

Thesaurus::Thesaurus(char *fileName) // constructor
{
                // create object with input from the thesaurus file
        fstream inFile;       // I spent ages trying to get ifstream to work 
				// but couldn't so switched to fstream
        inFile.open(fileName, ios::in);
        if (!inFile)  // if there is a problem opening the file
        {
                cerr << "Can't open file: "<< fileName << endl;
                exit(1);
        }//if
}//constructor


string
Thesaurus::findWord(string word)
{
        do
        {
                inFile.getline(lineBuffer, 1024);
                cout << lineBuffer <<endl;   /* this simply produces a blank 
line, which I take it means that getline is not producing anything,
it also only produces 'one' blank line, which sujests that the loop is only 
being executed once */
                while(char *current = strtok(lineBuffer, " "))
                        if (current == word)
                                return lineBuffer;
        } while(!inFile.eof());
                        // if control reaches here the word wasn't found
        stringstream ss;
        ss << "not found";
        return ss.str();
} // findWord

/***************
 *  testwords.C  a main program to test the Thesaurus class
 * ***************/
#include <iostream>
#include "Thesaurus.h"

using std::cout;
using std::endl;

int
main()
{
        Thesaurus theFile("synonyms");
        string words = theFile.findWord("doctor");
        cout << words << endl;
        return 0;
} //main

/**********
* synonyms - the samle words, the aim is to return the whole line if the word 
is there
************/
computer processor calculator machine
doctor physician specialist
animal creature organism mammal reptile






More information about the Programming mailing list