counter character,word & line

Write your question here.
CODE FOR COUNTING CHARACTER ,WORD AND LINES FROM FILE BY USER GIVEN TXT FILE
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
 
#include<fstream>
#include<iostream>
#include<string>
using namespace std;

int main()
{
    ifstream inFile; //Declares a file stream object
    string fileName;
    string word;
    int size=0;
    int count = 0;
    char a;
    int lines=1;

    cout << "Please enter the file name ";
    getline(cin,fileName);

    inFile.open(fileName.c_str());

    while(!inFile.eof())
    {               
        inFile >> word; 
        size+=word.length();
        count++;
    }
 
 	
   inFile.seekg (0);
    while (inFile.good ())
    {
        a=inFile.get ();
        if (a=='\n')
        {
            lines++;
        }
    }
    cout << "Number of words in file is " << count ;
    cout << "\n";
    cout << "The total number of characters entered is: " << size ;
    cout << "\n";
    cout << "In file is "<< lines<<" lines";
    inFile.close();

    cin.get();  
    return 0;
    
}
Topic archived. No new replies allowed.