Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ counting word occurence in a text file. I have everything written it just isn't working properly. We just learned dynamic memory so it's a

C++ counting word occurence in a text file. I have everything written it just isn't working properly. We just learned dynamic memory so it's a bit confusing for me still. I have it to where I read in one word from the text file, call my countWords function, then increment entries to move on to the next word (we aren't allowed to do any processing in the input function). When using the debugger, the word being held in temp gets copied over to my words variable but it won't enter the loop to compare two words, it breaks at the _stricmp condition. Can anyone please explain why it is not currently working and/or point me in the right direction? Thank you so much!

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include

using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream;

const char DATA_IN[] = "data.txt"; const char DATA_OUT[] = "report.txt";

int readWords(int& entries, char**& words, int*& count); void writeFile(int entries, char**& words, int*& count); void outputFile(int entries, char**& words, int*& count); void countWords(int entries, char**& words, int*& count, char* buffer);

int main() { ifstream input(DATA_IN); ofstream output(DATA_OUT);

int entries = 0; char** words = nullptr; int* count;

if (input.is_open()) { // load the file readWords(entries, words, count);

// write to the file writeFile(entries, words, count);

// output to the screen outputFile(entries, words, count); } else cout << "Error opening file!";

input.close(); return 0; }

// read words from text file int readWords(int& entries, char**& words, int*& count) { ifstream input(DATA_IN);

entries = 0; words = nullptr; char** temp = nullptr; char buffer[256];

if (input.is_open()) // if the file is open { while (!input.eof()) // while it is not end of file { input >> buffer; // input 1 word // call process function countWords(entries, words, count, buffer); entries++; // move on to next word

} } return entries; // return entries } // count how many times a word is found in the file, one at a time use stricmp void countWords(int entries, char**& words, int*& count, char* buffer) { count = new int [entries]; char** temp = nullptr;

int i, j;

temp = new char* [entries + 1]; // allocate memory for temp + null for (int i = 0; i < entries; i++) { temp[i] = words[i]; // copy words to temp }

temp[entries] = new char[strlen(buffer) + 1]; // set buffer length + 1 for null char

strcpy(temp[entries], buffer); // copy buffer into temp variable delete[] words; words = temp; // copy value in temp to words

// find word for (int i = 0; i < entries; i++) { for (int j = 0; j < entries; j++) { // if two words are found to be equal if (_stricmp(words[i], words[j]) == 0) { // increment count count[entries]++; } else { // add word words = new char* [entries]; // add count of 1 count = new int[entries + 1]; } } } }

//write list of words to file void writeFile(int entries, char**& words, int*& count) { ofstream output(DATA_OUT);

// making sure the file is open if (output.is_open()) { // for each entry, write to the output for (int i = 0; i < entries; i++) { output << words[i] << endl; }

} output.close(); }

// output word frequency occurence to screen void outputFile(int entries, char**& words, int*& count) { // header cout << "Word Frequency Analysis" << endl; cout << "Word\t" << "Frequency" << endl;

// for each entry, print out words and word frequency for (int i = 0; i < entries; i++) { cout << words[i] << '\t' << count[i] << endl; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions