Author Topic: [C++] converting a float to a string.  (Read 7583 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
[C++] converting a float to a string.
« on: November 29, 2009 »
I've seen on google search allready about sorta doing this but it's using cout ( print to console ) what im really after is converting a float ( my fps calc ) into a string ready for use with a print_bitmap_text routine.

Cheers and many thanks,
from Clyde.
xxxx
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: [C++] converting a float to a string.
« Reply #1 on: November 29, 2009 »
hey dude check this out,

Code: [Select]
#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a %d char long string\n",buffer,n);
  return 0;
}

now what this does is use sprintf to convert the ints into a string format to use floats just change the %d to %f and there are a lot of other diffrent switches that can be used you can even format the buffer to contain floats and ints and whatever else.

then instead of using printf just dump the buffer into whatever you use to print.

[edit forgot to say n contains string length but you dont have to assign sprintf to anything if you dont want.]
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: [C++] converting a float to a string.
« Reply #2 on: December 02, 2009 »
your welcome mate!  :)
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] converting a float to a string.
« Reply #3 on: December 02, 2009 »
Thanks Ninogenio, I really appreciate it at the moment I haven't tried this yet looks cool. Im trying to visualize that in draw_text( char *message, x, y );
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] converting a float to a string.
« Reply #4 on: December 02, 2009 »
You can do
Code: [Select]
float a=1.2345f;
char tmp[50];
sprintf(tmp, "%f", a)
draw_text(tmp, x, y);
Your next question is going to be "how do I make it only have 2 decimal places"
Code: [Select]
sprintf(tmp, "%.2f", a)

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] converting a float to a string.
« Reply #5 on: December 02, 2009 »
I see so that sprintf is printing / feeding into the string.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Re: [C++] converting a float to a string.
« Reply #6 on: December 02, 2009 »
If you really want C++ stuff (I mean no sprintf function from the C library), you can use the streamstring (http://www.cplusplus.com/reference/iostream/stringstream/).

If I remember well it will be something like this:

Code: [Select]
std::streamstring ss;
float myFloat = 3.14f; // Pure random float
ss << myFloat;
// And you can get the string like this:
ss.str();
// Or the char* like this:
ss.str().c_str();

The demoscene will never die, never!

Offline James_wright_1990

  • C= 64
  • **
  • Posts: 48
  • Karma: 4
    • View Profile
Re: [C++] converting a float to a string.
« Reply #7 on: December 02, 2009 »
I recenty had the same problem and for some reason my lecturers don't like us using stdio.h(I dont know why) so I also use stringstream the code looks like:
Code: [Select]
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main (int argc, char **argv)
{
float f = 3.33333f;

ostringstream os;
os << f;

string s = os.str();

cout << s << endl;

             int k;
cin >> k; // pauses the program

return 0;
}

This then prints to the console but you dont have to this is just the code I used whilst trying to figure out how to do it. If  you need to limit the decimal points I used this code :

Code: [Select]
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main (int argc, char **argv)
{
float f = 3.33333;

ostringstream os;
os << dec << setprecision(4) << f;

string s = os.str();

cout << s << endl;

             int k;
cin >> k; // pauses the program

return 0;
}

Not sure how good this code is as a solution but it seams to be working for me.

James.
"if debugging is the proccess of removing bugs from software then that means that programming must be the proccess of putting them in" my favourite programming quote.

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Re: [C++] converting a float to a string.
« Reply #8 on: December 02, 2009 »
Your code is correct.

Just that you don't need to use another variable for the string, to be able to display it.

I mean
Code: [Select]
std::cout << os.str() << std::endl;
Will do the job, without using the string s.
Lecturers can dislike old C functions because they does not launch exceptions, when they are falling, or stuff like this.
The demoscene will never die, never!

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] converting a float to a string.
« Reply #9 on: December 03, 2009 »
sprintf is totally unsafe, as you have no way of making sure the workspace you give it is going to be big enough (the cause of many stack/heap overflow exploitz), that's why I made it 50, but I pulled that number out of my arse.

So there is either the C++ stringstream route or the Safer C library (which is part of Visual Studio 2005 and later) which has sprintf_s() where one of the parameters is the output buffer size.

I think some people also have an aversion to mixing C++ includes and C .h includes, though it's just stylistic and should always be OK - as James shows, all the C functions appear in the std namespace.

Jim
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Re: [C++] converting a float to a string.
« Reply #10 on: December 03, 2009 »
I think the libc ( not VS "crappy" implementation ) has the snprintf (with a n somewhere in the same) where you have to put a size array :) to avoid all problems ( like the secure version )
The demoscene will never die, never!