File I/O

So I was able to do part of a program, and only got stuck on one part. The program uses text input file to return things such as age and gender. However, The only thing that I couldn't get any output for was string name. For example, the input file had Joe Bob and the compiler returned your name is, but not Joe Bob. This is a hw questions so please hints only. I included <string> and all that other stuff. Like my book says, "it should read a string value from one line of an input file."

This is what I have:

string name;
cout << " Your name is " << name << endl;
getline(fin, name);

Again hints only and thanks in advance!
Last edited on
note: I am using win 7 notepad as an editor, VC++ 2010 express as a compiler, and am using a PC.
Last edited on
It is not clear your hw for me. I guess you have a file that contains some info:

Little Joe
Joe Bob

Moreover you need to get a name and you have to search this name in the file and print the other things besides name, doesn't it?

First you should read the name, secondly you should use the getline on every line in the file because of searching the particular name. You can do it in a loop. If you find it, you can read more data.

Some help:

read a string as a name

read a line from a file

You can compare both string with assignment operator.

xXmusicmanXx wrote:
string name; 
cout << " Your name is " << name << endl;
getline(fin, name);

it looks like you are outputting the variable name BEFORE you put a value in it when you use the getline(cin, name);
Thanks for the help!
@screw: those links were helpful :)
@Maese909: Yea in the end I think that is what was wrong and the first link screw posted verified that.

Thanks again!
Topic archived. No new replies allowed.