Nov 30, 2014 at 3:33am UTC
The >> and << operators separate input by whitespace (spaces, tabs, newlines, etc).
You can use getline() instead.
Assuming my file f has
written in it,
I can put it into one string by doing:
1 2
std::string name_line;
getline(f, name_line); // name_line will now contain "firstName lastName"
Let me know if you need help applying that.
__________________________
Edit: Also, if you know you'll always have a first and last name in your file,
you can concatenate two strings:
1 2 3 4
string first_name;
string last_name;
f >> first_name >> last_name;
string full_name = first_name + " " + last_name;
Last edited on Nov 30, 2014 at 3:37am UTC
Nov 30, 2014 at 3:45am UTC
In your while loop, you still have it trying to do the >> operator on the u1.name property.
What does your file look like? I can show a better explanation if I know the file format.
Last edited on Nov 30, 2014 at 3:45am UTC
Nov 30, 2014 at 3:46am UTC
My txt file looks like this, but with actual words and numbers
firstName lastName
number
PIN
Nov 30, 2014 at 3:51am UTC
If that's the only thing in your file, a loop isn't really necessary.
If you want to use getline, try this:
Note that if you use getline, "firstName lastName" must be the first line of your file in this situation.
1 2 3 4 5 6
if (fin)
{
getline(fin, u1.name);
fin >> u1.phoneNumber >> u1.servicePin;
//...cout'ing
}
Alternative, string concatenation:
1 2 3 4 5 6 7 8
if (fin)
{
std::string temp_first;
std::string temp_last;
fin >> temp_first >> temp_last >> u1.phoneNumber >> u1.servicePin;
u1.name = temp_first + " " + temp_last;
//cout
}
Edit: Didn't see your post, glad you got it :)
Last edited on Nov 30, 2014 at 4:00am UTC