Trying to count 4, 5, and 6 letter words.

Hello, I'm trying to write code to count 4, 5, and 6 letter words, within a file. I've spent around 20 hours on a couple of assignments and exausted help from my computer lab as well ;). Not trying to get someone to write the code for me but if you could help me understand why mine isn't currently working I'd appreciate it.

//This program counts all 3, 4, 5, and 6 letter words from a file named QUOTES.TXT.

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

int main()

ifstream FileIn;
ofstream FileOut;
int count=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;
char inchar;"E:\\QUOTES.txt");"E:\\NUMWORDS.txt");

while (inchar>='A'&&inchar<='Z' || inchar>='a'&&inchar<='z')

if (count==3 )

if (count==4)
if (count ==5 )
count5++ ;
if (count ==6 )

cout << "3 letter words " << count3 << endl;
cout << "4 letter words " << count4 << endl;
cout << "5 letter words " << count5 << endl;
cout << "6 letter words " << count6 << endl;

FileOut << "3 letter words " << count3 << endl;
FileOut << "4 letter words " << count4 << endl;
FileOut << "5 letter words " << count5 << endl;
FileOut << "6 letter words " << count6 << endl;



The results I get are

3 letter words 0
4 letter words 0
5 letter words 0
6 letter words 0
Last edited on
I'm guessing that the input file contains something like a sentence with various words. Your code right now seems like it will stop reading from the input file after it gets to the first non-letter character, which is not what you want. You need to use a second while loop outside your current one that will execute your first while loop and your if statements until the end of file is reached.

Do you know how to use strings? While your method can work, doing it with strings woiuld be much simpler.
Ty for your help; I found some help though as I should have done before by searching. I feel stupid now for asking when there was already some good responses about this same type of assignment.
Topic archived. No new replies allowed.