I have a program that is written here that is not providing accurate counts on lines and words. I haven't counted the characters but see no reason to believe that it is accurate. I am a noob to C++ so I am hoping for some guidance from the guru's in Cplusplus land. Thanks
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
usingnamespace std;
// start of main program
int main()
{
string filename;
cout<<"Please enter the data file name: ";
// reads the filename from the user
cin>>filename;
// open the file for input.
ifstream infile(filename, std::ifstream::in);
// create the stream in read-only mode
if(!infile) {
cout << "Cannot open file for reading.\n";
_getch();
return 1;
}
// declares character and integer variables
char ch,c;
int count=1;
int i=0;
int count1=1;
// running the loop until file will end
while(infile.get(ch))
{
cout<<ch;
if(ch==' ')
{
count++;
count1++;
}
elseif(ch==' ')
count1++;
i=i+1;
}
cout<<"";
// display the number of character, words and line
cout<<"\nNumber of characters: "<<i-(count-1+count1-1)<<"";
cout<<"\nNumber of words: "<<count1<<"";
cout<<"\nNumber of lines: "<<count<<"";
// closes the file
infile.close();
_getch();
return 0;
}