Reading from a text file

May 3, 2011 at 7:43pm
hi, im writing a program that asks how you are feeling, and instead of hard coding all of the keywords (eg: "great") and using

found=x.find(a);
if (found>0)
{
cout << "ok.\n";
goto end1; \\to go to the end of the block
}

to find it in the string, i would like to know how i could write all of the words in a text file and then if one of the words in the file is found in the string it would give the response. basicly to replace the found thingy with a search in a text file.

thanks, izacy
May 3, 2011 at 7:49pm
So you want to know how to perform file input and output? Start here.
http://www.cplusplus.com/doc/tutorial/files/

Then you will need to think about the format in which to write your data to the file. The simplest would just be to write a string per line.
May 3, 2011 at 7:54pm
i have read the whole manual, but i was hoping for some more in-depth help, with things such as how i would include this into an if/else control structure.
May 3, 2011 at 9:34pm
So you read an input string from the user. Then you loop through the file, reading each string key in turn and use an if statement to check if the user input equals the file string. If it does then you need to grab your output string from the file as well.

If you show us what you've got so far, it would make it easier for us to help you improve your code.
May 3, 2011 at 9:45pm
@OP post all your code (formatted with code tags please) and let us know where you think your problems are
May 4, 2011 at 12:20pm
So you read an input string from the user. Then you loop through the file, reading each string key in turn and use an if statement to check if the user input equals the file string. If it does then you need to grab your output string from the file as well.

If you show us what you've got so far, it would make it easier for us to help you improve your code.


how would i do that? ill post all of the code later i dont have access too it right now =(
May 4, 2011 at 3:14pm
Once you have access to it, just copy and paste it into the reply box, making sure you enclose it in: [code][/code] tags.
Last edited on May 4, 2011 at 3:14pm
Topic archived. No new replies allowed.