-2

I want to copy the contents of a float to a string in C++. This doesn't work.

#include <iostream>
#include <sstream>

using namespace std;

int main() {
   float ans = getFloat();
   stringstream ss;
   string strAns;
   ss >> ans;
   strAns = ss.str();
   cout << strAns << "\n";     // displays "0"
   return 0;
}

How do I do this?

Charles
  • 50,943
  • 13
  • 104
  • 142
ofey
  • 423
  • 1
  • 7
  • 17

6 Answers6

5

I think

 ss>>ans;

should be

 ss<<ans;

Edit: As James Kanze noted, you are better off using std::ostringstream instead of std::stringstream as you are not using the bidirectional functionality of the first one. This way the compiler would also throw an error that you extracting ans from the string instead of inserting it into the string.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user23127
  • 827
  • 10
  • 21
  • 5
    And of course, if he'd used `std::ostringstream`, instead of the bidirectional `std::stringstream`, the compiler would have told him. – James Kanze Feb 04 '13 at 13:51
2

ss << ans; instead of ss >> ans and it will work

To work with stringstreams, you have to use the PUT TO operator( << ), with an object on the right hand side. That will convert the operator to a string(if the operator is defined for the particular type)(this operator<< is already defined for a stringstream object with float object).

Then, convert the string stream to a string.. and you will have successfully converted the object to string.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
1

As the other answers show, it should be ss << ans, since << is used for ostreams and >> is used for istreams.

If you want just to print the float to cout, you can of course avoid the detour and just write std::cout << ans;, but I guess you want to use the string otherwise.

You should however be aware of the simplifications provided by Boost's and C++11's libraries:

#include <iostream>
#include <string> //for std::string and std::to_string
#include <boost/lexical_cast.hpp> 

using namesapce std;
int main() {
   float ans=getFloat();
   string strAns1 = boost::lexical_cast<string>(ans); //boost way

   auto strAns2 = std::to_string(ans); //C++11 way

   cout << "boost: " << strAns1 << "\n"
        << "C++11: " << strAns2 << "\n";
}
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • Which one would you recommend? I am assuming that `boost` is faster and that `c++11` is the standard way? – SamGamgee Feb 04 '13 at 14:15
  • Of course the results stay in the stream between calls, so if you define it outside the loop, the contents will remain. So you have to either reset the string inside the stream in each iteration, e.g. using `ss.str("")`, or you could define the stream inside the loop, so you get a fresh untouched stream in each iteration. Purists and clean-code-gurus will say the latter (use variables in a restricted scope as possible); performance-pessimists and premture-optimizers will say the former, to do the stringstream initialization just once and not in each loop. – Arne Mertz Feb 04 '13 at 14:35
  • 1
    @SamGamgee I'd recommend the C++11 way, since **1** it's standard, **2** you have to include `` anyways, and **3** the stdandard library can at least be as fast as boost since the implementation may resort to some implementation defined magic that boost has no access to. – Arne Mertz Feb 04 '13 at 14:38
0

You are using wrong operator:

#include <iostream>
#include <sstream>
using namespace std;
int main() {
   float ans=getFloat();
   stringstream ss;
   string strAns;
   ss << ans;
   strAns=ss.str();
   cout<<strAns<<"\n";     // displays "0"
   return 0;
}
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
0

Just one line wrong here by the look of it. You need to stream the float into the stringsteram like this:

ss << ans;
Component 10
  • 10,247
  • 7
  • 47
  • 64
0

Use

strAns = std::to_string(ans);
alanw
  • 645
  • 6
  • 10