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
- #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(ifstream&, ofstream&);
- //void line_count(ifstream&, 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_name.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,out);
- //line_count(in,out);
- // Close files
- in.close();
- out.close();
- return 0;
- }}
- }
- //fuction definitions
- void word_count(ifstream& in, ofstream& out)
- {
- int count = 0;
- while (in >> !eof())
- {
- ++count;
- }
- out << "the word count is: " << count << "\n";
- }
- //void line_count(ifstream& in, ofstream& out)
- //{
- // int count = 1;
- // string input_string;
- // while(in >> getline(input_string))
- // {
- //
- // ++count;
- // }
- //
- // out << "the line count is: " << count << "\n";
- //
- //}