Counting words

So if anyone out there can help. I have to write a program that count the words of same length from a file using an array to keep count of the iterations I guess. For example if numWords[3]= 3425, there are 3425 three letter words. Basically I do not even get how to do it. My thought of how to accomplish this is to first convert the whole file into the number of characters then somehow isolate the length of the words by somehow collecting them with a function of some sort and counting every time it finds the same length storing it in an array's element.

#include <iostream>
#include <cstring>
#include <fstream>
#include <vector> //supposedly vectors are better arrays
Using namespace std;

int main()
{
Vector<int>value(60) // I believe maximum length was 60
Ifstream Words;
Words.open("file name") // arbitrary name I used
string letters;

int num = 0, length;
while(getline(Words,letters))//loop to read words to letters
{
Length = Letters.length();//converted the words into # charcters
num++;// total number of words in case I'll need
}

//so this is what I have. I can't find anywhere online where A function will read the same number of characters in a file so I'm writing this to get some help. I'm pretty sure there must be a function out there where Itll read a specific length only and I'll tally it with another loop into the vector array.

Last edited on
My professor said we'll just need an array and a loop with functions as optional
I was wondering is it possible
Int number;
for(number = 1; number <= 60; number++)// loop that controls array
{
While(getline(words,letters))//loop to read to letters
{
length = letters.length();// convert to # characters
If(function that searches for the length of the words)
Value[number]++;
}
Cout <<"There are "<< value[number] << " with "<< number << "characters" << endl;
}
So I was thinking that the while inner loop will read through the file and everytime it hits first a one character word, it'll increment the value into the first element(for the first post I think it needs to be vector<int>value(61)={0} so that all 0-60 elements will have a value of 0). The while loop will read through the whole file before the next element is called on by the for loop and it'll continue until all lengths had been cycled.

Idk if it'll even work I can't even try it because I don't have a function to get the lengths. And there must be another way where I don't have to cycle through the file 60 times lol. I think I'll need something to control the while loop to? Or is it gonna loop until the last item on file?
Last edited on
Topic archived. No new replies allowed.