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!
bluejay_1111 0 Newbie Poster
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
/*********************************************************************/
/* Author: ******* ****** */
/* Course: CIS136 */
/* Assignment: #2 */
/* Due Date: October 20 , 2007 */
/* Filename: wordInfo.cpp */
/* Purpose: This program will contain all the properties */
/* of p1.cpp but will make "wordInfo" into a .h file */
/**********************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <istream>
#include <sstream>
// Variable Value
#define ARRAY_SIZE 100
using namespace std;
struct wordInfo
{
string wordList;
int count;
};
//Prototypes
bool openFile(ifstream &infile);
bool read1Word(ifstream &infile, string &temp1Word);
void readAllWords(wordInfo words[], int &numberWords);
void sort(wordInfo words[], int xNumberWords);
void zeroTheCounts(wordInfo words[]);
void menu(wordInfo words[] , int xNumberWords);
int biSearch(wordInfo words[], int xNumberWords, string finder);
void wordIndex(wordInfo words[], int xNumberWords);
int sumAllCounts(wordInfo words[], int xNumberWords);
int linSearch(wordInfo words[], int xNumberWords, string xTemp);
void wordSearch(wordInfo words[], int xNumberWords, string &find);
int main()
{
// Variables
wordInfo words[ARRAY_SIZE];
int numberWords;
readAllWords(words, numberWords); // Calls to read
sort(words, numberWords); // Calls to sort
menu(words, numberWords); // Calls the menu
return 0;
}
/**************************************************************************/
/* Function name: swap */
/* */
/* Description: swaps data in the file */
/* */
/* Parameters: int - x: one integer; int - y: another integer */
/* Return Value: none */
/**************************************************************************/
void swap(int x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
/**************************************************************************/
/* Function name: openFile */
/* */
/* Description: opens a selected file */
/* */
/* Parameters: ifstream - infile: allows for a file to be "read" in */
/* Return Value: true or false */
/**************************************************************************/
bool openFile(ifstream &infile)
{
string yourFile;
cout << "Enter the name of the file you wish to process: " << endl;
cin >> yourFile;
infile.open(yourFile.c_str());
if (infile.fail())
{
cout << "Error opening file: " << yourFile << endl; //something was wrong
return(false);
}
return(true);
}
/**************************************************************************/
/* Function name: read1Word */
/* */
/* Description: reads 1 word of the file */
/* */
/* Parameters: ifstream - infile: allows for a file to be "read" in
string - temp1Word: a temport word slot */
/* Return Value: true or false */
/**************************************************************************/
bool read1Word(ifstream &infile, string &temp1Word)
{
if (infile >> temp1Word)
return(true);
return(false);
}
/**************************************************************************/
/* Function name: readAllWords */
/* */
/* Description: reads all the words of the file */
/* */
/* Parameters: wordInfo - words[]: reads the whole wordInfo struct */
/* int - xNumberWords: a value to hold # of words read */
/* Return Value: none */
/**************************************************************************/
void readAllWords(wordInfo words[], int &xNumberWords)
{
ifstream infile;
string temp;
xNumberWords = 0;
if (openFile(infile) == false) //check to see if the file opened
exit(1);
zeroTheCounts(words);
while (xNumberWords < ARRAY_SIZE && read1Word(infile, temp))
{
int index = linSearch(words, xNumberWords, temp);
if (index == -1)
{
words[xNumberWords].wordList = temp;
words[xNumberWords].count = 1;
xNumberWords++;
}
else
words[index].count = words[index].count + 1;
}
//Outputs words that are unique
if (xNumberWords == 100)
cout << "Some words are not processed due to the fact that there were 100 unique words." << endl;
else
cout << " There currently are " << xNumberWords << " unique words detected." << endl;
infile.close();
return;
}
/**************************************************************************/
/* Function name: sort */
/* */
/* Description: sorts all the words of the wordInfo struct */
/* */
/* Parameters: wordInfo - words[]: reads the whole wordInfo struct */
/* int - xNumberWords: a value to hold # of words read */
/* Return Value: none */
/**************************************************************************/
void sort(wordInfo words[], int xNumberWords)
{
int loc;
int indexTop;
int index;
for (loc = xNumberWords - 1; loc > 0; loc --)
{
indexTop = loc;
for (index = 0; index < loc; index++)
if (words[index].wordList > words[indexTop].wordList)//remember to remove this);
indexTop = index;
if (indexTop != loc);
{
swap(words[indexTop].wordList, words[loc].wordList);// same here);
swap(words[indexTop].count , words[loc].count);//here too);
}
}
}
/**************************************************************************/
/* Function name: zeroTheCounts */
/* */
/* Description: zeros all the counts for the array/struct wordInfo */
/* */
/* Parameters: wordInfo - words[]: reads the whole wordInfo struct */
/* Return Value: none */
/**************************************************************************/
void zeroTheCounts(wordInfo words[])
{
for (int index = 0; index < ARRAY_SIZE; index++)
words[index].count = 0;
}
/**************************************************************************/
/* Function name: menu */
/* */
/* Description: allows the user to interact via a menu interface */
/* */
/* Parameters: wordInfo - words[]: reads the whole wordInfo struct */
/* int - xNumberWords: a value to hold # of words read */
/* Return Value: none */
/**************************************************************************/
void menu(wordInfo words[], int xNumberWords)
{
int choice;
int sum;
string findAWord;
while (choice != 4)
{
// This is the start of the menu
cout << endl;
cout << "Please select one of the following choices: " << endl;
cout << "1. Find a word by its index" << endl;
cout << "2. Search for a word in the file" << endl;
cout << "3. Output the number of words processed" << endl;
cout << "4. Exit" << endl;
cin >> choice;
// A switch statement
switch (choice)
{
case 1:
wordIndex(words, xNumberWords);
break;
case 2:
wordSearch(words, xNumberWords, findAWord);
break;
case 3:
sum = sumAllCounts(words, xNumberWords);
cout << "The total of words processed is: " << sum <<endl;
break;
case 4:
exit(1);
break;
default:
cout << "Error in choice selection! Please enter a numerical value listed on the menu." << endl;
}
}
} // ends the funtion
/**************************************************************************/
/* Function name: biSearch */
/* */
/* Description: performs a binary search on the data */
/* */
/* Parameters: wordInfo - words[]: reads the whole wordInfo struct */
/* int - xNumberWords: a value to hold # of words read */
/* string - finder: string that holds a var to find a word */
/* Return Value: none */
/*************************************************************
Narue 5,707 Bad Cop Team Colleague
>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.
bluejay_1111 0 Newbie Poster
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.
ithelp 757 Posting Virtuoso Banned
You are still writing C style code , put a class wordinfo having all of these as member functions.
Narue 5,707 Bad Cop Team Colleague
>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. :)
bluejay_1111 0 Newbie Poster
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.