Functions: User Chosen File, Word Count w/ missing Main Function

I am just starting out learning C++. I have been working on this program for several days with no success.

I would sincerely appreciate any advice or guidance you could provide.

I apologize in advance for any bad form, and I also thank you in advance for taking the time to help me.

INSTRUCTIONS:

Write a function that determines how many times a word is repeated in a file.

The function must conform to the following:

Name:
count_the_word

Parameters:
string filename - The name of the file that will be searched through.
string word - The word that is being looked for.

Return type:
int - The number of times that word was found in the file that is named filename.

Put this code in a file called word_search.cpp.

Do not include a main function.

THE PRE-SUPPLIED MAIN FUCTION - TO CALL MY FUNCTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "word_search.cpp"
using namespace std;
 
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;
 
}


MY FUNCTION:


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 <string>

string count_the_word(string word; int word_count)

    ifstream word_search;
    // temporarily stores a word as it's read from the file stream
    string word;
    //counts the number of times user input was found in text
    int word_count = 0;
    //counts total number of words
    int total_words = 0;
    //stores the word to search for
    string user_input;
{
    //ask user for the word to search for
        cout << "Enter filename" << endl;
        cin >> user_input;

    //step 2: open file
        word_search.open(user_input.c_str());
    //step 3: process file
    //if file couldn't be opened for any reason...
    if(!word_search)
    {
        //print error
        cout << "File not found" << endl;
        //leave main
        return 0;
    }

    //while we are not at the end of the file
    while(!word_search.eof())
    {
        //read next word
        word_search >> word;
        //remember that we saw this word
        total_words++;
    }

    // step 4: clos (stop using this resounce
        word_search.close();

        cout << "Found " << total_words << " words." << endl;

    }



MY ERROR SCREEN:
In file included from E:\temp\6433.tmp\driver.cpp:2:0:
E:\temp\6433.tmp\/word_search.cpp:4:1: error: 'string' does not name a type
E:\temp\6433.tmp\/word_search.cpp:4:50: error: expected initializer before ')' token
E:\temp\6433.tmp\/word_search.cpp:8:5: error: 'string' does not name a type
E:\temp\6433.tmp\/word_search.cpp:14:5: error: 'string' does not name a type
E:\temp\6433.tmp\/word_search.cpp:15:1: error: expected unqualified-id before '{' token

THANK YOU
Last edited on
It looks like you're not properly scoping the std::string class.
hmmm... I haven't learned that in class yet.

Is there another way to get the desired results?

Thank You.
You have a couple of options, one is to use the scope resolution operator:: each time you use the std::string class. Another option would be to use the using declaration using std::string;. And finally you could use the using namespace std; clause.

Would using namespace std; clause be just a matter of including that in my heading?
string count_the_word(string word; int word_count)

Parameters are separated by commas, not by semicolons.
Good catch, Thanks!

closer now...
Now my errors are:

error: expected initializer before 'ifstream'
error: expected unqualified-id before '{' token

What is this asking for?

I don't know how to interpret this.
In your first post, the brace on line 14 needs to be on line 4.
YES!!!!!
Thank You!

Now only one error.

error: expected unqualified-id before '{' token

I am baffled.

Any advice??
The error is in line 6

Here is the new code:

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

  {
     string count_the_word(string word, int word_count)


    ifstream word_search;
    // temporarily stores a word as it's read from the file stream
    string word;
    //counts the number of times user input was found in text
    int word_count = 0;
    //counts total number of words
    int total_words = 0;
    //stores the word to search for
    string user_input;


    //ask user for the word to search for
        cout << "Enter filename" << endl;
        cin >> user_input;

    //step 2: open file
        word_search.open(user_input.c_str());
    //step 3: process file
    //if file couldn't be opened for any reason...
    if(!word_search)
    {
        //print error
        cout << "File not found" << endl;
        //leave main
        return 0;
    }

    //while we are not at the end of the file
    while(!word_search.eof())
    {
        //read next word
        word_search >> word;
        //remember that we saw this word
        total_words++;
    }

    // step 4: clos (stop using this resounce
        word_search.close();

        cout << "Found " << total_words << " words." << endl;

    }





This program gets errors on line 1, 2, & 3 - saying it is nested too deeply
also
line 5 gets expected unqualified-id before '{' token

Any ideas why?

This is due tonight at midnight, and I am going crazy from this program.

Thanks.


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
#include <iostream>
#include "word_search.cpp"
#include <string>
using namespace std;
{



    string count_the_word(string word, string word_search)


    ifstream word_search;
    // temporarily stores a word as it's read from the file stream
    string word;
    //counts the number of times user input was found in text
    int word_count = 0;
    //counts total number of words
    int total_words = 0;
    //stores the word to search for
    string user_input;


    //ask user for the word to search for
        cout << "Enter filename" << endl;
        cin >> user_input;

    //step 2: open file
        word_search.open(user_input.c_str());
    //step 3: process file
    //if file couldn't be opened for any reason...
    if(!word_search)
    {
        //print error
        cout << "File not found" << endl;
        //leave main
        return 0;
    }

    //while we are not at the end of the file
    while(!word_search.eof())
    {
        //read next word
        word_search >> word;
        //remember that we saw this word
        total_words++;
    }

    // step 4: clos (stop using this resounce
        word_search.close();

        cout << "Found " << total_words << " words." << endl;

    }

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;

}
Look at the very first example in the tutorial on this page:
http://www.cplusplus.com/doc/tutorial/functions/

Do you see how it fits together, and how this relates to your code..


nested too deeply
Are you trying to get the file to include itself?
Last edited on
The brace on line 5 needs to be on line 10

So, since the return will be an integer, then I should use int as my type.

In the parameters, I have word and word_search, so both should be string.

About getting the file to include itself.. I don't know. I don't think so. I just

need to be able to open two files, user inputted (raj.txt and clock.txt), and

count the user inputted word.

Sorry if I am not being clear.

Thank You.
Topic archived. No new replies allowed.