So I'm trying to count the number of words in a file in such a way that multiple spaces or breakliines will not effect my answer. The goal of the loop being to test each line of the file, find whitespace or a breakline after a word (any grouping of non whitespace characters) and when that happens adding to a wordcounter. however, for the file
This &%file should!!,...
have exactly 7 words.
for example, I get a wordcount of 5... all of my files were off by between 1-3 words, and I can't find any correlations between the text and the error margin.
are my parameters not logical?
edit: okay, fixed that problem, changed if((letter ==' ' || letter == '/n') to if((letter ==' ' || position== Length) where Length is InputString.length -1.
It now outputs the proper number
it refuses to loop however, so I can only check 1 file at a time, but I haven't given that issue much thought yet myself. I'll probably be able to fix that issue on my own, thanks Yanson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
do
{
cout << "enter filename you wish to count the words in "
<< "(file1.txt, file2.txt, file3.txt, file4.txt, or file5.txt) type quit to exit: ";
cin >> FileName;
if (FileName == "quit")
break;
inFile.open(FileName.c_str());
if (!inFile)
{
cout << "Error oppening file" << endl;
return 1;
}
while(!inFile.eof())
{
getline(inFile, InputString);
for(int position=0; position<InputString.length(); position++)
{
letter=InputString.at(position);
if(letter!=' ')
{
letterBool = true;
}
if((letter ==' ' || letter == '/n') && letterBool == true)
{
letterBool = false;
WordCount++;
}
}
letterBool = false;
}
if (FileName != "quit")
{
cout << WordCount << endl;
}
FileName.clear();
}
while(true);
|