943,640 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 362
  • C++ RSS
Apr 8th, 2009
0

Reading in Character Frequences help

Expand Post »
Hi I was wonder if anyone could help with this problem I am receiving. My program is suppose to read in any character frequencies from a file, even spaces and /n. My problem is that is reading an extra character that I can figure out.
I had inputed a text file that aab no spaces and no new lines. It would out put as
The Character Frequencies are
1
a 2
b 1
which throws everything off.
This program will make a huffman code out of the frequencies, but it messes up everything cause of the unknown space and 1. Here is what I've been using for my read input. If anyone could take a look I would really appreciate it.


C++ Syntax (Toggle Plain Text)
  1. #include <cstdio>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cstring>
  5. //tab space 1
  6. using namespace std;
  7.  
  8.  
  9. void phase1(int array[], ifstream& in)
  10. {
  11. int c = 0;
  12. for(int i = 0; i < 255; i++)
  13. {
  14. array[i] = 0;
  15. }
  16. while(c != EOF)
  17. {
  18. c = in.get();
  19. c = (int)c;
  20. array[c] = array[c]+1;
  21. }
  22. }
  23.  
  24. //phase2 prints the frequency of each character
  25. //read from a text file
  26. void phase2(int array[])
  27. {
  28. int n = 0;
  29.  
  30. cout << "The character frequencies are: " << '\n';
  31.  
  32. while(n < 255)
  33. {
  34. if(array[n] != 0)
  35. {
  36. cout << (char)n;
  37. cout << " ";
  38. cout << array[n];
  39. cout << "\n";
  40. }
  41. n = n + 1;
  42. }
  43. }
  44.  
  45. int main(int argc, char* argv[])
  46. {
  47. ifstream in(argv[1]);
  48. int array[256];
  49. phase1(array, in);
  50. phase2(array);
  51. return 0;
  52. }
Reputation Points: 46
Solved Threads: 0
Newbie Poster
yingfo is offline Offline
13 posts
since Sep 2008
Apr 8th, 2009
0

Re: Reading in Character Frequences help

I actually don't understand your question ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 8th, 2009
0

Re: Reading in Character Frequences help

it will read in some imaginary output I guess is the best way the say it.
Since there are no spaces or new lines, its reading something that it shouldn't
that's why I get
1
a 2
b 1
does that make sense?
Reputation Points: 46
Solved Threads: 0
Newbie Poster
yingfo is offline Offline
13 posts
since Sep 2008
Apr 8th, 2009
0

Re: Reading in Character Frequences help

I guess the unwanted number '1' is the number of newlines your program has counted so far ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 8th, 2009
0

Re: Reading in Character Frequences help

In your function phase1 you've to change everything in the while loop to:
C++ Syntax (Toggle Plain Text)
  1. c = in.get();
  2. /* We use an if-statement to ignore the newlines */
  3. if(c != '\n')
  4. {
  5. c = (int)c;
  6. array[c] = array[c]+1;
  7. }

It should work now !
Last edited by tux4life; Apr 8th, 2009 at 11:13 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 8th, 2009
0

Re: Reading in Character Frequences help

If I were to make this, I wouldn't use array's, but I'd use a map:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. int main(void)
  8. {
  9. //store
  10. string str = "aab";
  11. map<char,int> total;
  12. for (unsigned i =0; i < str.size(); i++)
  13. total[str[i]] += 1;
  14. //show
  15. map<char,int>::iterator it;
  16. for (it = total.begin(); it != total.end(); ++it)
  17. cout << "'" << it->first << "': " << it->second << " times\n";
  18. return 0;
  19. }

nice and simple
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 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: Problem with a swap
Next Thread in C++ Forum Timeline: Problem with AfxBeginThread





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


Follow us on Twitter


© 2011 DaniWeb® LLC