I got most of this too work, i talked to my teacher and he gave me some hints but i cannot get it full. The idea is to get the program to count specifically how many "a"'s there continueing through all the 26 letters. I cannot figure out exactly out how to get that part to work. I want to use the function "tolower" to get the letters all lower case when going into the program. we just need to count the number of each specific letter not distinguish between upper and lower case. A file is being read into a file.
here the code

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


void printArray(const int [], int);

int main ()
{
	unsigned int strlength;
	int alphabet[26] = {0};
	int charnum = 0;
	char fileName[30];
	char str[26];
	char letter = 'a';
	

	cout << "Enter the name of the input file"; // opens file

	cin >> fileName;

	ifstream infile(fileName);

	if ( !infile ) 
	{
		cerr << "Cannot open the input file .\n "; // error occurs if bad file name

		return (1);
	}
	
	while ((infile >> str) && (*str != EOF)) // continues until end of file
	{										//counts char without spaces
		string line;
		getline(infile, line);
		int lentemp = line.size();
		charnum += lentemp;
	}
	cout << "The number of char without spaces is " << charnum << endl;


	while ((infile >> str) && (*str != EOF))//counting the number of each letter
	{ 
		if (97 <= (static_cast<int>(tolower('a'))) <= 122)
		
		alphabet[static_cast<int>('a') - 97]++;
	}		
	printArray(alphabet,  26 ); // prints historgram for frequency of letter

	

	infile.close ();

	for (int i = 1; i <= 26; i++)
	{
		cout << "alpha[ " << i << "] = " << alphabet[i] << endl; 
	}		
	return ( 0 );

}

void printArray(const int a[], int size) // function for historgram
{
	for (int i= 0; i < size; i++)
	{ 
	if ( i % 20 == 0 )
		cout << endl;
		cout << setw(2) << a[i];
	}
}

Thats the entire program the part im having problems with is

while ((infile >> str) && (*str != EOF))//counting the number of each letter
	{ 
	if (97 <= (static_cast<int>(tolower('a'))) <= 122)
		
	alphabet[static_cast<int>('a') - 97]++;
	}

alphabet is an array that should count all the letters from a to z and count the occurance of each.
please help, im very confused
thanks very much
goo

Recommended Answers

All 5 Replies

This is not likely ever to be true.

*str != EOF

The first loop goes to the end of file, right? Then the second loop never has anything to read, right?

When do you expect to get anything different for this?

if (97 <= (static_cast<int>(tolower('a'))) <= 122)
		alphabet[static_cast<int>('a') - 97]++;

That is, when will 'a' not be between 'a' and 'z' (in ASCII)?

Arrays are indexed from 0 to N-1, not 1 to N.

for (int i = 1; i <= 26; i++)

For a 26-element array, the elements would be from 0-25.

>This is not likely ever to be true.
You mean false? I'd say it's highly likely to be true since EOF must be a negative quantity and characters gathered from a narrow stream must fit within an unsigned char. Fortunately, the other half of the condition will cause the loop to terminate properly.

>>This is not likely ever to be true.
>You mean false?
Uh, yeah. Parse error between monitor and chair.

>Uh, yeah. Parse error between monitor and chair.
Wetware compilers are so unreliable. Sounds like you need an upgrade. ;)

>>Uh, yeah. Parse error between monitor and chair.
>Wetware compilers are so unreliable. Sounds like you need an upgrade. ;)
Perhaps. But I know I need to get rid of this darn virus -- I've had it for almost two weeks. (Me and the baby and the wonders of day care keep passing it around in mutated forms methinks.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.