473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

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
2 1432
DeMan
1,806 1GB
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
9,208 Expert Mod 8TB
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

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

Similar topics

30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
19
by: anguo | last post by:
i find in many hash function use 5381,for exampla: static constmap_hash hash(char *pchData, int iLen) { unsigned char cBuf; constmap_hash ulHashId; ulHashId = 5381; while (iLen > 0) { cBuf =...
4
by: sun6 | last post by:
this is a program counting words from "text_in.txt" file and writing them in "text_out.txt". it uses binary tree search, but there is an error when i use insert () thanks for any help ...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
13
by: | last post by:
I'm curious if anyone knows why the C# and VB.NET compilers don't automatically call Dispose() on objects that support IDisposable when they go out of scope. I asked a co-worker and his response...
6
by: gk245 | last post by:
Basically, i want to make a function that will receive a sentence or phrase, and count its words. It would start like this (i think): #include <stdio.h> int count ( char sentence ) {...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: Words are delimited by white spaces (blanks,...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.