Menu

istringstream

Githlar
2005-02-28
2012-09-26
  • Githlar

    Githlar - 2005-02-28

    I want to use istringstream to print out a large number. Can somebody give me an example of how to use it? I've looked at some examples, but they seem really complicated. Can somebody give me a simple example of how I would go about doing this?

     
    • Paul van Zelst

      Paul van Zelst - 2005-03-01

      Really complicated? I wonder what kind of examples you've been looking at then :)
      Using string streams is just as easy as using other kinds of streams... like cout for example.
      Oh and you can't use an istringstream (read "input string stream") for output, obviously. You'll need an ostringstream ("output string stream").

      ostringstream out;
      out << my_large_number;

      Unless you mean large numbers coming from some arbitrary large number library... in that case you'll need to look at the documentation for that library.

      Paul

       
    • Githlar

      Githlar - 2005-03-06

      Thanks :)

       
    • Githlar

      Githlar - 2005-03-10

      I try to ostringstream a double, but it won't print it out. Here is the piece of my code:

      include <sstream>

      ...
      double totalcombos = 1000000000;
      ..
      cout << "Total combinations: ";
      ostringstream outstream;
      outstream << totalcombos;
      cout << "\n";

      Why won't it print totalcombos to the screen?

       
      • Paul van Zelst

        Paul van Zelst - 2005-03-10

        Why would it? You are using the stream output operator on a STRING stream... the name kind of gives it away, huh? It doesn't print to the screen, it prints to a string.

        This would fix it:

        cout << "Total combinations: ";
        ostringstream outstream;
        outstream << totalcombos;
        cout << outstream.str() << "\n";

        And so would this:

        cout << "Total combinations: " << totalcombos << "\n";;

        Paul

         
    • Nobody/Anonymous

      Well, I want to print out the number without it going into scientific notation. I wasn't putting the .str() onto it. Thanks.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.