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