character frequency class

Hi,

I need to make a class that counts the ASCII characters from a file, and outputs the count of each character. I am having trouble figuring out the function definitions for each class method. This is what I have so far, and I probably did the definitions wrong. I just would like some suggestions on how to make this work.

Thank you.


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

using namespace std;

class charFrequency
{
public:
	char getCharacter();
	void setCharacter(char character);
	long getCount();
	void setCount(long count);
	void increment();

private:
	char character;
	long count;
};


int main()
{
	charFrequency myCount;						
	ifstream infile;			//input file stream variable
	ofstream outfile;			//output file stream variable

		// check for file
	infile.open ("textIn.txt");

	if (!infile)
	{
		cout << "Cannot open the input file." << endl;
		
		return 1;
	}

	outfile.open("textOut.txt");

	myCount.setCount(127);
	myCount.setCharacter('A');
	myCount.getCount();
	myCount.getCharacter();
	myCount.increment();





	system ("pause");
	return 0;
}


char charFrequency::getCharacter()
{
	return character;
}


long charFrequency::getCount()
{
	int index;
	for (index = 0; index < 127; index++)
	return	cout << static_cast<char> (index + static_cast<int> ('A')) << " count = " << index << endl;
}

void charFrequency::setCharacter(char character)
{
	char ch;
	character = static_cast<int>(ch) - static_cast<int>('A');
}

void charFrequency::setCount(long count)
{
	 
}

void charFrequency::increment()
{
	character++;


}

Were you told that this is what your class has to look like?

Your getCount() function should simply return the count. It doesn't have to actually count anything... (Also, you cannot return multiple times from a loop.)

Why does your setCharacter() modify the character. Just this->character = character;.

The setCount() function should work likewise.

The increment() function is supposed to increment the count of characters, not the character value.


If your assignment is
to make a class that counts the ASCII characters from a file, and outputs the count of each character
then you need some more work, since the class you have only works for a single character. An ASCII file can properly contain 128 different character codes -- you might want to program it with the possibility that it can contain 256 different character codes.

Google around "C/C++ lookup table" for more. The idea is that you need an array (or other indexable container, like a vector) that has 128 (or 256) entries. When the program starts, the entire container is initialized to zero. Every time you read a character from file, use the character as an index into the array/container and increment the value. For example:
1
2
3
4
5
6
7
8
9
10
11
// Here is my array, initialized to zeros
unsigned long counts[ 256 ] = { 0 };

// Here is a vector, initialized to zeros
vector <unsigned long> counts( 256, 0 );

// Here is a character
char ch = 'A';

// Increment that character's count:
counts[ ch ] ++;

Hope this helps.
I understand now. Thanks a lot
Topic archived. No new replies allowed.