Ok so I need to read from a file that the user will enter in. They then enter a search string to look for in the file. All I am trying to do here is just take it step by step and make sure everything works. I can get it to read the file name and print that out, I can get it to read the string name and print that out- but when I try to print out the "words" in the file- it will only print out the last word in my file. EX: file is named filename
contents of file are Hi my name is Jenna
search string is my
will print: filename my Jenna
How can i get it to print filename my Hi my name is Jenna ????
here is my code:
I know if has something to do with name.get(ch) in thw while loop because that will only get one word but why is it getting the last word as opposed to the first word and also how can i make it get the whole content of the file?Code:int main() { string filename; string search; string words;; char ch; ifstream name; cout << "Please enter the name of the input file" << endl; cin >> filename; name.open(filename.c_str()); cout << "Please enter your search string" << endl; cin >> search; name >> words; while(!name.eof()) { name.get(ch); name >> words; } cout << filename << " " << search << " " << words << endl; }
Thanks!