Thread: i need help with string streams

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    your right... but i have to clear the string for some reason for it to work
    Doing this:
    Code:
    temp.name.clear();
    is the same as doing this:
    Code:
    temp.name = "";
    and in your program it has absolutely no effect since you do this:
    Code:
    temp.name=first+" "+last;
    What you are doing can be boiled down to this:
    Code:
    string str = "start text";
    str = "";
    str = "new text";
    which has the same effect as:
    Code:
    string str = "start text";
    str = "new text";
    So, no, you don't have use clear() to erase the string for your code to work.

    so the stream works once...but it should work multiple times because i clear it after i am done each time
    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.
    Last edited by 7stud; 05-26-2006 at 06:53 PM.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM