943,960 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 511
  • C++ RSS
Sep 6th, 2008
0

need help

Expand Post »
my program is suppose to count d number of occurrences of a chars in a file.
i just dont know how to store d occurences. this is wat i have done up to now.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. ifstream fin;
  6. void charactercount(int& w, char& s );
  7. void main()
  8.  
  9. {
  10. //int sum[128];
  11. int w;
  12. char s;
  13. char charin;
  14. char infile[20];
  15.  
  16. cout<< "enter file name"<< endl;
  17. cin>> infile;
  18. cout<<infile;
  19.  
  20. cout<< " " << endl;
  21.  
  22. fin.open(infile);
  23.  
  24.  
  25. if ( fin.fail())
  26. {
  27. cout<< " cannot find file" << endl;
  28. exit(0);
  29. }
  30. else
  31. {
  32. while(!fin.eof())
  33. {
  34. fin.get(charin);
  35.  
  36. w=static_cast<int>(charin);
  37. s=static_cast<char>(w);
  38.  
  39. }
  40. //here i think i have to make a for loop that compares the position of i (from 0 to 127) with the value of w and
  41. // if they are equal add one to d occurrence of d position i
  42.  
  43.  
  44.  
  45.  
  46. }}}
Thanks for reading it .
i hope u understand wat i mean.
Last edited by cscgal; Sep 6th, 2008 at 2:38 pm. Reason: Added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bigboy06 is offline Offline
13 posts
since Sep 2008
Sep 6th, 2008
0

Re: need help

Click to Expand / Collapse  Quote originally posted by Bigboy06 ...
Thanks for reading it .
i hope u understand wat i mean.
Well, No. Have you abbreviated 'the' to 'd' in your sentences?

Are you trying to count the number of occurences of each character in the file? If so, you could store the count in a map, or even an array.

e.g. int count[256]; // the index is the character, the value is the count

and increment the counter in your loop: count[(unsigned char)charin]++
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Sep 6th, 2008
-1

Re: need help

yes i use d for "the".

i will try that it .
i know very little about programming so i will study wat u told me thank u;

Click to Expand / Collapse  Quote originally posted by dougy83 ...
Well, No. Have you abbreviated 'the' to 'd' in your sentences?

Are you trying to count the number of occurences of each character in the file? If so, you could store the count in a map, or even an array.

e.g. int count[256]; // the index is the character, the value is the count

and increment the counter in your loop: count[(unsigned char)charin]++
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bigboy06 is offline Offline
13 posts
since Sep 2008
Sep 6th, 2008
0

Re: need help

hey;
thanks for d help but i still cant do it . can u explain a little bit better plz.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bigboy06 is offline Offline
13 posts
since Sep 2008
Sep 6th, 2008
0

Re: need help

Sorry if you can't understand my notes above. Post what you've done so far and I'll see if can explain better.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

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: Recursive function not fully working
Next Thread in C++ Forum Timeline: Creating a query





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


Follow us on Twitter


© 2011 DaniWeb® LLC