Short ques

This is a discussion on Short ques within the C++ Programming forums, part of the General Programming Boards category; How can i convert a double into a char i can put into an array of chars? thnx...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    36

    Short ques

    How can i convert a double into a char i can put into an array of chars?


    thnx
    Time for TOTAL WAR

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    sprintf() or one of the stream members, eg...

    Code:
    float X;
    char Buffer[10];
    
    X = 5.7;
    sprintf(Buffer, "%f", X);
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    How would you solve it using one of the stream members ?

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    lol thats actually very clever, sprintf prints the double into a string (buffer) so that it goes in looking just like it would if you cout<<'d it. At least thats how I understand it.

    Compile the code and see for yourself.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> How would you solve it using one of the stream members ?

    Like that...
    Code:
    #include <strstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        float X;
        char Buffer[10];
        ostrstream OutStr(Buffer, sizeof(Buffer));
    
        X = 5.7;
        OutStr << X << ends;
        cout << Buffer << endl;
        return 0;
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2006, 12:54 AM
  4. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 10:17 AM
  5. My Allegro Program will not run...
    By Vicious in forum Game Programming
    Replies: 17
    Last Post: 06-14-2002, 01:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21