By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,377 IT Pros & Developers. It's quick & easy.

why function isn't counting words

P: 57
I am writting a code that opens a file and gets the file name from a command line argument. I then open an output stream to print results of functions in a separate file. I have written a function and it isn't doing what I want it to. It is suppose to count the words in the file that was opened.

the word_count function only incruments once giving a word count of one and I can figure out why. Can anyone help?

this is my code.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. using std::cin;
  6. using std::cout;
  7. using std::cerr;
  8. using std::ifstream;
  9. using std::ofstream;
  10. using std::setw;
  11. using std::string;
  12.  
  13.  
  14.  
  15. void word_count(ifstream&, ofstream&);
  16. //void line_count(ifstream&, ofstream&);
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20. //if command line doesnt have 2 arguments output error message.
  21.     if(argc < 2)
  22.     cout << "error need more arguments.";
  23.  
  24.  
  25.  
  26. while(argc > 1)
  27. {
  28.  
  29. //output each command line argument.
  30. for(int idx = 1; idx < argc; ++idx)
  31. {
  32.     //put file name in a c string.
  33.     string arg(argv[idx]);
  34.  
  35.  
  36.  
  37.         // Define object for input
  38.     ifstream in(arg.c_str());
  39.     if(!in)
  40.     {
  41.             // couldn't open input file, exit
  42.         cerr << "Error: couldn't open "
  43.              << arg
  44.              << " exiting\n";
  45.         exit(1);
  46.     }
  47.  
  48.         // create name of output file
  49.     string out_file_name = arg + ".COPY";
  50.  
  51.         // Define object for output
  52.     ofstream out(out_file_name.c_str());
  53.     if(!out)
  54.     {
  55.             // couldn't open output file, exit
  56.         cerr << "Error: couldn't open "
  57.              << out_file_name
  58.              << " exiting\n";
  59.         exit(1);
  60.     }
  61.  
  62.     const int WIDTH = 4;
  63.     int count = 1;
  64.     string input_string;
  65.  
  66.  
  67.     while (in >> input_string)
  68.     {
  69.  
  70.            // one word per line
  71.         out << setw(WIDTH) << count << ": "
  72.             << input_string << "\n";
  73.         ++count;
  74.     }
  75.  
  76.  
  77.     word_count(in,out);
  78.     //line_count(in,out);
  79.  
  80.  
  81.         // Close files
  82.     in.close();
  83.     out.close();
  84.     return 0;
  85. }}
  86. }
  87.  
  88. //fuction definitions
  89. void word_count(ifstream& in, ofstream& out)
  90. {
  91.     int count = 0;
  92.     while (in >> !eof())
  93.     {
  94.         ++count;
  95.     }
  96.  
  97.  
  98.     out << "the word count is: " << count << "\n";
  99.  
  100. }
  101.  
  102. //void line_count(ifstream& in, ofstream& out)
  103. //{
  104. //    int count = 1;
  105. //    string input_string;
  106. //    while(in >> getline(input_string))
  107. //    {
  108. //
  109. //      ++count;
  110. //    }
  111. //
  112. //    out << "the line count is: " << count << "\n";
  113. //
  114. //}
May 27 '07 #1
Share this Question
Share on Google+
2 Replies


DeMan
100+
P: 1,806
I think (could be wrong) that >> gets confused when you get to a space....
Try looping using getline(), and then either tokenising it or using find to search for spaces
May 27 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Your bug is here:

Expand|Select|Wrap|Line Numbers
  1. while (in >> input_string)
  2.     {
  3.  
  4.            // one word per line
  5.         out << setw(WIDTH) << count << ": "
  6.             << input_string << "\n";
  7.         ++count;
  8.     }
  9.  
  10.  
  11.     word_count(in,out);   <----THE BUG
  12.     //line_count(in,out);
  13.  
Your count is OK but that call to word_count sets count to 0 and then drops into a loop that does some really odd stuff. Most importantly the input file is at eof(). So the loop exits immediately and you display a count of 0.

Maybe instead this word_count call you could just cout << count??
May 27 '07 #3

Post your reply

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