Character String

Pages: 12
Is this the proper way to put a floating number in an array?

char s[25];
sprintf(s,"%4.1f",winpercent*100.0);
Not really, that would be the C way.

The C++ way looks like this:
1
2
3
stringstream ss;
ss << fixed << setprecision(1) << winpercent*100.0;
string s=ss.str();
does the way i did it work properly tho?

thanks.
Only if winpercent is below a certain size, otherwise you'll have a buffer overflow.
winpercent is always between 0 and 1, so by size do u mean if it has more than 25 digits beyond the decimal point (since s is of size 25)?

and if so, your way resolves that issue?
Yes and yes.
i added

#include <sstream>
#include <iostream>

to my visual c++ file, yet i get the error

'stringstream' : undeclared identifier

what am i missing?
You need to fully qualify the name: std::stringstream

Or insert an using declaration before using the name: using std::stringstream;
i'm getting the following error:

'setprecision' : is not a member of 'std'

on this line:

ss << std::fixed << std::setprecision(1) << tempy;

setprecision is a member, so what did i do wrong?
#include <iomanip>
still not there yet. i get an error that "s" is now of wrong type when i try to use TextOut.

std::stringstream ss;
ss << std::fixed << std::setprecision(1) << tempy;
std::string s;
s=ss.str();
dc.TextOut(x,y,s,4);

originally, i had s as

char s[25].

Try dc.TextOut(x,y,s.c_str(),4);
almost there. ;)

i do this operation in a loop:

std::stringstream ss;
std::string s;
for(z=1;z<10;z++){
ss << std::fixed << std::setprecision(1) << winpercent[z]*100.0;
s=ss.str();
dc.TextOut(x,y,s.c_str(),4);
}

s does not change beyond the first loop. do i need to reset it somehow?
Actually, you just need to reset ss every loop.
does this code require any release of memory in terms of memory leaks?

thanks.
No. Typically you'll only need to release memory manually if you use new or malloc().
i'm getting an error when the number to be texted is below 10, like 9.8. i guess by specifying "4" in the text out it tries to add the hundreth numeral, which on-screen comes out as a square box. how can i make sure it only texts to the tenth of a point?
By passing the real length of the string.
Replace 4 by s.length().
but the real length could vary hugely, as in 1/6. isn't there a comparable method to %4.1f using this method so i know it will only go to tenths?
This part: ss << std::fixed << std::setprecision(1) << winpercent[z]*100.0;

is like the "4.1" stuff. After you have done that, the string contains only the parts parts you wanted.
Pages: 12