Hello, I'm very, very new to C++ and I'm taking computer science course right now for fun. I really want to learn C++ but I'm having a lot of trouble with this new homework assignment and I'm hoping I just take it one step at a time so I completely understand it.
I've read a lot of the topic here to help me out, but most topics that fall under this category are too complex for my understanding.
The entire problem involves reading one text file with "search words" in it and then counting the number of times these search words show up in a second text file.
From what I know of C++, I want to start by taking all of the words from the search words file (words.txt) and place them into an array which will be used for later (I can't open the search file more than once). I know how to open the file from a program I found online:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("word.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
I've been trying my best to work with a for loop nested in the while loop, but I just can't figure out how to do it. I get an error with each effort. My attempts at the for loop are placed right after the getline (myfile,line); and look something like this:
for(i=0;i<line.length();i++){
myfile >> word[i];
}
I delcare word as a string so it looks like:
string word[1000];
I don't even know if that is possible, but it my compiler doesn't appreciate it very much. Is there a better way of doing this? Or even just another way I could attack this problem so I can continue on?
Also, there are actually only 8 words in the file, but I can't get it to work with using the numbers 0 and 7 even.
Any help would be greatly appreciated.
Thank you.