counting number of lines in a given file

i am suppose to write a program where it asks a user to input a text file location and then counts the number of lines in this file. output may look as:
1
2
enter a text file:c:/data/file.txt
c:/data/file.txt contains 478 lines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
	string s,t;
	int count=0;
	cout<<"enter a text file :";
	cin>>s;
	ifstream inFile(s);
	while(!inFile.eof())
	{
		getline(inFile,t);
		count++;
	}
	cout<<s<<"contains "<<count<<"lines";
}


problem is when i enter a location nothing happens.
ok i figured it out. the problem is with 11th line it is supposed to be:
ifstream inFile(s.c_str());
this will probably help:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

//
//

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

typedef unsigned int uint32;

class LineProcessor
{
public:
    //=================================================================
    LineProcessor() : total(0),relevant(0),words(0),characters(0) {}
    ~LineProcessor() {}
    
    //=================================================================
    void AddLine(std::string line) {
        if(line.empty()) {
            ++total;
        } else {
            ++total;
            for(std::string::size_type i = 0; i < line.length(); ++i) {
                if(isalnum(line.at(i))) {
                    ++characters;
                }
            }
            std::vector<std::string> delimit = Delimit(line);
            words += delimit.size();
            if(delimit.size() > 0) { ++relevant; }
        }
    }
    //=================================================================
    friend std::ostream& operator<<(std::ostream& out, LineProcessor& lp)
    {
        lp.Print(out);
        return out;
    }
    //=================================================================
    std::vector<std::string> Delimit(std::string line) {
        std::vector<std::string> dl;
        std::string token;
        std::istringstream iss(line);
        while( getline(iss,token,' ') ) {
            if( !token.empty() ) { dl.push_back(token); }
        }
        return dl;
    }
    //=================================================================
    void Print(std::ostream& out) {
        out << "Total lines: " << total;
        out << ", Non-empty lines: " << relevant;
        out << ", Words: " << words;
        out << ", Characters: " << characters;
    }
    //=================================================================
private:
    uint32 total;
    uint32 relevant;
    uint32 words;
    uint32 characters;
    
};



int main(int argc, char** argv)
{
    std::string fname;
    
    if(argc == 1) {
        std::cout << "\n\tPlease enter a filename: ";
        std::cin >> fname;
    } else {
        fname = std::string(argv[1]);
    }
    
    std::ifstream inFile(fname.c_str());
    std::string line;

    LineProcessor* lineprocessor = new LineProcessor;
    
    std::vector<uint32> counts(3,0);
    while(!inFile.eof()) {
        getline(inFile,line,'\n');
        lineprocessor->AddLine(line);
    }
    
    std::cout << "\n\t" << *lineprocessor << "\n" << std::endl;
    
    return 0;
}


Topic archived. No new replies allowed.