468,248 Members | 1,463 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,248 developers. It's quick & easy.

calling function for each time for each word? duplicate?

254 100+
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5. #include <iterator>
  6.  
  7. using namespace std;
  8.  
  9. void f(string str){
  10.     transform(str.begin(), str.end(), str.begin(), ptr_fun(::tolower));
  11. }
  12.  
  13. void testFunc(){
  14.     map<string, int> frequent;
  15.     string str;
  16.        string strr;
  17.  
  18.     while(cin >> str){
  19.         strr = f(str);       // keep calling f(str) function in order to convert to lowercase.
  20.         frequent[str]++;
  21.     }
  22.     map<string, int>::const_iterator it;
  23.         for (it=frequent.begin(); it != frequent.end(); ++it) {
  24.         cout << it->second << " " << it->first << endl;
  25.     }
  26. }
  27.  
  28. int main(int argc, char* argv[]){
  29.  
  30.     string str2;
  31.     ifstream myfile;
  32.  
  33.     switch(argc){
  34.     case 1:      // no filename specified
  35.  
  36.         testFunc();
  37.         break;
  38.  
  39.     case 2:      // filename specified
  40.  
  41.         myfile.open(argv[1], ios::in);         // open the report.txt
  42.         if(myfile.is_open()){
  43.             while(! myfile.eof()){
  44.                 getline(myfile,str2);
  45.             }
  46.         }
  47.         else{
  48.             //cout << "Unable to open the file" << endl;
  49.         }
  50.         break;
  51.     }
  52.     return 0;
  53. }
  54.  
My code above, okay or not?
Is there any other simple way to convert all words user type in to lowercase by just calling the function ONCE only ?
look at the while loop in the function testFunc() ....
(try run the above program using NO command line argument, thanks)

thanks in advance.
Nicky Eng.
Nov 22 '06 #1
0 867

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by Nige | last post: by
1 post views Thread by Asapi | last post: by
3 posts views Thread by Scupper | last post: by
4 posts views Thread by Peter Afonin | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.