Functions doing a word_search

I am writing a program that determines how many times a word is repeated in a file without a main function. I almost have it correct but it doesn't exit after it finds how many of the last word and it just loops.

Here is my coding
int count_the_word(string filename, string word)
{
ifstream word_search;
int number_the = 0
int number_is = 0
int number_of_romeos = 0
int number_case = 0
string user_input;

cout << "Enter the name of the file" << endl;
cin >> user_input;

word_search.open(user_input.c_str());

if (!word_search)
{
cout << "Couldn't open file" << endl;
return 0;
}
while (!word_search.eof())
{
word_search >> word;
if (word == "the")
{
number_the++;
}
else if(word == "is")
{
number_is++;
}
else if(word == "Romeo")
{
number_of_romeos++;
}
else if(word == "case")
{
number_case++;
}
}
word_search.close();

cout << number_the << endl;
cout << number_is << endl;
cout << number_of_romeos << endl;
cout << number_case << endl;
}

there is also two more lines from a different txt file i just havn't coded them yet.

the main function that is used to test it is as follows
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;

it works like its supposed to but like i earlier mentioned it outputs another number after it finds how many "case" are in the file and then it loops asking for a filename.
Are you new to c++ ?

I don't really know what you want... but I think u want to count the number a specific word inside a file .... the filename is given trough a parameter ... but why would want to input the filename again...,, then it`s a strange that u cout a function that doesn`t retrun... it should return a value but it didn't ..
I am new to C++ and im trying to write this function without userinput and without a main function. I also havn't figured out how to return it. but like i mentioned earlier the main function is used to test it.
Topic archived. No new replies allowed.