Results 1 to 21 of 21

Thread: convert float to string

  1. #1

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919

    convert float to string

    Code:
    	char *output="fps=";
    	char tmp[20];
    
    
    	strcat(output,itoa((int)fps,tmp,2));
    that's my code, but my program always locks up at the "strcat" section. fps is a float, which i typecasted cuz of "possible loss of data"...see any problems?

    I'm trying to get a string that says "FPS=<fps>", but it won't work
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  2. #2

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    nevermind, i used sprintf()
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In pure C++:
    Code:
    ostringstream oss;
    oss << "FPS: " << fps_value;
    
    // Do something with oss.str()
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    I'm outputting it to a window i created with CreateWindow()
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    DrawText(hDC, oss.str().c_str(), ...);
    ...or something
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    i think it was textout..yeah here it is:

    Code:
    	char *output=new char[];
    
    	sprintf(output,"FPS=%f",fps);
    	TextOut(hDC,10,10,output,strlen(output));
    The thing was i had it output every second, and it showed up for a split second then disappeared...adn reappeared every other second...confusing but yeah.

    Then I outputed it in the DrawGL() section, and it did the same thing here
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    char *output=new char[];
    Uh...

    Not sure how that one can work...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    Hey I don't know either...I just know it works.

    Should I use

    Code:
    char *output = (char *)malloc(255*sizeof(char));
    instead? I've used that for a while without any problem, so I just stuck with it.

    Or do you mean "why does it work he didn't specify a max size"?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I'm just intrigued as to what size buffer you get when you don't give a size - I expect it's compiler dependent.

    In C++, use new:
    Code:
    char *buffer = new char[255];
    I'd still use the stringstreams one though, safer and more c++-like (probably just as fast as well, if not faster).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    I'm using VC++ 6.0

    It doesn't just return 6? (FPS=30) (example)

    I'll do a quick thing to check
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  11. #11

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    Yup, I just did quick test, since the number was a float, it came out as


    85.00000

    so strlen() was 12
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Before you insert the float, try something like oss.precision(2);.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    ok thanks

    what's oss?

    i think i'll change it to an int...i dont think the fps will go above 32000 (or whatever the max # for int is)
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    oss is the ostringstream object from earlier.

    The maximum value of an int is about 2 thousand million (4 for unsigned).

    Basically, anything you can do to a stream like cout or cin, you can do to a stringstream...that's what makes them so utterly cool
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    Ah. And that works for printf() and scanf()?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I don't think they're particularly compatible with each other, but they do more or less the same thing.

    Basically, as printf, scanf, sprintf, sscanf all use a format string, cout, cin, ostringstream, istringstream all use the same manipulators.

    For example, if you define operator>> for your class, then it will work with a stringstream
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    <-- C++ n00b
    "Basically, as printf, scanf, sprintf, sscanf all use a format string, cout, cin, ostringstream, istringstream all use the same manipulators.

    For example, if you define operator>> for your class, then it will work with a stringstream"


    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ok.
    Code:
    printf("Hello: %d\n", int_value_here);
    
    cout << "Hello: " << int_value_here << endl;
    ...or...
    Code:
    void thingie(ostream &os) {
        os << 56 << hex << 255 << endl;
    }
    
    void somecode() {
        thingie(cout);
    
        ostringstream oss;
        thingie(oss);
    
        cout << oss.str();
    }
    All the streams work in the same way, and are interchangeable at the ostream / istream level.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    Ohh okay I get it...Thanks
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  20. #20
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No problem.

    When you think about it, it's far more logical than C's approach.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  21. #21

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    yeah, you can customize it alot easier with less code.

    Me think's ill use that somewhere...
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width