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;
}