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
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/
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.
#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);
unsignedlong 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&)'|
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
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++;
}
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.