help with counting letters part of program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 5
Reputation: bluegoo06 is an unknown quantity at this point 
Solved Threads: 0
bluegoo06 bluegoo06 is offline Offline
Newbie Poster

help with counting letters part of program

 
0
  #1
Apr 25th, 2005
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
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8.  
  9. void printArray(const int [], int);
  10.  
  11. int main ()
  12. {
  13. unsigned int strlength;
  14. int alphabet[26] = {0};
  15. int charnum = 0;
  16. char fileName[30];
  17. char str[26];
  18. char letter = 'a';
  19.  
  20.  
  21. cout << "Enter the name of the input file"; // opens file
  22.  
  23. cin >> fileName;
  24.  
  25. ifstream infile(fileName);
  26.  
  27. if ( !infile )
  28. {
  29. cerr << "Cannot open the input file .\n "; // error occurs if bad file name
  30.  
  31. return (1);
  32. }
  33.  
  34. while ((infile >> str) && (*str != EOF)) // continues until end of file
  35. { //counts char without spaces
  36. string line;
  37. getline(infile, line);
  38. int lentemp = line.size();
  39. charnum += lentemp;
  40. }
  41. cout << "The number of char without spaces is " << charnum << endl;
  42.  
  43.  
  44. while ((infile >> str) && (*str != EOF))//counting the number of each letter
  45. {
  46. if (97 <= (static_cast<int>(tolower('a'))) <= 122)
  47.  
  48. alphabet[static_cast<int>('a') - 97]++;
  49. }
  50. printArray(alphabet, 26 ); // prints historgram for frequency of letter
  51.  
  52.  
  53.  
  54. infile.close ();
  55.  
  56. for (int i = 1; i <= 26; i++)
  57. {
  58. cout << "alpha[ " << i << "] = " << alphabet[i] << endl;
  59. }
  60. return ( 0 );
  61.  
  62. }
  63.  
  64. void printArray(const int a[], int size) // function for historgram
  65. {
  66. for (int i= 0; i < size; i++)
  67. {
  68. if ( i % 20 == 0 )
  69. cout << endl;
  70. cout << setw(2) << a[i];
  71. }
  72. }
Thats the entire program the part im having problems with is
  1. while ((infile >> str) && (*str != EOF))//counting the number of each letter
  2. {
  3. if (97 <= (static_cast<int>(tolower('a'))) <= 122)
  4.  
  5. alphabet[static_cast<int>('a') - 97]++;
  6. }
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help with counting letters part of program

 
0
  #2
Apr 26th, 2005
This is not likely ever to be true.
  1. *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?
  1. if (97 <= (static_cast<int>(tolower('a'))) <= 122)
  2. 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.
  1. for (int i = 1; i <= 26; i++)
For a 26-element array, the elements would be from 0-25.
"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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,856
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with counting letters part of program

 
0
  #3
Apr 26th, 2005
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help with counting letters part of program

 
0
  #4
Apr 26th, 2005
>>This is not likely ever to be true.
>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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,856
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with counting letters part of program

 
0
  #5
Apr 26th, 2005
>Uh, yeah. Parse error between monitor and chair.
Wetware compilers are so unreliable. Sounds like you need an upgrade.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help with counting letters part of program

 
0
  #6
Apr 26th, 2005
>>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.)
"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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3866 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC