Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Counting word occurrences in text file - won't output or write to screen C++. When I have the output and write functions inside my count

Counting word occurrences in text file - won't output or write to screen C++. When I have the output and write functions inside my count function it DOES output and write to the file. However when they are in their own functions my words variable doesn't get passed over and it gets thrown, I'm not sure why. Another issue is that for every word that is matched, a garbage value gets created which you can see below (I tested this by keeping the output and writing inside the count function). I'm still new to programming and any advice would be helpful, I'm sure there is an easier way to do this but dynamic memory has been a confusing concept for me. Thank you!

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include

using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream; using std::setw; using std::right; using std::left;

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

void 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 = nullptr;

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

input.close(); return 0; }

// read words from text file void readWords(int& entries, char** words, int* count) { ifstream input(DATA_IN); char* str = new char[5000]; char* chptr; entries = 0; char buffer[256]; words = new char* [5000];

if (input.is_open()) // if the file is open { while (!input.eof()) // while it is not end of file { input.getline(str, 500, ' '); chptr = strtok(str, " ");

while (chptr != nullptr) { words[entries] = new char(strlen(chptr) + 1); words[entries] = chptr; entries++; // move on to the next word chptr = strtok(nullptr, " "); } } countWords(entries, words, count, buffer); // call process function } delete[] str; }

// 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]; // allocating count bool isThere = false; int increment = 0; char** words2 = new char* [500]; int* wordCount = new int[500]; char** temp = new char* [entries];

// setting elements in words2 array to null for (int i = 0; i

for (int i = 0; i

if (!isThere) // Word not found in new array -- Populating new array with the word. { words2[increment] = temp[i]; // word being added to new array wordCount[increment] = count[i]; // parallel count being added to new parallel count array increment++; // increment so both arrays are parallel to one another } } // gets rid of duplicates for (int i = 0; i

delete[] words; delete[] words2; }

//write list of words to file void writeFile(int& entries, char** words, int* count) { // write function (won't currently work outside of countWords) ofstream output; output.open(DATA_OUT);

if (output.is_open()) { // for each entry, write to the screen // currently for each match it is creating a null value? for (int i = 0; i

}

// output word frequency occurence to screen void outputFile(int& entries, char** words, int* count) { // print function (won't currently work outside of countWords) cout

image text in transcribed

Word Frequency Analysis Word Frequency The definition of insanity is doing same thing over and expecting different results 02222 02222 2222 -842150451 -842150451 -842150451

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

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

b. What groups were most represented? Why do you think this is so?

Answered: 1 week ago

Question

3. Describe phases of minority identity development.

Answered: 1 week ago

Question

5. Identify and describe nine social and cultural identities.

Answered: 1 week ago