Tabulate and store length of words from .txt file

Hello guys,

I have a project which requires me to write a program that initially prompts the user for a filename. If the file is not found, an error message is output, and the program terminates. Otherwise, the program produces output as such:

File: filename goes here Words: 81
Analysis of Words
Size 1 2 3 4 5 6 7 8 9 10+
#Words 7 12 14 24 9 8 4 1 2 0

So basically, I want it to read in the data from a text file, recording the length of each word and storing the values into an array to be outputted like the example above.

This is what I have thus far,

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream inf;
string filename;
// Prompt User
cout << "Enter Filename: ";
cin >> filename;
inf.open(filename.c_str());
// Check if open failed
if (inf.fail())
cout << "Error: No File" <<endl;
string word;
// Output words and their length
while (inf >> word)
cout << word << " " << word.length() <<endl;
return (0);
}

Then, I have an idea of how to start the array to store values, but not sure where to go next. This is what I have for that:

words[]
inf >> word
for (int x = 0, x < 9, x++)
{
if(word.length > 9)
words[0]++
}

I will greatly appreciate any input/advice and a forewarning, I'm a novice with c++ so limit functions to "basics."

Thanks!
Last edited on
Well, you have almost everything you need.

you will need the place to store amount of words:
int word_amount[10] = {0}; //Initialize all word amounts to 0 initially

then you would read word file >> word and then increment amount of words with desired length. A special handling is needed for words with length > 10: we want them to count with 10 symbols long ones:
1
2
int length = std::min( word.size(), 10 ); //If size is > 10, set it to 10
word_amount[length - 1] += 1;

Still a little stuck even with this information you gave me. Not quite sure where to put those functions into the program and also some sort of loop in the array that goes through each word, storing the value of length each time before moving on. Should I be using any void functions for this or can it all be done in main?
Should I be using any void functions for this or can it all be done in main?
it certainly can be done in main(). Here I changed your code slightly and annotated places which need work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>

int main()
{
    std::string filename;
    std::cout << "Enter filename: ";
    std::cin >> filename;
    std::ifstream inf(filename);
    if ( !inf) {
        std::cout << "Error: No File\n";
        return 1;
    }
    //You will need to setup structure (array) for
    //holding amount of words somewhere before loop
    std::string word;
    while (inf >> word) {
        std::cout << word << ' ' << word.size() << '\n'; //Remove this later
        //Here you need to check length of the world
        //and increment corresponding array element
    }
    //Here all your output goes
}
Topic archived. No new replies allowed.