Thread: How can I use a string stream to do multiple conversions?

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    How can I use a string stream to do multiple conversions?

    I want to use a string stream to convert an int to a string, then a string to an int use the same stringstream. If I declare two like this, it works fine:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    
    int main()
    {
        const int iNum = 56;
        std::string sNum;
        const std::string str("78");
        int num;
        std::stringstream ss;
        
        //stuff int into the stream
        ss<<iNum;
        
        //release the stream as an string
        sNum = ss.str();
        
        //print both results
        
        std::cout<<"iNum: "<<iNum<<std::endl;
        std::cout<<"sNum: "<<sNum<<std::endl;
        
        std::stringstream ss1;
        ss1<<str;
        ss1>>num;
        
        std::cout<<"str: "<<str<<std::endl;
        std::cout<<"num: "<<num<<std::endl;    
        std::cin.ignore();
        std::cin.get();    
    }
    Here is the output:

    Code:
    iNum: 56
    sNum: 56
    str: 78
    num: 78
    This is exactly what I expect. But what if when I am writing the program I don't want to have to declare multiple string streams? Basically this is what I want to do:

    1. declare a string stream object
    2. stuff an integer in it
    3. call the str() function on the object to get the int value as a string
    4. *here is the confusing part. Now i need to clear this stream. I don't know how without declaring a new string stream object*
    5. Then I want to stuff a string into the cleared string stream object
    6. Now I want to put the stream into an int and print the value.

    Below is my attempt at this:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    
    
    int main()
    {
        const int iNum = 56;
        std::string sNum;
        const std::string str("78");
        int num;
        std::stringstream ss;
        
        //stuff int into the stream
        ss<<iNum;
        
        //releaase the stream as an string
        sNum = ss.str();
        
        //print both results
        
        std::cout<<"iNum: "<<iNum<<std::endl;
        std::cout<<"sNum: "<<sNum<<std::endl;
    
        /**********Need to somehow clear the stream here*****/   
    
        ss<<str;
        ss>>num;
        
        std::cout<<"str: "<<str<<std::endl;
        std::cout<<"num: "<<num<<std::endl;    
        std::cin.ignore();
        std::cin.get();
          
    }
    Here is my output:
    Code:
    iNum: 56
    sNum: 56
    str: 78
    num: 5678

    Obviously my stream isn't getting cleared. Inthe above point where I need to clear the stream I have tried everything on this page that looks applicable: http://www.cppreference.com/cppio/index.html


    Code:
    1. ss.clear();
    2. ss.flush();
    3. ss.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    I just don't know what to put there. Any ideas? Thanks.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Never actually tried to do this myself, but ss.str(""); seems to work. See this documentation.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Sweet. Thank you very much.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Or simpler would be to just go straight to ss.str(str);

    If you are doing input, you might also need to call clear() to clear any error bits that are set, like eofbit or failbit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM