Complexicator Complexicator - 1 year ago 201
C++ Question

Storing and outputting word.length() into an array

I have been at this for a few hours and I am having difficulty reading into my text file, counting how many letters each word has, the amount of words for every amount of letters.

I have come up with this, thus far:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;

const int array_size = 29;

int main() {

ifstream inputfile;
string word, word2;
int wordlength[array_size];
int length = 0;

cout << left << setw(10) << "Length: ";
cout << left << setw(10) << "# of words: " << endl;"C:/EnglishWords.txt");

while (inputfile) {

inputfile >> word;

int len = word.length();
wordlength[len]++; //initialized array for '29'

for (int i = 1; i < 29; i++) {
cout << left << setw(10) << wordlength[i];
cout << left << setw(10) << i;


return 0;

I am essentially getting variations of -8293729 (I am assuming this is garbage memory) for each actual value that I want printed. I could really use the power of stackoverflow on this one because I am stumped :/.

EDIT: My file that I am reading from is a list of "all" the English words separated by /n;

Answer Source

First of all, your wordlentgth array is not initialized. Try using a for loop to set its content to 0 before incrementing it. Or, better yet, use memset

int wordlength[array_size];
memset(wordlength, 0, array_size);

EDIT: int wordlength[array_size] = {0}; is the way to go in this case. memset is useful when you have to re-set an array for example.

You will need to #include <cstring> in order to use it.

Second, if any of the word is greater than array_size your program will crash because of a segmentation fault (you should look it up, it will be the most frequent error you will encounter if you program in C/C++). In order to avoid this bug, just make sure that len is lesser than array_size before incrementing wordlength[len] by wrapping the increment in an if:

int len = word.length(); 
if(len < array_size) {
} else {
    cerr << "A word was ignored because it was too long: \"" << word << "\"\n";

Lastly, you should read a little bit about naming conventions. It is a matter of preference really, but just try to be consistent (i.e. wordlength doesn't follow the same convention that array_size). The way you wrote array_size is called snake-case and I personally like it, but the mainstream style in the C family of language is CamelCase. Another note about style: it is ok to use global constants, but it is really recommended to name it so it is clear that it is a constant: ARRAY_SIZE instead of array_size.

Also, indent your code correctly. Better yet, use an editor that can auto-indent your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download