I have a program that is designed to read in a line of text and ouptut the number of words in the line and the number of occurrences of each letter. A word is defined as any string of letters that is delimted at each end by either whitespace, a period, a comma or the beginning of a new line. This is what I have so far. It seems like it continues looping because when I interrupt it, it has these large number counts for the word and letters.
#include <iostream>
#include <cctype>
using namespace std;
void readAndCount (int &numWords, int letterCount[]);
// Reads a line of input. Counts the words and the number
// of occurrences of each letter.
void outputLetterCounts (int letterCount[]);
// Prints the number of occurrences of each letter that
// appears in the input line.
// =========================
// main function
// =========================
int main()
{
int numWords;
int letterCount[26]; // stores the frequency of each letter
cout << endl;
cout << "Enter a line of text.." << endl << endl;
readAndCount (numWords, letterCount);
cout << endl;
cout << numWords << " words" << endl;
outputLetterCounts(letterCount);
return 0;
}
// =========================
// Function Definitions
// =========================
void readAndCount (int &numWords, int letterCount[])
{
char name [20];
int p = 0;
int occ = 0;
while (p < 20)
{
if (name[p] == '\0')
{
occ = occ+1;
}
cin >> name[p];
p = p + 1;
}
}
void outputLetterCounts(int letterCount[])
{
for (int i = 0; i < 26; i++)
{
if (letterCount[i] > 0)
{
cout << letterCount[i] << " " << char('a' + i) << endl;
}
}
}