0

I (a student whose professor encourages online research to complete projects) have an assignment where I have to analyze the contents of a file (frequency of certain words, total word cout, largest and smallest word) and I'm getting stuck on even opening the file so the program can get words out. I've tried to just count the words that it reads and i get nothing. As I understand it, the program should be opening the selected .txt file, going through its contents word by word and outputing it right now.

Here's code:

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>

    string selected[100];
    //open selected file.
    ifstream file;
    file.open(story.c_str());
    string line;
    if (!file.good())
    {
        cout << "Problem with file!" << endl;
        return 1;
    }
    while (!file.eof())
    {
        getline(file, line);

        if (line.empty())
            continue;

        istringstream iss(line);

        for (string word; iss >> word;)
            cout << word << endl;

    ```




  • 1
    It's undefined behavior to call `main()` – drescherjm Dec 15 '19 at 15:03
  • There was a misunderstanding between you and the one asking to minimise the code. Please read and apply this concept: [mre] – Yunnosch Dec 15 '19 at 15:33
  • I recommend to simplify the code (which does not contradict the MRE idea above) to the point where it ONLY tries to read from a text file. That way you can avoid all the unknowns related to calling `main()`. You are aware that `main()` is the function which always begins the program, aren't you? So try to get `main()` read a word from the file, then go on making things more complex. – Yunnosch Dec 15 '19 at 15:36
  • The above code is not a complete program. By contrast, most "hello world" programs are complete, though small, programs. You may wish to start by getting a simple program working, then add capability getting you closer to where you need to be until something breaks - then ask for help. – theMayer Dec 15 '19 at 16:16
  • I guess it wasnt even opening the .txt because it couldnt access the file itself? (fixed that bit.) Now it's print out the file word by word (progress) but i have no idea how to assign it to an array 100 words at a time so i can use other bits of code to analyze it. (i shouldnt need much more explained past getting it into arrays, and i appreciate the help so far!) – jamie bellinger Dec 15 '19 at 16:21

1 Answers1

0

Because of the simplicity of the attached code, I will not give detailed explanations here. With the usage of std::algorithm every task can be performed in a one-liner.

We will read the complete source file into one std::string. Then, we define a std::vector and fill it with all words. The words are defined by an ultra simple regex.

The frequency is counted with a standard approach using std::map.

#include <fstream>
#include <string>
#include <iterator>
#include <vector>
#include <regex>
#include <iostream>
#include <algorithm>
#include <map>

// A word is something consiting of 1 or more letters
std::regex patternForWord{R"((\w+))"};

int main() {

    // Open file and check, if it could be opened
    if (std::ifstream sampleFile{ "r:\\sample.txt" }; sampleFile) {

        // Read the complete File into a std::string
        std::string wholeFile(std::istreambuf_iterator<char>(sampleFile), {});

        // Put all words from the whole file into a vector
        std::vector<std::string> words(std::sregex_token_iterator(wholeFile.begin(), wholeFile.end(), patternForWord, 1), {});

        // Get the longest and shortest word
        const auto [min, max] = std::minmax_element(words.begin(), words.end(),
            [](const std::string & s1, const std::string & s2) { return s1.size() < s2.size(); });

        // Count the frequency of words
        std::map<std::string, size_t> wordFrequency{};
        for (const std::string& word : words) wordFrequency[word]++;

        // Show the result to the user
        std::cout << "\nNumber of words: " <<  words.size() 
            << "\nLongest word: " << *max << "  (" << max->size() << ")"
            << "\nShortest word: " << *min << "  (" << min->size() << ")"
            << "\nWord frequencies:\n";
        for (const auto& [word, count] : wordFrequency) std::cout << word << " --> " << count << "\n";

    }
    else {
        std::cerr << "*** Error:  Problem with input file\n\n";
    }
    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44