classes and files

the following code needs to display the number of characters, lines and spaces in a file. Can you tell me what is the way to put what's in the main() function ( the while loop and if statement ) in the corresponding function definitions. For example the if (character == ' ') {numberOfSpaces += 1; } in GetSpaceCount()function. I hope you understand what I'm trying to say.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class FileAnalyzer {

public:
	

	FileAnalyzer(string name, int numberOfCharacters, int numberOfSpaces, int numberOfLines)
	{
		this->name = name;
		this->numberOfCharacters = numberOfCharacters;
		this->numberOfSpaces = numberOfSpaces;
		this->numberOfLines = numberOfLines;
	}
	

	int GetCharacterCount()
	{
		return numberOfCharacters;
	}

	int GetSpaceCount()
	{
		return numberOfSpaces;
	}

	int GetLineCount()
	{
		return numberOfLines;
	}

	void displayGetCharacterCount(){
		cout << "number of characters is: " << numberOfCharacters << endl;
	}
	
	void displayGetSpaceCount(){
		cout << "number of spaces is: " << numberOfSpaces << endl;
	}
	
	void displayGetLineCount(){
		cout << " number of lines is: " << numberOfLines << endl;
	}
	
	
private:
	string name;
	int numberOfCharacters;
	int numberOfSpaces;
	int numberOfLines;
};

int main()
{
	ifstream input;
	input.open("textdoc.txt");
	char character;
	int numberOfCharacters = 0;
	int numberOfSpaces = 0;
	int numberOfLines = 0;
	input.get(character);
	while (!input.eof())
	{
		input.get(character);
		numberOfCharacters +=1;
		if (character == ' ') {
			numberOfSpaces += 1;
		}
		else if (character == '\n') {
			numberOfLines += 1;
		}
	}

	FileAnalyzer characters("textdoc.txt", numberOfCharacters, numberOfSpaces, numberOfLines);
	characters.displayGetCharacterCount();
	
	FileAnalyzer spaces("textdoc.txt", numberOfCharacters, numberOfSpaces, numberOfLines);
	characters.displayGetSpaceCount();
	
	FileAnalyzer lines("textdoc.txt", numberOfCharacters, numberOfSpaces, numberOfLines);
	lines.displayGetLineCount();
	
	
	return 0;

}



Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
#include <locale>

struct text_file_analyser
{
    explicit text_file_analyser( std::string name ) : name(name) {}

    std::string name ;
    std::streamsize num_chars = 0 ;
    std::streamsize num_spaces = 0 ;
    std::streamsize num_new_lines = 0 ;
    char last_char_read = 0 ;

    std::streamsize num_lines( bool new_line_is_a_separator = true ) const
    {
        // C++ like: new line is a separator
        // (a new line is not required at the end of the last line of a source file)
        if( new_line_is_a_separator ) return num_new_lines + 1 ;

        // not C++-like: new line is a terminator:
        // we need to decide what to do if the last line is not terminated
        else if( last_char_read == '\n' ) return num_new_lines ; // last line is terminated
        else return num_new_lines + 1 ;	// we add one for the un-terminated last line
    }
};

text_file_analyser analyse( std::string file_name )
{
    text_file_analyser tfa( file_name ) ;

    if( std::ifstream file{file_name} )
    {
        const auto& locale = file.getloc() ;
        char c ;
        while( file.get(c) )
        {
            tfa.last_char_read = c ;
            ++tfa.num_chars ; // note that new line is counted as a single character

            // note that in the following code, new lines are counted as (white) spaces
            // to change the behaviour, make the second if an else if (uncomment the else)
            if( c == '\n' ) ++tfa.num_new_lines ;
            /*else*/ if( std::isspace( c, locale ) ) ++tfa.num_spaces ;
        }
    }

    return tfa ;
}

int main()
{
    const std::string file_name = __FILE__ ;
    const auto tfa = analyse(file_name) ;

    std::cout << " file name: '" << tfa.name << "'\n"
              << "    #chars: " << tfa.num_chars << '\n'
              << "   #spaces: " << tfa.num_spaces << '\n'
              << "#new lines: " << tfa.num_new_lines << '\n'
              << "    #lines: " << tfa.num_lines() << " (C++ like) / "
              << tfa.num_lines(false) << " (not C++ like)\n" ;
}

http://coliru.stacked-crooked.com/a/5c0290626c326ddd
http://rextester.com/SHJFDU91917
Thank you but the task is to use a class and the functions GetCharacterCount(), GetSpaceCount(), GetLineCount().
the task is to use a class

A ‘struct’ is a class where members are public by default. In JLBorges’s code you could just turn the word ‘struct’ into ‘class’ and than add a ‘public:’ as first statement.

...and the functions GetCharacterCount(), GetSpaceCount(), GetLineCount()

Just add them. Move “analyse()” inside the class, tweak it, and define those new three functions. They just need to return “num_chars”, “num_spaces” and “num_new_lines”.

If you decided to stuck to your code, have a look at it: you’ve declared a class, but you hardly use it.
If you want to use it, there should be at least three functions which increment number-of-characters, number-of-spaces and number-of-lines.
Ex. void increaseCharCount() { ++number_of_characters; }
To invoke them, you’d need an instance of the class
FileAnalyzer fa;
to be used more or less this way:
fa.increaseCharCount();

Anyway, that could be a good starting point, but it’s basic design.
If you call your class “FileAnalyzer”, well, it is supposed to be the one which analyse the file, isn’t it? So it should carry out all the hard tasks (I mean, open the file, get through, count the characters...), while main() should do nearly nothing, apart from ‘prompting’ FileAnalyzer.

In case you’re stuck, ask again.
Happy coding!
Topic archived. No new replies allowed.