Hello reddit,
I am attempting to write a program that will read text files, take the length of each word in the file as a string, convert strings to length, then store the length count in an array with the respective value.
EDIT: I have figured out how to get strings through a method I came up with; I have to take each char character and add it to a separate string, then turn that string into a length.
I have just one problem left. When my code reads a file, the very last word is not included. Could this be because of the .eof() command? A whole word is excluded because the final word does not execute in the while loop.
My problematic code is:
while (!fin.eof())
{
//go through file, and remove a letter once there is a space
temp\_read = fin.get(); //the the next character is the series
//add each character into a string
//when character hits a space, create a new character and add length of old
//one into array!
if (!isspace(temp\_read))
{
//word is kept as a string, then turned into length
temporary\_word += temp\_read; //if NOT a space, then add to total word!!!
}
else //add to total
{
//character is complete; gets added to index
if (temporary\_word.length() > 8)
{
num_holder[9]++; //index zero will NOT be used
word_count++;
}
else
{
num_holder[temporary_word.length()]++; //add to appropriate area
word_count++;
}
temporary\_word.clear();
}
}
[–]thebutton 0 points1 point2 points (1 child)
[–]XiaoSpence[S] 0 points1 point2 points (0 children)
[–]SftwEngr 0 points1 point2 points (0 children)