D
DrNoose
Hi!
I'm writing a program that is supposed to read a line of input, count
the words and the number of occurrences of each letter. Then it should
prints the number of occurrences of each letter that appears in the
input line. For example, if I typed "I say Hi." it should give the
following output:
3 words
1 a
1 h
2 i
1 s
1 y
Here is my problem. If I type in a phrase like "I ask you, what is
truth? I get this:
7 words
257 a
512 h
257 i
1 k
1 o
256 r
257 s
768 t
257 u
256 w
1 y
Press any key to continue
Why am I getting this bad output? Can anyone see anything in my code
that would cause this. You should be able to put commas in the middle of
your text and not have it count as a word count or print garbage. Here
is my code:
#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 = 0;
int letterCount[26] = {0}; // 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 c;
do
{
// If the character can't be read for some reason
if ( !cin.get ( c ) )
break;
// Assuming ASCII, and isupper and tolower can't be used
// Convert if upper case letter to lower case
if ( c >= 'A' && c <= 'Z' )
c -= 'A' - 'a';
// Only works with lower case letters
++letterCount[c - 'a'];
if ( c == '\n' || c == '\t' || c == ' '|| c == ',' || c == '.' )
++numWords;
} while ( c != '\n' );
}
void outputLetterCounts(int letterCount[])
{
for (int i = 0; i < 26; i++)
{
if (letterCount > 0)
{
cout << letterCount << " " << char('a' + i) << endl;
}
}
}
Thanks!
Kathy
I'm writing a program that is supposed to read a line of input, count
the words and the number of occurrences of each letter. Then it should
prints the number of occurrences of each letter that appears in the
input line. For example, if I typed "I say Hi." it should give the
following output:
3 words
1 a
1 h
2 i
1 s
1 y
Here is my problem. If I type in a phrase like "I ask you, what is
truth? I get this:
7 words
257 a
512 h
257 i
1 k
1 o
256 r
257 s
768 t
257 u
256 w
1 y
Press any key to continue
Why am I getting this bad output? Can anyone see anything in my code
that would cause this. You should be able to put commas in the middle of
your text and not have it count as a word count or print garbage. Here
is my code:
#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 = 0;
int letterCount[26] = {0}; // 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 c;
do
{
// If the character can't be read for some reason
if ( !cin.get ( c ) )
break;
// Assuming ASCII, and isupper and tolower can't be used
// Convert if upper case letter to lower case
if ( c >= 'A' && c <= 'Z' )
c -= 'A' - 'a';
// Only works with lower case letters
++letterCount[c - 'a'];
if ( c == '\n' || c == '\t' || c == ' '|| c == ',' || c == '.' )
++numWords;
} while ( c != '\n' );
}
void outputLetterCounts(int letterCount[])
{
for (int i = 0; i < 26; i++)
{
if (letterCount > 0)
{
cout << letterCount << " " << char('a' + i) << endl;
}
}
}
Thanks!
Kathy