wordcount using functions? please help!!

Hi. i am a beginner at C++ and i need help with this program. i am trying to count specific words in a function, i think i am close at solving the problem , but i am stuck now and i do not know what to do. please help.

This is the start of my program, there are some things missing. but i do not know what they are. but the only errors that come up are, or should i say with this program all i get is

1996242174, <== 6 times

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int count_the_word(string file, string word = 0)
{
    ifstream filename;
    int result;

    if(!filename)
    {
        cout << "File not found." << endl;
    }
    else if(filename.is_open())
    {
        string word;
        unsigned long wordcount = 0;

    while (!filename.eof());
    {
        filename >> word;
        if(word == "the" || word == "is" || word == "Romeo" || word == "clock" || word == "synchronization")
        {
           wordcount++;
        }

    }
    cout << wordcount << endl;

    }
    filename.close();
}

int main()
{
    cout << count_the_word("raj.txt", "the") << endl;
    cout << count_the_word("raj.txt", "is") << endl;
    cout << count_the_word("raj.txt", "Romeo") << endl;
    cout << count_the_word("raj.txt", "case") << endl;
    cout << count_the_word("clock.txt", "clock") << endl;
    cout << count_the_word("clock.txt", "synchronization") << endl;
    return 0;
}
Last edited on
why have you told your function to return an int when you're printing out the count inside it?
also it looks like you dont open your file see first example here:
http://www.cplusplus.com/doc/tutorial/files/

Last edited on
the bottom portion is already a generated code that tests my function.

1
2
3
4
5
6
7
8
9
10
11
12

int main()
{
    cout << count_the_word("raj.txt", "the") << endl;
    cout << count_the_word("raj.txt", "is") << endl;
    cout << count_the_word("raj.txt", "Romeo") << endl;
    cout << count_the_word("raj.txt", "case") << endl;
    cout << count_the_word("clock.txt", "clock") << endl;
    cout << count_the_word("clock.txt", "synchronization") << endl;
    return 0;
}


also how would i open the file in the function, thats what i have been trying to do.
Last edited on
the bottom portion is already a generated code that tests my function.

so why are you also printing out your count in your function as well? Your code doesnt actually compile on my machine because you dont return an int from your function, even though you've told the compiler your function should return one.

I've quickly knocked this up as an example:


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
#include <iostream>
#include <fstream>
#include <string>

void count_the_word(std::string file, std::string word)
{	
	std::string line("");

	// This opens the file. Make sure you have the FULL path to the 
	// file OR put the file where the code is running
	std::ifstream myfile(file);  
	unsigned long wordcount = 0;
	if (myfile.is_open())
	{
		// geline reads the line into your string. I assuming there is only one word per line?
		// Other your if() below wont work.
		while ( getline (myfile,line) )  
		{
			if(word == "the" || word == "is" || word == "Romeo" || word == "clock" || word == "synchronization")
			{
				wordcount++;
			}
		}
		std::cout << wordcount << std::endl;
		myfile.close();
	}
	else
	{
		std::cout << "Unable to open file"; 
	}

}

int main()
{
	// no need to cout here as you're doing it in your function.
	count_the_word("raj.txt", "the");
	
	return 0;
}
the files are plays from romeo and juilet, and another called clock. . . so i shouldnt make the word == "to anything"? because i want to search for those words.
i tried to run your code on my rig but and error occurs. . .

F:\Users\Julio Alvarez\Desktop\CS 111 pt 1\Problem Set 6\word_search.cpp|12|error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'|
it runs fine on my box (visual studio 2010).

the files are plays from romeo and juilet, and another called clock. . . so i shouldnt make the word == "to anything"? because i want to search for those words.

In that case once you've read your line in you need to split this up into each word because your line might be:
"romeo punches juliet in the face"

but then you're testing for equality with "the" for example on the whole string, which will evaluate to false because

"romeo punches juliet in the face" != "the"
so to read each word individually, maybe i need too?. . . i am lost because the example that my teacher showed us, without using function, when we were barely learning about files, to count words was


1
2
3
4
5
6
7
//file                   //the lines of text
data_store >> line_of_text;  

if (number_of_romeos == "Romeo")
{
      number_of_romeos++;
}
Last edited on
take a look at this:
http://www.cplusplus.com/reference/string/string/find/

You should (in my opinion) read the whole line in, but you can use find to see if that line contains a required string.
please help me with this program... i am really lost. im retard...

my teacher showed us this code to find certain words in a file
this is just the part where the program counts the word.

1
2
3
4
5
num_of_romeos >> line_of_text;
if(num_of_romeos == "Romeo")
{
      num_of_romeos++;
}

i've already explained why your "teachers code" wont find a word in a line of text like that, and provided you with a link explaining how to do that.
I'm not sure what I else I can tell you sorry.
you cannot do the code for me? please. :(
No. I have my own coding to do.
oh ok, thanks for the help anyways.
Topic archived. No new replies allowed.