#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
int count[26] = {0};
string input;
cout << "Enter a message: ";
getline(cin, input);
for(int i = 0; i < input.length(); i++)
{
if(isalpha(input[i])) // A-Za-z
{
int j;
if(isupper(input[i])) {
j = input[i] - 'A';
}
else {
j = input[i] - 'a';
}
count[j]++;
}
}
// Show the counts:
for(int i = 0; i < 26; i++)
{
char letter = i + 65;
cout << setw(5) << letter
<< setw(3) << count[i];
if(i%5 == 4) {
cout << endl;
}
}
cout << endl;
return 0;
}
// Dealing with errors in 'cin'.
// Here's an example that can detect if reading a
// numeric value fails. You have to tell cin to
// clear its error condition and then ignore the
// previous input. It's a little awkward.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number: ";
while(!(cin >> num))
{
cout << "ERROR: try again: ";
cin.clear(); // reset error state
cin.ignore();// must clear *then* ignore
}
cout << "You entered " << num << endl;
return 0;
}