943,935 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4955
  • C++ RSS
Apr 25th, 2005
0

help with counting letters part of program

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bluegoo06 is offline Offline
5 posts
since Sep 2004
Apr 26th, 2005
0

Re: help with counting letters part of program

This is not likely ever to be true.
C++ Syntax (Toggle Plain Text)
  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?
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. for (int i = 1; i <= 26; i++)
For a 26-element array, the elements would be from 0-25.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 26th, 2005
0

Re: help with counting letters part of program

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 26th, 2005
0

Re: help with counting letters part of program

>>This is not likely ever to be true.
>You mean false?
Uh, yeah. Parse error between monitor and chair.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 26th, 2005
0

Re: help with counting letters part of program

>Uh, yeah. Parse error between monitor and chair.
Wetware compilers are so unreliable. Sounds like you need an upgrade.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 26th, 2005
0

Re: help with counting letters part of program

>>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.)
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Displaying records
Next Thread in C++ Forum Timeline: using mysql_real_escape_string()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC