I really need your expert advices on my following assignment.
I intend to compile a C++ program to read a text file containing a lot of text information.
I need to find a keyword in this text file and output the word that is next to it.
For example, the whole text string is (...the game is pokemon and etc).
Which function in C++ can i use to search the word "the game" and which function can i use to extract the word the to it "pokemon" and Cout it to my console.
So far i have look through strtok function but i don't think it works.
As I am really new to C++, I will need your advice to help me focus on the relevant function for this extraction.
Thank you very much in advance to anyone who are able to advice me on this. =)
string word_to_find = "foo";
ifstream file ( "textfile.txt" );
string temp;
while ( file.good() )
{
file >> temp; // read word from file
if ( temp == word_to_find ) // check the word
{
file >> temp; // read next word
// here you may want to check if the word was read fine
cout << "the word following " << word_to_find << " is " << temp; // display stuff
// if you want to continue the loop do nothing, if you want to stop it add break;
}
}
Hmmm... I will try this out! I been trying to figure out on this for weeks. thanks so much bazzy! With people like you around, my nightmares with C++ will be lessen! If only my lecturer is as supportive as you.
I have play around and testing with your example and also read out on strings funtions, but I am still figuring out on how to find more than one keyword together and displaying more than one word from the example that you given me. I try to add "they" + "are"; but they show me error.
Can you advice more on the strings function for this? I have tried reading the C++ for dummies but still having problems understanding strings.
// #include <list>
// ... same declarations as before ...
list<string> words_to_find;
words_to_find.push_back("they"); // add a word to find
words_to_find.push_back("are");
list<string>::iterator i = words_to_find.begin();
while( file.good() )
{
file >> temp;
if ( *i != temp ) i = words_to_find.begin(); // restart checking if a word doesn't match to the sequence stored in the list
if ( i == words_to_find.end() ) // all the words were right
{
i = words_to_find.begin(); // restart
cout << temp; // show next word
}
++i;
}
Wow Bazzy, this is something very new to me. I am not that fimiliar using list. Nevertherless, I need to read on this list string function and try it and test once again on my coding.
Thank you once more Bazzy. I will try to digest this and get back to u if i can't understand on the coding that you have shown.
I'm not so sure; if he hasn't learnt to use pointers yet in class; his teacher might not want him to use them. Then again if he shows he has done some learning in his spare time... I don't know...
Flubber; the best way to learn is to do. Rewrite all of what Bazzy wrote. Then, if you can rewrite all the comments correctly, without copying his, you know you've learnt how the program works.
Just make sure it doesn't look like exactly like Bazzy's code, but does the same thing. And that you know why. Other wise it might just be a coincidence.
Actually in my class, the requirement is to know C++. But I dont know why I have already starting this module with C++. Therefore I have no choice but to digest all the C++ basic and try to implement the solution in less than 1 month.
Thankfully to Bazzy, he help me to focus on the important functions for my assignments so that i can able to read, understand and implement with my own understandable codes.
I am left with 1 week till I got to submit this. I am glad that someone like Bazzy able to guide and advice me. Unlike my lecturer who seems to give us a "dont care attitude."
In the end I will want to understand what I am implementing so that I will not lose anything in my future C++ modules.
getline reads all the characters until a delimiting character is found ( for default '\n' ) if you want to show all the words present until the end of line/sentence you can use it. If you want to display the next 2/3 words, just read them using >> and then output them with cout
I have managed to get the content of all the text in the file using the getline function as per below. But I am figuring out how to either use the >> operator as your first example or using the list function to display more than one words for my cout.
I am now concentrating on using 1 keywords for my extraction.