why function isn't counting words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    why function isn't counting words

    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.

    [CODE=cpp]#include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using std::cin;
    using std::cout;
    using std::cerr;
    using std::ifstream;
    using std::ofstream;
    using std::setw;
    using std::string;



    void word_count(ifst ream&, ofstream&);
    //void line_count(ifst ream&, ofstream&);

    int main(int argc, char* argv[])
    {
    //if command line doesnt have 2 arguments output error message.
    if(argc < 2)
    cout << "error need more arguments.";



    while(argc > 1)
    {

    //output each command line argument.
    for(int idx = 1; idx < argc; ++idx)
    {
    //put file name in a c string.
    string arg(argv[idx]);



    // Define object for input
    ifstream in(arg.c_str()) ;
    if(!in)
    {
    // couldn't open input file, exit
    cerr << "Error: couldn't open "
    << arg
    << " exiting\n";
    exit(1);
    }

    // create name of output file
    string out_file_name = arg + ".COPY";

    // Define object for output
    ofstream out(out_file_na me.c_str());
    if(!out)
    {
    // couldn't open output file, exit
    cerr << "Error: couldn't open "
    << out_file_name
    << " exiting\n";
    exit(1);
    }

    const int WIDTH = 4;
    int count = 1;
    string input_string;


    while (in >> input_string)
    {

    // one word per line
    out << setw(WIDTH) << count << ": "
    << input_string << "\n";
    ++count;
    }


    word_count(in,o ut);
    //line_count(in,o ut);


    // Close files
    in.close();
    out.close();
    return 0;
    }}
    }

    //fuction definitions
    void word_count(ifst ream& in, ofstream& out)
    {
    int count = 0;
    while (in >> !eof())
    {
    ++count;
    }


    out << "the word count is: " << count << "\n";

    }

    //void line_count(ifst ream& in, ofstream& out)
    //{
    // int count = 1;
    // string input_string;
    // while(in >> getline(input_s tring))
    // {
    //
    // ++count;
    // }
    //
    // out << "the line count is: " << count << "\n";
    //
    //}[/CODE]
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Your bug is here:

      [code=cpp]
      while (in >> input_string)
      {

      // one word per line
      out << setw(WIDTH) << count << ": "
      << input_string << "\n";
      ++count;
      }


      word_count(in,o ut); <----THE BUG
      //line_count(in,o ut);
      [/code]

      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??

      Comment

      Working...