| | |
help with counting letters part of program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 5
Reputation:
Solved Threads: 0
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
Thats the entire program the part im having problems with is
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
here the code
C++ Syntax (Toggle Plain Text)
#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]; } }
C++ Syntax (Toggle Plain Text)
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
This is not likely ever to be true. 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? 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 a 26-element array, the elements would be from 0-25.
C++ Syntax (Toggle Plain Text)
*str != EOF
When do you expect to get anything different for this?
C++ Syntax (Toggle Plain Text)
if (97 <= (static_cast<int>(tolower('a'))) <= 122) alphabet[static_cast<int>('a') - 97]++;
Arrays are indexed from 0 to N-1, not 1 to N.
C++ Syntax (Toggle Plain Text)
for (int i = 1; i <= 26; i++)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>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.
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.
New members chased away this month: 4
>>This is not likely ever to be true.
>You mean false?
Uh, yeah. Parse error between monitor and chair.
>You mean false?
Uh, yeah. Parse error between monitor and chair.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>>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.)
>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.)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Tutorial: Automating Office applications (Visual Basic 4 / 5 / 6)
- How to count/keep track of letters from file input in C? (C)
- how can i make the program depend on time (C)
Other Threads in the C++ Forum
- Previous Thread: Displaying records
- Next Thread: using mysql_real_escape_string()
Views: 3866 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






