Counters mess each other up

Feb 8, 2017 at 3:19am
Hi again, hope I posted this correctly. I wrote some code to simply count words, lines and characters in a text file. But when i run this entire code the output is incorrect. If i only run the word counter i get the right answer; this also applies for lines. My character counter gives me a wrong output every time. How do I run this code without one counter messing up the other?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 #include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main ( int argc, char *argv[] )
{
  
    ifstream the_file ( argv[1] );
    
    if ( !the_file.is_open() )
      cout<<"Cant open file";
    else {
        string lines;
      int l=0;
      while (getline(the_file, lines)) 
      {
        ++l;
         
      }
               
    string word;
	int w=0;
	while(!the_file.eof())
	{
		the_file >> word;
		w++;
        
    
    }
        int char_count = 0;
       char ch;

       while (!the_file.eof())
       {
              the_file >> ch;
           
           char_count++;
       }
          
        cout << l<<endl <<w<<endl <<char_count;
    }
}



output

< 78
< 0
< 0
\ No newline at end of file
> 78 330 1791 file2.txt

Last edited on Feb 8, 2017 at 3:21am
Feb 8, 2017 at 4:14am
To count the lines you read the entire file. ¿In what part do you instruct the program to go back to the beginning to now count the words/characters?
Or instead of reading the file three times, you may process each line.

Also, don't loop on eof, loop on the reading operation
while( the_file >> word )
Feb 9, 2017 at 12:33am
Ok, now i understand to go back to the beginning of the program.
The extra to the while statement is in case a file is empty. so it will return 0 0 0 because it was returning 0 1 1. but im still getting the character/letter counter wrong. can u see why? im getting 1790 when it should be 1791.
ps
thanks for the earlier comment. It helped me so much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main ( int argc, char *argv[] )
{
  
    ifstream the_file ( argv[1] );
    
    if ( !the_file.is_open() )
      cout<<"Cant open file";
    else {
        string lines;
      int l=0;
      while (getline(the_file, lines)) 
      {
          
        ++l;
         
      }
        
the_file.clear();
the_file.seekg(0, ios::beg);
     
            
    string word;
	int w=0;
        
	while(true)
{
    
	if(the_file.peek() == -1)
		break;
        
		the_file >> word;
		w++;
		
 }
        
the_file.clear();
the_file.seekg(0, ios::beg);
        
	int number_of_chars = 0;
	char c;

	while(true)
{
    
	if(the_file.peek() == -1)
		break;

	c = the_file.get();
	

		++number_of_chars;
    
}
        
        cout << l<<endl <<w<<endl <<number_of_chars;
    }
}



output;
< 78
< 330
< 1790
Last edited on Feb 9, 2017 at 12:42am
Feb 9, 2017 at 1:48am
learn to indent, that hurts to look at.

> The extra to the while statement is in case a file is empty. so it will
> return 0 0 0 because it was returning 0 1 1.
No, that's wrong.
Suppose that the file has only one character, an space ' '.
1
2
3
4
5
6
		while(true) {
			if(the_file.peek() == EOF) //it passes, there's an space
				break;
			the_file >> word; //this would execute (and fail)
			w++; //you've counted a word.
		}


Again, loop on the input operation.


However, for the characters it should be the same. Perhaps you see different in windows if the file ends with an end-of-line or not.
You may do while( the_file >> std::noskipws >> c ) instead.
Last edited on Feb 9, 2017 at 1:56am
Feb 9, 2017 at 7:32am
I did peek the way above but still get one char less in my output, maybe The_file characters were counted incorrectly for this test case.

Thank you for all the helps :)
Topic archived. No new replies allowed.