944,137 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12777
  • C++ RSS
Sep 14th, 2006
1

character count

Expand Post »
hey guys,
i am trying to get my code to count the number of characters in a file. However, the program is continuously going through the characters (so it goes from a-z thousands of times) when i want it to say how many times 'a' occured and 'b' and so on, in the text file.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. void characterCount(char ch, int list[]);
  6. void writeTotal(int list[]);
  7. void initialize(int list[]);
  8. int main()
  9. {
  10. int letterCount[26];
  11. string fileName;
  12. ifstream inStream;
  13. inStream.open("C:\\data.txt");
  14. if (inStream.fail())
  15. {
  16. cout << "could not open input file";
  17. return 1;
  18. }
  19.  
  20. while (!inStream.eof())
  21. {
  22. writeTotal(letterCount);
  23. }
  24. system("PAUSE");
  25. return 0;
  26. }
  27. void initialize(int list[])
  28. {
  29.  
  30. for(int j=0; j<26; j++)
  31. list[j]=0;
  32. }
  33. void characterCount(char ch, int list[])
  34. {
  35. int index;
  36. ch=tolower(ch);
  37.  
  38. index=static_cast<int>(ch)-static_cast<int>('a');
  39.  
  40. if (0 <= index & index <26)
  41. list[index]++;
  42. }
  43. void writeTotal(int list[])
  44. {
  45. int index;
  46.  
  47. for(index=0; index<26; index++)
  48. cout<<static_cast<char>(index
  49. +static_cast<int>('a'))
  50. <<"("<<list[index]<<")"<<endl;
  51. }
When i change my while loop to:
C++ Syntax (Toggle Plain Text)
  1. while(!inStream.eof())
  2. {
  3. inStream>>fileName;
  4. cout<<fileName<<" ("<<fileName.size()<<")"<<endl;
  5. }
i manage to get all the words to display with the amount of characters in each word. I am struggling to make the transition from calculating how many in a word to how many of each letter.
Any help will be appreciated.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Newbie Poster
insamd is offline Offline
13 posts
since Aug 2006
Sep 14th, 2006
2

Re: character count

First off, lets look at two code segments. The first is your original while loop, the second is writeTotal.
C++ Syntax (Toggle Plain Text)
  1. /* ... */
  2. // the while loop:
  3. while (!inStream.eof())
  4. {
  5. writeTotal(letterCount);
  6. }
  7. /* ... */
  8. // writeTotal
  9. void writeTotal(int list[])
  10. {
  11. int index;
  12. for(index=0; index<26; index++)
  13. cout << static_cast<char>(index+static_cast<int>('a')) << "(" << list[index] << ")" << endl;
  14. }
Your loop is based of whether you've reached the end of the file. If you have not, you call writeTotal. That outputs the total from the array. It doesn't change the file, so you keep looping (thousands of times, if you let it go that long).

Second, using eof() might not be the best way to check for file state (see http://www.daniweb.com/techtalkforum...155265-18.html; and kudos to Dave Sinkula for posting it in another recent thread. )

[edit:] So I tinkered with the code a little bit to get it working, and there were some things I noticed. You don't initialize letterCount, so whatever values you get as your result will likely be on the order of -1203455802 or similar. That can be fixed by calling initialize(), or just having int letterCount[26] = {0}; and the compiler will autofill the array with 0.

Next, you never call characterCount. This is the critical part of your program. I'd recommend that you change it to accept a string instead of a character, but that's up to you. Either way, there's also no check in place to make sure the char is a letter or not. You can #include <cctype> to get the isalpha(char) function.

Third, writeTotal should not be inside the loop, unless you want to see all 26 counts each time one of them changes. A little extreme in my opinion.
Last edited by Infarction; Sep 14th, 2006 at 3:38 am.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Sep 14th, 2006
0

Re: character count

thanks for your prompt reply. i called initialized letterCount and the letters come up, except with 0 next to each one, so this i gather is where i need to actually get the characterCount happening so. I was thinking that maybe
C++ Syntax (Toggle Plain Text)
  1.  
  2. while (!inStream.eof())
  3. {
  4. characterCount(ch, letterCount);
  5. }
will work... but it doesn't show up anything fullstop. This might be where the eof is messing me up.
I 'can' use eof though can't i, its just im using the incorrect code!?
thanks again
Last edited by insamd; Sep 14th, 2006 at 4:03 am.
Reputation Points: 16
Solved Threads: 0
Newbie Poster
insamd is offline Offline
13 posts
since Aug 2006
Sep 14th, 2006
2

Re: character count

Yeah, you can use eof(). If you read the example in the link I posted, you might get duplicate data.

The problem with the code segment you just posted is that you're still not reading anything from the file. (You also have nothing to pass to characterCount() becaouse of it.) You could do something along the lines of:
C++ Syntax (Toggle Plain Text)
  1. string temp;
  2. while (inStream >> temp) // loop as long as you can read data from the file
  3. {
  4. for(int i = 0; i < temp.length(); ++i)
  5. characterCount(temp[i], letterCount); // pass each letter to characterCount()
  6. }
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Sep 14th, 2006
1

Re: character count

ahhh how silly of me!! thanks, i managed to get it working! just fixed up my code a little and voila im seeing the results.
thanks for your help, appreciated!!! incrementing your rep :p
Reputation Points: 16
Solved Threads: 0
Newbie Poster
insamd is offline Offline
13 posts
since Aug 2006

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: having touble can anyone help
Next Thread in C++ Forum Timeline: using ifstream, counting elements of each line in a dat file





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


Follow us on Twitter


© 2011 DaniWeb® LLC