File stream I/O specifics...

So in my program I have a text file that stores data as follows:

Grant Chase
45.98
Checking

Harry Truman
337.05
Savings

etc...

How exactly do I allow the user enter an EXACT string of text (their name, e.g. "Harry Truman"), search the file for it, and display the line direct AFTER that line?

e.g. Enter your name: "Harry Truman", Your account balance is: $ "337.05"


Second similar question, how would I prompt the same thing (i.e. ask for exact name), search file, and display THAT name as well as the lines directly proceding it?

e.g. Enter your name: "Harry Truman"
"Harry Truman
337.05
Savings"

I'm a little confused about getline and strings in these cases, could someone please show me a template for this?
In this thread a similar problem was exposed: http://www.cplusplus.com/forum/beginner/12765/
Notice that in your case would be better using getline so you can check for a name composed by multiple words
getline works as follows:
1
2
3
4
ifstream file("whatever");
string str;
getline ( file, str );
str == "1st  line of 'file' ";
Last edited on
Topic archived. No new replies allowed.