God I hate C++

lukatmyshu

Senior member
Aug 22, 2001
483
1
0
Using C++ how would I make a string have the same value as a an float ... so if I have a float x = .5 how do I get a string to have the same value?
 

eklass

Golden Member
Mar 19, 2001
1,218
0
0
if you just want to convert the value and not output it, look in your class book or google for "typecasting" or "casting" or "cast"
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
stringstream ss;

float x = .5;

ss << setf(1) << x;

string s = ss.c_str();

// This is close, but may be off a little since I don't have my book in front of me.
 

GermyBoy

Banned
Jun 5, 2001
3,524
0
0
You hate it because the string class << doesn't override having a float in the picture? Jeez...

I wrote a program which goes through and downloads pictures for comics every day of the year, until they changed it to random numbers, instead of just the dates. That was the life.
 

PowerMacG5

Diamond Member
Apr 14, 2002
7,701
0
0
Just use string streams.
Here's a quick and dirty example:


#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
stringstream x;
float y = 1e29;
string z;
x << y;
x >> z;
cout << z << endl;
return 0;
}