Date: 20/05/2017 11:05:30
From: Fee
ID: 1067998
Subject: Anyone out there an expert in C++

Hi guys

just wondered if there was anyone who could help me with a (probably simple) problem I am having with a C++ program I need to write.

I have created a class called Word which holds words and their definitions

I now need to create a class call Dictionary which holds an array (or vector) of Word objects and does various things with them. I can get the program to do the things required within the main code without creating the class but as soon as I create the class I create a whole bunch of problems and can’t seem to make it work.

Anyone have any ideas? Happy to provide code if someone wants to have a look

Thanks in advance :)

Reply Quote

Date: 20/05/2017 11:52:01
From: mollwollfumble
ID: 1068010
Subject: re: Anyone out there an expert in C++

Fee said:


Hi guys

just wondered if there was anyone who could help me with a (probably simple) problem I am having with a C++ program I need to write.

I have created a class called Word which holds words and their definitions

I now need to create a class call Dictionary which holds an array (or vector) of Word objects and does various things with them. I can get the program to do the things required within the main code without creating the class but as soon as I create the class I create a whole bunch of problems and can’t seem to make it work.

Anyone have any ideas? Happy to provide code if someone wants to have a look

Thanks in advance :)

Never used C++ though sometimes use C. Let’s have a look at your code, only the relevant parts.

Reply Quote

Date: 20/05/2017 12:10:07
From: Fee
ID: 1068022
Subject: re: Anyone out there an expert in C++

OK – below is my Dictionary class – hope that it makes sense and I haven’t sent you too much superfluous stuff. All of this code works inside the main() function so I am guessing it is my passing of parameters that is my problem.

class Dictionary
{
private: Word dictionary;
public: void loadDictionary(Dictionary*); int getMaxNoOfWords();
};

void Dictionary::loadDictionary(Dictionary* d)
{ ifstream dictionaryFile; // iostream input object dictionaryFile.open(“c:/users/fee/dictionary.txt”); // open dictionary file

string word, definition, wordType, blankLine; // variables to hold dictionary elements if (!dictionaryFile.good()) { // File not found or could not be opened cout << “File could not be opened”; return; // close program if file open fails } else { // File found – proceed cout << “Welcome to CSP2104 Object Oriented Programming with C++ \n\t\t Assignment Part 1\n\n”; cout << “\tWord Play with Supplied Dictionary (dictionary.txt)\n\n”; cout << “Loading Dictionary….” << endl; int wordTally = 0; // to establish number of words for (int i = 0; i < DICTIONARY_SIZE; i++) // ##can’t resolve eof yet** still to be done { getline(dictionaryFile, word); //————————————————- getline(dictionaryFile, definition); // Get dictionary elements getline(dictionaryFile, wordType); //————————————————— getline(dictionaryFile, blankLine); // Skip blank line after each record dictionary.setWord(word); //————————————————- dictionary.setDefinition(definition); // Set dictionary elements in array dictionary.setWordType(wordType); // ————————————————- wordTally++; // increment word counter } dictionaryFile.close(); //housekeeping – close file. } }

int Dictionary::getMaxNoOfWords()
{ return wordTally;
}

Sorry that’s probably more than you need

I thought I understood that to call the loadDictionary function you would use loadDictionary(&dictionary);

Thank you :)

Reply Quote

Date: 20/05/2017 12:21:20
From: mollwollfumble
ID: 1068036
Subject: re: Anyone out there an expert in C++

Didn’t display on forum well. Let’s see if this displays better.

Fee said:


OK – below is my Dictionary class – hope that it makes sense and I haven’t sent you too much superfluous stuff. All of this code works inside the main() function so I am guessing it is my passing of parameters that is my problem.

class Dictionary
{
private:
Word dictionary;
public:
void loadDictionary(Dictionary*);
int getMaxNoOfWords();
};

