is there any way to write a float to a ch string

This is a discussion on is there any way to write a float to a ch string within the C++ Programming forums, part of the General Programming Boards category; is there any way to write a float to a ch string such as double me=254,1948; i want to write ...

  1. #1
    Registered User mayfda's Avatar
    Join Date
    Apr 2002
    Posts
    11

    is there any way to write a float to a ch string

    is there any way to write a float to a ch string such as

    double me=254,1948;

    i want to write this it to
    char * temp=new char [200];

    is there any way to do this?
    -mayfda-

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    sprintf() or stringstreams.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User mayfda's Avatar
    Join Date
    Apr 2002
    Posts
    11

    ??

    can you explain it in a more detailed way?
    -mayfda-

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Your compiler not got helpfiles?
    Forgotten what a search engine is?
    Didnt you see the search button ?
    Can you wipe your own ass or do you need nursmaiding for that too?

    here...

    sprintf, swprintf
    Write formatted data to a string.

    int sprintf( char *buffer, const char *format [, argument] ... );

    int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... );

    Routine Required Header Compatibility
    sprintf <stdio.h> ANSI, Win 95, Win NT
    swprintf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT


    For additional compatibility information, see Compatibility in the Introduction.

    Libraries

    LIBC.LIB Single thread static library, retail version
    LIBCMT.LIB Multithread static library, retail version
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version


    Return Value

    sprintf returns the number of bytes stored in buffer, not counting the terminating null character. swprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character.

    Parameters

    buffer

    Storage location for output

    format

    Format-control string

    argument

    Optional arguments

    For more information, see Format Specifications.

    Remarks

    The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

    swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Detection of encoding errors in swprintf may differ from that in sprintf. swprintf and fwprintf behave identically except that swprintf writes output to a string rather than to a destination of type FILE.

    Generic-Text Routine Mappings

    TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
    _stprintf sprintf sprintf swprintf


    Example

    /* SPRINTF.C: This program uses sprintf to format various
    * data and place them in the string named buffer.
    */

    #include <stdio.h>

    void main( void )
    {
    char buffer[200], s[] = "computer", c = 'l';
    int i = 35, j;
    float fp = 1.7320534f;

    /* Format and print various data: */
    j = sprintf( buffer, "\tString: %s\n", s );
    j += sprintf( buffer + j, "\tCharacter: %c\n", c );
    j += sprintf( buffer + j, "\tInteger: %d\n", i );
    j += sprintf( buffer + j, "\tReal: %f\n", fp );

    printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
    }


    Output

    Output:
    String: computer
    Character: l
    Integer: 35
    Real: 1.732053

    character count = 71

    and now look up istringstream and ostringstream to see a more c++ way of doing things.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Shadow12345
    Guest
    Your compiler not got helpfiles?
    Forgotten what a search engine is?
    Didnt you see the search button ?
    Can you wipe your own ass or do you need nursmaiding for that too?
    don't forget to check out msdn.microsoft.com, but do not take them too seriously because they have void mainers. I hope they fired the cavemen that wrote those help documents!

    OH and btw having a nurse maid person to wipe my ass would be awesome!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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