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.