Alright, I am trying to count words in a file. The user running the program can choose a file that the program can count how many words are in the file. I have it working, but.... it keeps looping the last sentence

cout<<"There are "<<words<<" words."<<endl;

over and over and over while the word is just repeating 0. so its not counting. i also know that the loop is not working. it should stop after it reads the whole file?
any help would be awesome.

Here is my code:

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

int main()
{
	int words = 0, count = 0, letters = 0;
	int ch = 0;
	int Option1;
	string FILE;
cout <<"Hello, This program will count the number of words in a file of your choice!"<<endl; 
cout <<"If you are ready to begin enter 1 and press enter, press 2 and enter to exit."<<endl;
cin >> Option1;
if(Option1==2)
{
cout <<"Have a nice day"<<endl;
cout <<endl;
system("pause");
return 0;
}
if(Option1==1)
{
cout <<"Alrighty lets begin, Please enter the name of the file you wish to open"<<endl;
cout <<"Also include the file format! for example(.txt, .doc, .rtf ect...)"<<endl;
cin >> FILE;

ifstream in;
in.open(FILE);

	if(in.fail())
	{
		cout << "input file failed to open."<<endl;
		exit(1);
	}
	while(!in.eof())
	{
	in>>ch;
	if((ch==' ')||(ch=='.')||(ch=='\n'))
	{
	letters++;
	words++;
	letters=0;
	}
	
	cout<<endl;
	cout<<"There are "<<words<<" words."<<endl;
	}
}

system("pause");
return 0;


}

You need to move the final word count out of the while() loop. You also need to check for in.fail() in the loop itself, in case there's a problem with the input that did not appear when it was opened.
EDIT: I think I know where the problem lies: you need to use in.get() rather than the << operator to read in the characters. Here's a modified version of your code that ought to work correctly:

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>

using namespace std;

int main()
{
    int words = 0, count = 0, letters = 0;
    int ch = 0;
    int Option1;
    string FILE;
    cout <<"Hello, This program will count the number of words in a file of your choice!"<<endl;
    cout <<"If you are ready to begin enter 1 and press enter, press 2 and enter to exit."<<endl;
    cin >> Option1;
    if(Option1==2)
    {
        cout <<"Have a nice day"<<endl;
        cout <<endl;
        system("pause");
        return 0;
    }
    if(Option1==1)
    {
        cout <<"Alrighty lets begin, Please enter the name of the file you wish to open"<<endl;
        cout <<"Also include the file format! for example(.txt, .doc, .rtf ect...)"<<endl;
        cin >> FILE;

        ifstream in;
        in.open(FILE.c_str());

        if(in.fail())
        {
            cout << "input file failed to open." << endl;
            exit(1);
        }
        while(!in.eof() && !in.fail())
        {
            ch = in.get();
            // cout.put(ch);
            letters++;

            if((ch=='.') || isspace(ch))
            {
                words++;
            }
        }

        cout << endl;
        cout << "There are " << letters << " characters and" << endl;
        cout << "There are " << words << " words." << endl;
    }

    system("pause");
    return 0;
}

Edited 5 Years Ago by Schol-R-LEA: n/a

Why are you reading an int (ch) and testing as a character? It should be defined as a char.

What does letters do in your code if you increment it then immediately set it to 0?

Also see this ( feof() is identical to .eof() )
and this
and this

EDIT: I think I know where the problem lies: you need to use in.get() rather than the << operator to read in the characters. Here's a modified version of your code that ought to work correctly:

Come on, you know better than this...
And you are also wrong. >> works just fine.

Edited 5 Years Ago by WaltP: n/a

Comments
I completely missed that! Thank you, that explains a lot.

Alright I got it working perfectly!, Thanks so much, I thought i was missing a few steps to it. I moved

cout << "There are " << words << " words." << endl;

out of the while loop so it does keep repeating until all of the words have been read just comes up a solid line.

and WaltP I dont know why i had CH as an int. just a mistake changed it to a char. thanks.

thank you Schoil-R-LEA and WaltP

This question has already been answered. Start a new discussion instead.