Counting words in file

Hello funs2code!
I have the following problem. I need to count the number of occurrences of a concrete word in a text file. I've tried to count the number of occurrences of each word at first.
The following code has no errors but it isn't working! When I type in a command line "project1.exe test.dat cat dog cat dog" the empty string is showed. I've checked if-statements -> right working. Then I've made a conclusion that problem is in for-statement (with iterators).

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
#include <iostream.h>
#include <fstream.h>
#include <map.h>
#include <string.h>
#include <iterator.h>

typedef std::map<std::string, int> StrIntMap;

void countWords(std::istream& in, StrIntMap& words) {
std::string s;

  while (in >> s) {
++words[s];
  }

}

using namespace std;

int main(int argc, char** argv) {

	if (argc < 2) {
                printf("\nNot correct input");
		return(EXIT_FAILURE);
                }

ifstream in(argv[1]);
	if (!in) {
                printf("\nFile ");
                printf(argv[1]);
                printf(" is not found");
		exit(EXIT_FAILURE);
                }
StrIntMap w;
countWords(in, w);

	for (StrIntMap::iterator p = w.begin( );
	p != w.end( ); ++p) {
		std::cout << p->first << " occurred "
		<< p->second << " times.\n";
	}

}
Last edited on
I tried your code and it seems to work for me. The only thing of consequence which I changed was the headers, instead of #include <iostream.h>
It should be #include <iostream> etc.

I also changed the indentation partly as a matter of preference, but also I found the original code hard to read because of inconsistent indentation.

(I also used setw() to format the output - that was simply my own preference).


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
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <iterator>
#include <cstdlib>
#include <iomanip>

typedef std::map<std::string, int> StrIntMap;

void countWords(std::istream& in, StrIntMap& words)
{
    std::string s;

    while (in >> s)
    {
        ++words[s];
    }
}

using namespace std;

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        printf("\nNot correct input");
        return EXIT_FAILURE;
    }

    ifstream in(argv[1]);
    if (!in)
    {
        printf("\nFile ");
        printf(argv[1]);
        printf(" is not found");
        exit(EXIT_FAILURE);
    }

    StrIntMap w;
    countWords(in, w);

    for (StrIntMap::iterator p = w.begin( ); p != w.end( ); ++p)
    {
        std::cout << setw(20) << left  << p->first  << " occurred "
                  << setw(5)  << right << p->second << " times.\n";
    }

}
Hi @Chervil! I've compiled your recommended code by Builder 6.0 and go in the directory where "project1.exe" is located (in a cmd). Then I've typed "project1.exe test.dat cat dog cat dog". Program has responded me "File test.dat is not found" (according to the line 32 in the code above). Then I've created empty file "test.dat" in the directory where "project1.exe" is located (Folder "CountWords"). I've typed "project1.exe test.dat cat dog cat dog" again and emptiness was shown. What did I realize wrong?

"cat occurred 2 times.
dog occurred 2 times." would shown?
Last edited on
I don't quite follow what it is you are doing. You seem to have extra parameters on the command line which are not used by your program:

argv[1] = "test.dat"
argv[2] = "cat"
argv[3] = "dog"
argv[4] = "cat"
argv[5] = "dog"

What I did was to create a file called "input.txt" and into that i typed some words, then saved that file.

After that I ran the program with "progname.exe input.txt".

Oh... Hey @Chervil, first off thank you so much, your explanation makes it more clear to me. I've considered that it is necessary to type text in the command line, not in the text file :P
Thanks again!
Topic archived. No new replies allowed.