New to the site, just need some help on this... Most of it is done, I just need to turn the wordInfo struct into a full-blown .h class. Any thoughts? It's attached... Appreciate it!

Recommended Answers

All 5 Replies

>Most of it is done
Not really. What you want to do is actually pretty involved if you want to do it right. If you want to keep it relatively simple, make the functions that work with a wordInfo object into member functions. Then from the definition of those functions, remove the parameters that are stored in the wordInfo object anyway. See how far you get and ask again if you need more help.

Got a little further. you're right about not being close cause I didn't post my .h file... (my bad) Anyway, how do you recommend implementing the member functions? Do I need to involve operator overloading so that (for example: << would call a member function?). Or am I lost completely? Thanks for the prompt reply, you guys really know what you're doing.

You are still writing C style code , put a class wordinfo having all of these as member functions.

>how do you recommend implementing the member functions?
Break it down into functions that operate on a word, functions that operate on a collection of words, and functions that really have no business being part of either. Right now you have this as your object and the function(s) that use it:

struct wordInfo {
  string wordList;
  int count;
};

bool read1Word(ifstream &infile, string &temp1Word);

Not much going on there, right? But you can still make it a class for good measure:

class wordInfo {
  string word;
  int count;
public:
  bool readWord ( istream& in );
};

bool wordInfo::readWord ( istream& in )
{
  return in>> word;
}

Notice the changes. readWord only takes a stream as the parameter, and I changed it from ifstream to istream because you might want to read words from standard input. Ideally you'd overload the >> operator for wordInfo, but that's a more advanced topic.

The real work of your program deals with a collection of words. Skipping the usual advice of using the right data structure to suit your problem, you can take what you have and wrap it all in a class called wordList:

class wordList {
  wordInfo words[ARRAY_SIZE];
  int numberWords;
public:
  void fill ( istream& in );
  void index();
  void find ( const string& key );
  void sort();

  int sumCounts();
  void zeroCounts();
private:
  int linearSearch ( const string& key );
  int binarySearch ( const string& key );
};

The parameters that have become data members aren't needed anymore because member functions have access to the data members at any given time. This greatly simplifies your interface:

bool openFile ( ifstream& infile );
void menu ( wordList& words );

int main()
{
  ifstream in;

  if ( openFile ( in ) ) {
    wordList words;

    words.fill ( in );
    words.sort();
    menu ( words );
  }
}

Now see if you can finish up the implementation of the wordList member functions. :)

Helped wonderfully, thanks a bunch. I think I got it now... Appreciate the prompt replies. I own you one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.