Doing this:
Code:
temp.name.clear();
is the same as doing this:
Code:
temp.name = "";
well all i know is that when i had my code like this... it didnt work right... and it displayed the whole string stream.. not the string i wanted...
Code:
while(getline(fFile,temp.name))
{
getline(fFile,temp.cnumber);
getline(fFile,temp.hnumber);
getline(fFile,temp.address);
s.str(temp.name);
//temp.name.clear();
s >> heading >> first >> last;
temp.name=first+" "+last;
s.str("");
s.str(temp.cnumber);
//temp.cnumber.clear();
s >> heading >> temp.cnumber;
s.str("");
s.str(temp.hnumber);
//temp.hnumber.clear();
s >> heading >> temp.hnumber;
s.str("");
s.str(temp.address);
//temp.address.clear();
s >> heading >> temp.address;
s.str("");
vEntries.push_back(temp);
when i had my code like this, not clearing the string, it didn't display properly
(it would do this...
Name: Name: Joe Dirt
and with the clear() it does this...
Name: Joe Dirt)
... im guessing that the >> operator, in this case, is the same as a += not a =...although please correct me if i am wrong.. once i again, i am very new at string streams...
and in your program it has absolutely no effect since you do this:
Code:
temp.name=first+" "+last;
Well, I was just doing Joe(space)Dirt... how is this wrong? Wouldn't it just be JoeDirt if i didn't include the space?
Are you aware that there is a distinction between a string and a stream? I don't see anywhere in your code where you are clearing a stream.
When you read from a file, if any errors occur while you are reading from the file, an error flag is set which will prevent you from reading from the file anymore. Reaching eof is considered an error, and it causes an error flag to be set. Similarly, if you read the end of a stringstream, an error flag is set. You can use a stream's clear() function to reset all the error flags for the stream and then attempt to read from the stream again.
Oh, i was unaware of using clear() for string streams.. i went to this website and it said you could use s.str(""); ...
Here it is!