void Dictionary::loadDictionary(Dictionary* d)
{
ifstream dictionaryFile;
dictionaryFile.open(“c:/users/fee/dictionary.txt”);
string word, definition, wordType, blankLine;
if (!dictionaryFile.good())
{
cout << “File could not be opened”;
return;
}
else
{
cout << “Welcome to CSP2104 Object Oriented Programming with C++ \n\t\t Assignment Part 1\n\n”;
cout << “\tWord Play with Supplied Dictionary (dictionary.txt)\n\n”;
cout << “Loading Dictionary….” << endl;
int wordTally = 0;
for (int i = 0; i < DICTIONARY_SIZE; i++)
{
getline(dictionaryFile, word);
getline(dictionaryFile, definition);
getline(dictionaryFile, wordType);
getline(dictionaryFile, blankLine);
dictionary.setWord(word);
dictionary.setDefinition(definition);
dictionary.setWordType(wordType);
wordTally++;
}
dictionaryFile.close();
}
}

int Dictionary::getMaxNoOfWords()
{
return wordTally;
}

Reply Quote

Date: 20/05/2017 12:29:33
From: mollwollfumble
ID: 1068047
Subject: re: Anyone out there an expert in C++

Not a C++ person but …

Should “DICTIONARY_SIZE” be “Dictionary.Size” with a dot instead of underscore?

Reply Quote

Date: 20/05/2017 12:46:47
From: Fee
ID: 1068064
Subject: re: Anyone out there an expert in C++

DICTIONARY_SIZE is a constant – they ask us to use that convention when declaring them.

I did C many years ago (along with COBOL, Fortran, Pascal and RPG III) – object orientated programming is fine until I start trying to deal with the objects :)

Reply Quote

Date: 20/05/2017 21:52:20
From: btm
ID: 1068135
Subject: re: Anyone out there an expert in C++

Fee said:


OK – below is my Dictionary class – hope that it makes sense and I haven’t sent you too much superfluous stuff. All of this code works inside the main() function so I am guessing it is my passing of parameters that is my problem.

class Dictionary
{
private:
   Word dictionary;
public:
   void loadDictionary(Dictionary*);
   int getMaxNoOfWords();
};

void Dictionary::loadDictionary(Dictionary* d)
{
   ifstream dictionaryFile; // iostream input object
   dictionaryFile.open(“c:/users/fee/dictionary.txt”); // open dictionary file

   string word, definition, wordType, blankLine; // variables to hold dictionary elements

   if (!dictionaryFile.good())
   {
   // File not found or could not be opened

     cout << “File could not be opened”;
     return; // close program if file open fails
   }
   else
   {
   // File found – proceed

     cout << “Welcome to CSP2104 Object Oriented Programming with C++ \n\t\t Assignment Part 1\n\n”;
     cout << “\tWord Play with Supplied Dictionary (dictionary.txt)\n\n”;
     cout << “Loading Dictionary….” << endl;
     int wordTally = 0; // to establish number of words for (int i = 0; i < DICTIONARY_SIZE; i++) // ##can’t resolve eof yet** still to be done
     {
     getline(dictionaryFile, word); //————————————————-
     getline(dictionaryFile, definition); // Get dictionary elements
     getline(dictionaryFile, wordType); //—————————————————
     getline(dictionaryFile, blankLine); // Skip blank line after each record

     dictionary.setWord(word); //————————————————-
     dictionary.setDefinition(definition); // Set dictionary elements in array
     dictionary.setWordType(wordType); // ————————————————-

     wordTally++; // increment word counter
     }

   dictionaryFile.close(); //housekeeping – close file.
   }
}

int Dictionary::getMaxNoOfWords()
{
   return wordTally;
}

Sorry that’s probably more than you need

I thought I understood that to call the loadDictionary function you would use loadDictionary(&dictionary);

Thank you :)

I can’t really tell you exactly what’s wrong without knowing more (like exactly what issues you’re having, and what the Word class contains), but several things seem to stand out at first glance:

Reply Quote

Date: 25/05/2017 11:29:21
From: Fee
ID: 1070403
Subject: re: Anyone out there an expert in C++

Hello Mollwollfumble and BTM

Sorry it took me so long to get onto my real computer and not my phone to respond. I just wanted to say I really appreciated your help. I have had success in making the dictionary class work and am thrilling myself with the next steps in my program, namely palindromes, anagrams and word guessing games…so far I think I am actually developing a fairly reasonable handle on the whole thing.

Anyway, thanks so much guys for your help. You gave me things to think about and I reckon I will be able to actually submit my assignment as a working program :D

Cheers

Fee

Reply Quote

Date: 25/05/2017 11:52:17
From: mollwollfumble
ID: 1070419
Subject: re: Anyone out there an expert in C++

:-)

Reply Quote