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
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 isalphabet is an array that should count all the letters from a to z and count the occurance of each.Code: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]++; }
please help, im very confused
thanks very much
goo



LinkBack URL
About LinkBacks


