Here is the Class Dictionary .h file.
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include <iostream>
#include <cstdlib>
#define NUM_READ_LINES 50
using namespace std;
class Dictionary
{
friend ostream& operator << (ostream&, const Dictionary&);
friend istream& operator >> (istream&, Dictionary&);
friend ifstream& operator >> (ifstream&, Dictionary&);
friend ofstream& operator << (ofstream&, const Dictionary&);
public:
char Capital[NUM_READ_LINES][50];
void openFile1();
int assignment();
const Dictionary& operator=(const Dictionary&);
Dictionary(const char *);
Dictionary();
Dictionary(const Dictionary&);
~Dictionary();
char &operator[] (int);
const char &operator[](int) const;
//Overload the relational operators for class Dictionary
bool operator==(const Dictionary&) const;
bool operator!=(const Dictionary&) const;
bool operator<=(const Dictionary&) const;
bool operator<(const Dictionary&) const;
bool operator>=(const Dictionary&) const;
bool operator>(const Dictionary&) const;
char *strPtr; //Pointer to the char array that holds the string
int strLength; //Variable to hold the length of the string
};
#endif
Here is the Dictionary .cpp file
#include "Dictionary.h"
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include <fstream>
#include <time.h>
#define NUM_READ_LINES 50
using namespace std;
Dictionary::Dictionary(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength +1];
strcpy(strPtr, str);
}
Dictionary::Dictionary()
{
strLength = 0;
strPtr = new char[1];
strcpy(strPtr, "");
}
Dictionary::Dictionary(const Dictionary& capStr)
{
strLength = capStr.strLength;
strPtr = new char[strLength +1];
strcpy(strPtr, capStr.strPtr);
}
Dictionary::~Dictionary()
{
delete [] strPtr;
}
const Dictionary& Dictionary::operator =(const Dictionary& capStr)
{
if (this != &capStr)
{
delete [] strPtr;
strLength = capStr.strLength;
strPtr = new char[strLength + 1];
strcpy(strPtr, capStr.strPtr);
}
return *this;
}
char& Dictionary::operator [](int index)
{
assert (0 <= index && index < strLength);
return strPtr[index];
}
const char& Dictionary::operator [](int index) const
{
assert (0 <= index && index < strLength);
return strPtr[index];
}
bool Dictionary::operator==(const Dictionary& capStr) const
{
return (strcmp(strPtr, capStr.strPtr) == 0);
}
bool Dictionary::operator <(const Dictionary& capStr) const
{
return (strcmp(strPtr, capStr.strPtr) == 0);
}
bool Dictionary::operator <=(const Dictionary& capStr) const
{
return (strcmp(strPtr, capStr.strPtr) == 0);
}
bool Dictionary::operator >(const Dictionary& capStr) const
{
return (strcmp(strPtr, capStr.strPtr) == 0);
}
bool Dictionary::operator >=(const Dictionary& capStr) const
{
return (strcmp(strPtr, capStr.strPtr) == 0);
}
bool Dictionary::operator !=(const Dictionary& capStr) const
{
return (strcmp(strPtr, capStr.strPtr) == 0);
}
ostream& operator << (ostream& osObject, const Dictionary& str)
{
osObject << str.strPtr;
return osObject;
}
istream& operator >> (istream& isObject, Dictionary& str)
{
char temp[81];
isObject >> setw(81) >> temp;
str = temp;
return isObject;
}
ifstream& operator >> (ifstream& isObject, Dictionary& str)
{
char temp[81];
isObject >> setw(81) >> temp;
str = temp;
return isObject;
}
//ofstream& operator << (ofstream& osObject, Dictionary& str)
//{
//osObject << str.strPtr;
//return osObject;
//}
void Dictionary::openFile1()
{
int i = 0;
int counter = 0;
ifstream inFile;
inFile.open("caps.txt");
while (!inFile.eof() && (i < NUM_READ_LINES))
{
inFile.getline(Capital[counter],50);
counter++;
}
for (int i = 0; i < counter; i++)
{
cout << Capital[i] << endl;
}
}
int Dictionary::assignment()
{
int random_num;
srand((unsigned)time(0));
//cout << (rand() % 50 + 1) << endl;
random_num = (rand() % 50 +1);
cout << "The randomly chosen word is: " << Capital[random_num] << endl;
return random_num;
}
And Main .cpp file. The first instantiation of Dict is just to test some of the functions in the class.
#include <iostream>
#include "Dictionary.h"
#include <fstream>
using namespace std;
int main()
{
Dictionary Dict;
Dict = "Kansas City";
cout << "Dict[2] = " << Dict[2] << endl;
cout << "The length of Dict is: " << Dict.strLength << endl;
Dict.openFile1();
Dict.assignment();
//Here is where everything goes wrong!
Dictionary Dict2 = Capital[random_num];
cin.get();
cin.get();
return 0;
}
All I want to do is declare Dict2 is Capital[random_num] so that I can perform the functions in the Dictionary class on the object itself.

New Topic/Question
Reply




MultiQuote




|