The way you are currently doing this is just too painful.
I noticed you are using STL and don't know if you are allowed to use
any of the data structures there. If you can this problem becomes
trivial by using the std::map<char, int> data structure.
Now for you code,
This looks like a problem. ch is defined as a char. So anytime you
read in your input, you are only reading 1 character. Either use a std::string
or create a char buffer
CODE
// From prompt(void)
cin >> ch;
// Define like this instead
char buffer[256];
// or define like this
std::string buffer2;
Next count_let()
CODE
for(i=0; letter[26];ch++)
{
if(letter[26]==ch)
{
acount++;
bcount++;
}
}
I'm not sure what you are trying to do here.
The for loop needs a terminal case.
letter[26] == Z and Z is undefined. It was never initialized to anything
so it should be treated as having a garbage value.
Ok, so how do we move towards a solution. Well think about what it is your trying to count.
In this case letters A-Z and a-z. If we look at the ASCII table we can find out that those
letters are really just the integers such that
A-Z == 65 to 90
a-z == 97 to 122
there are a couple of other values between Z and a. ( we don't care about those)
So what we need is a way to map the letters to a counter variable in this case we need
122 - 65 place holders(57)
That can be done like this
CODE
#define BINS 58
int counters[BINS] = {0};
Next we need to figure out how to map a letter (A-Z, a-z) to one of the bins in counters.
so think about it like this
counters[0] == A
counters[1] == B
counters[2] == C
....
....
counters[32] == a
counters[33] == b
...
...
counters[57] == z
NOTE: This is not assigning anything. This is simply a logical mapping between the
ASCII letters and the counters array.
The code below is the fix for count_let(). It will be up to you to finish/fix the other functions
CODE
std::string data; // input string
int letters[58] = {0};
int count_let(void)
{
char ch;
for(int i=0; i < data.length(); i++)
{
ch = data[i];
// why 65? Because 'A' == 65 and we want
// letters[0] to map to 'A'
letters[ch - 65]++;
}
}
All that remains is to fix your display function.
Cheers
This post has been edited by skaoth: 21 Nov, 2007 - 01:30 AM