Program to count characters, words and lines

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

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include<iostream>
#include<fstream>
#include<string>
#include<conio.h>

using namespace 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++;
		}
		else if(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;
}
Topic archived. No new replies allowed.