Question
Create a program that reads a text file filled with words (for example animal names) and counts the number of occurrences of its constituent tokens
Create a program that reads a text file filled with words (for example animal names) and counts the number of occurrences of its constituent tokens using a sequential container object iterator. Use "WordData.h" and "Worddata.cpp" in your program.
For testing purposes to create 2 text files. One is filled with max 10 words (some of them can be duplicates), and the other is filled with 10 unique words and multiple repetitions.
I already have some code. I need help with the rest. Open to suggestions for fixing the code. #include
int main() { //Declaring all variables that will be used within the main function string fileName =""; //name of the input file string fileContents=""; //contents within the input file
////Start of the program cout<<"This program will count the number of words within your selected file, using the parallel iterative arrays."< //Promting the user for the name of the file cout<<"Please enter the name of the file: "< //function to find the input file ifstream MyReadFile(fileName); // Using a while loop together with the getline() function to read the file line by line, and input it into fileContents while (getline (MyReadFile, fileContents)) { //Output the text from the file - This section is to be deleted when done, just a process to make sure that the program reads the contents of the file. cout << "Your files contents: "< // Here call the separate function (or subfunction) that will perform the operation from the given problem. } Name of the file: test1.txt Contents: Dog Cat Cow Pig Dog Cat Cow Cat Cow Pig Name of the file: test2.txt Contents: Cat Cow Pig Alpaca Hamster Dog Fish Rat Zebra Lion Cat Cow Pig Alpaca Rat Zebra Lion Cow Pig Alpaca Hamster Dog Fish Name of the File: WordData.cpp #include using namespace std; WordData::WordData(string wrd, int cnt) { setWordData(wrd, cnt); } void WordData::setWord(string wrd) { word = wrd; } void WordData::setCount(int cnt) { count = cnt; } void WordData::setWordData(string wrd, int cnt) { setWord(wrd); setCount(cnt); } string WordData::getWord() const { return(word); } int WordData::getCount() const { return(count); } WordData& WordData::operator++() // preincrement { setCount(getCount()+1); return(*this); } WordData WordData::operator++(int) // postincrement { WordData temp; setCount(getCount()+1); return(temp); } Filename: WordData.h Purpose: This simple object is designed to hold a string and an int. The string is meant to hold a word and the int is meant to hold the number of occurences of the word in a file. The int and the string can be retrieved and set both on their own as well as at the same time. The object also has the ability to increment the word's counter. #ifndef WORDDATA_H #define WORDDATA_H #include using namespace std; class WordData { public: //PUBLIC FUNCTIONS /* Function name: constructor */ /* Description: Will construct a default word data object.*/ /* Default values are an empty string zero as*/ /* the count. */ /* Parameters: string wrd - Word to be placed in object */ /* int cnt - Value to be placed in counter */ /* Member Type: */ /* Return Value: none */ WordData(string wrd = "", int cnt = 0); //SETS // /* Function name: setWord */ /* Description: Will set the object's string. */ /* Parameters: string wrd - Word to be placed in object */ /* Member Type: */ /* Return Value: none */ void setWord(string wrd); /* Function name: setCount */ /* Description: Will set the occurence counter. */ /* Parameters: int cnt - Value to be placed in counter */ /* Member Type: */ /* Return Value: none */ void setCount(int cnt); /* Function name: setWordData */ /* Description: Will set both the object's string and the */ /* object's occurence count. */ /* Parameters: string wrd - Word to be placed in object */ /* int cnt - Value to be placed in counter */ /* Member Type: */ /* Return Value: none */ void setWordData(string,int); //GETS // /* Function name: getWord */ /* Description: Will retrieve the object's string. */ /* Parameters: none */ /* Member Type: */ /* Return Value: string */ string getWord() const; /* Function name: getCount */ /* Description: Will retrieve the occurence counter. */ /* Parameters: none */ /* Member Type: */ /* Return Value: int */ int getCount() const; /* Function name: operator ++ */ /* Description: Will increment the occurrence counter. */ /* Parameters: int inc - Value to be incremented by */ /* Member Type: */ /* Return Value: none */ WordData& operator++(); // preincrement WordData operator++(int); // postincrement private: //PRIVATE VARIABLES string word; //Object's word int count; //Object's occurrence counter }; /* Function name: operator << */ /* Description: Associated stream-insertion operator */ /* Parameters: ostream &output - output stream */ /* const WordData &word - object to print */ /* Return Value: none */ ostream &operator<< ( ostream &output, const WordData &word); #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started