Reading in Character Frequences help

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

Join Date: Sep 2008
Posts: 9
Reputation: yingfo is on a distinguished road 
Solved Threads: 0
yingfo yingfo is offline Offline
Newbie Poster

Reading in Character Frequences help

 
0
  #1
Apr 8th, 2009
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.


  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Reading in Character Frequences help

 
0
  #2
Apr 8th, 2009
I actually don't understand your question ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 9
Reputation: yingfo is on a distinguished road 
Solved Threads: 0
yingfo yingfo is offline Offline
Newbie Poster

Re: Reading in Character Frequences help

 
0
  #3
Apr 8th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Reading in Character Frequences help

 
0
  #4
Apr 8th, 2009
I guess the unwanted number '1' is the number of newlines your program has counted so far ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Reading in Character Frequences help

 
0
  #5
Apr 8th, 2009
In your function phase1 you've to change everything in the while loop to:
  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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,963
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Reading in Character Frequences help

 
0
  #6
Apr 8th, 2009
If I were to make this, I wouldn't use array's, but I'd use a map:
  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
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC