Question: Write a program that reads 1 0 , 0 0 0 words into an array of strings; the List of 1 0 , 0 0

Write a program that reads 10,000 words into an array of strings; the "List of 10,000 Random Words" file is on Moodle. The program will then read a second file (the list of "Search Words" is also on Moodle) that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list. Do not use an array for the second file, process the words as they are read. Note that some of these "words" might have spaces; handle your input accordingly.
I keep getting 348 words, but that is not right. What part of my code is incorrect? The Int main() is underlined in green sayingthe bytes of stake exceeds the limit. How do I make it bigger?
Here is my code:
#include
#include
#include
using namespace std;
const int NUM_WORDS =10000; // number of words in the first array
int main(){
// declare and initialize the first array with the 1000 words
string words[NUM_WORDS];
ifstream infile("10000RandomWords.txt");
if (!infile.is_open()){
cout << "Error: could not open 1000RandomWords.txt"<< endl;
return 0;
}
for (int i =0; i < NUM_WORDS; i++){
infile >> words[i];
}
infile.close();
// open the second file and read the words one by one
ifstream searchfile("Search Word.txt");
if (!searchfile.is_open()){
cout << "Error: could not open SearchWords.txt"<< endl;
return 0;
}
string searchword;
int count =0; // counter for the number of words found
while (searchfile >> searchword)
{
// remove leading and trailing white space characters from searchword
searchword.erase(0, searchword.find_first_not_of("\t"));
searchword.erase(searchword.find_last_not_of("\t")+1);
// search the first array for the current search word
for (int i =0; i < NUM_WORDS; i++){
if (searchword == words[i]){
// if the search word is found, increment the counter
count++;
// break; // exit the loop
}
}
}
searchfile.close();
// print the number of words found
cout << "Number of words found: "<< count << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!