Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By laserlight

StringStream

This is a discussion on StringStream within the C++ Programming forums, part of the General Programming Boards category; Ah, after I read carefully about stringstream. I have some code below: Code: int main(){ //string to Int /* ******************************************** ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question StringStream

    Ah, after I read carefully about stringstream. I have some code below:
    Code:
    int main(){
    //string to Int
    /* ******************************************** */
        stringstream ss;
        printf("FIRST TEST\n");
        string strNum;
        cin>>strNum;
        ss<<strNum;
        int a;
        ss>>a;
        a=a+1;
        cout<<a<<endl;  //NO PROBLEM HERE
    
    
        //int to String
    /* ******************************************* */
        printf("SECOND TEST\n");
        int num;
        cin>>num;
        ss<<num;
        string str;
        ss>>str;
        str=str+" IS A NUMBER";
        cout<<str<<endl;   //PROBLEM: it just print "IS A NUMBER"
    
    
     /* ****************************************** */
        printf("THIRD TEST\n");
        stringstream ss_2;  string str_2;
        cin>>num;
        ss_2<<num;
        ss_2>>str_2;
        str_2=str_2+" IS A NUMBER";
        cout<<str_2<<endl;   //NORMAL AGAIN: it will print: "<num> IS A NUMBER"
        /* ****************** */
        return 0;
    }
    //Note: PROBLEM mean I don't want like that, not something's wrong

    Uhm, in above code, at second test, if I use ss stringstream again, ss>>str will not put flow in ss into str. and the problem will be solved if I use another variable(example: ss_2). So, who can explain for me please

    thanks

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Try this:
    Code:
    #include <iostream>
    #include <sstream>
    
    int main(void){
    
    	int num;
    	std::stringstream ss;
    	std::string output;
    
    	std::cout << "Enter a number: "; //note NO PRINTF
    	std::cin >> num;
    
    	ss << num; //place num into stringstream
    	ss >> output; //place contents of stringstream into string object
    
    	output += " is a number"; //append string object
    	std::cout << output << std::endl;
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    yes, I have tried it, But It meets same problem with me . this is my code, building from your code:
    Code:
    int main(){
    int num;
        stringstream ss;
        string output;
        cout<<"Enter a number:";
        cin>>num;
        ss<<num;
        ss>>output;
        output+=" is a number";
        cout<<output<<endl;
    
    
    
        /* ********************** */
        cout<<"Enter a number again:";
        cin>>num;
        ss<<num;
        ss>>output;
        output+=" is a number";
        return(0);
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    If you want to reuse the stream then, before the line "ss << num;", you need a line "ss.clear();"
    hqt likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    So, who can explain for me please
    I would but I cannot understand your post.

  6. #6
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Just FYI, template functions can make arbitrary conversions fairly trivial:

    Code:
    #include <sstream>
    #include <cctype>
    #include <typeinfo>
    
    template <typename Dst, typename Src>
    bool iocpy(Dst& dst, const Src& src)
    {
    	bool success = true;
    	std::stringstream io;
    	if(!(io << src) || !(io >> dst))
    		success = false;
    	else
    	{
    		char ch;
    		while(io.get(ch))
    			if(!std::isspace(ch))
    				success = false;
    	}
    	if(!success)
    		dst = Dst();
    	return success;
    }
    
    template <typename Dst, typename Src>
    Dst iocpy(const Src& src)
    {
    	Dst dst;
    	if(!iocpy(dst, src))
    		throw std::bad_cast();
    	return dst;
    }
    
    #include <iostream>
    
    int main(int argc, char** argv)
    {
    	using namespace std;
    	int sum = 0;
    	for(int a = 1; a < argc; ++a)
    	{
    		try
    		{
    			sum += iocpy<int>(argv[a]);
    		}
    		catch(bad_cast&)
    		{	
    			cout << "Sorry, '" << argv[a] << "' is not an integer!" << endl;
    		}			
    	}	
    	cout << "Sum: " << sum << endl;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by gardhr
    Just FYI, template functions can make arbitrary conversions fairly trivial:
    If you have Boost, I suggest using boost::lexical_cast, though it is less tolerant of whitespace. Incidentally, the first function template could also have been written as:
    Code:
    template <typename Dst, typename Src>
    bool iocpy(Dst& dst, const Src& src)
    {
        std::stringstream io;
        bool success = (io << src) && (io >> dst >> std::ws) && io.eof();
        if (!success)
        {
            dst = Dst();
        }
        return success;
    }
    gardhr likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    I have use ss.clear() and nothing happened,yet.
    @ laserlight and gardhg: I'm still reading your posts (because It's may strange for me, so I will read slowly), but can you give me STD library C++ solution, please. Because, I just can use it.(for contest purpose).
    Of course, many solutions get, many ways to improve my knowledge

    thanks
    Last edited by hqt; 09-18-2011 at 01:43 AM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    <sigh> It would also help if, in your last code fragment (post 3), you actually write output to std::cout so you can see what is happening. Writing to an object named "output" does not magically ensure anything is written to a file, cout, etc.

    I am assuming that you have recompiled and linked your code, so you are actually executing it .....
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    @grumpy: your answer maybe useless to me, until I read my code again, and, :"> I haven't printed <string>output to monitor,yet.
    And after that, I see that ss.clear() have solve my question completely.(at post 1 and post 3, too)

    Again, I'm sorry sorry so much for my fault

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    By the way, one side effect of wrapping the conversion code in a function (template) is that the stringstream is used once for the conversion. This might be a little wasteful, but it means that you do not need to clear the state of the stream and reset it with an empty string in order to reuse the stringstream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream fun
    By Prediluted in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2011, 10:14 AM
  2. stringstream help
    By Airick92 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2011, 10:28 AM
  3. stringstream Help...
    By neogst in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2011, 09:23 AM
  4. stringstream
    By jk1998 in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2007, 06:35 PM
  5. std::stringstream
    By Magos in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2004, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21