double -> LPCSTR (if its possible)

This is a discussion on double -> LPCSTR (if its possible) within the C++ Programming forums, part of the General Programming Boards category; Hi My Aim is to convert a double type to a LPCSTR and afterwards set it as an item text ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    double -> LPCSTR (if its possible)

    Hi
    My Aim is to convert a double type to a LPCSTR and afterwards set it as an item text of an edit box... (in Dialog box not a window)

    I ve got these two lines but apparantly its complete rubbish... Isnt it?
    Code:
    TCHAR chValue[128];
    double m_iTotal;
    wsprintf(chValue, "%s", m_iTotal);
    SetDlgItemText(hDlg, IDC_EDIT, chValue);
    Do you have any idea how is the conversion possible?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    LPCSTR is not a wide character string. TCHAR might be, it might not, depending on whether UNICODE is defined. wsprintf works only on wide character strings.

    So which do you want?

    If you want to use sprintf, I don't think that %s is the proper format specifier for double. Perhaps %f will work better.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The format specifer is indeed wrong. Why are you printing a double as a string? Change it to either %f or %lf (check whatever wsprintf() accepts for doubles). You might want to also try to make sure you don't overrun your buffer.

    BTW, LPCSTR is just a pointer to a constant string. In C terms, this is a const char *.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Instead of using C functions, I would prefer using C++ features...
    Code:
    std::wstringstream ss;
    ss << m_iTotal;
    SetDlgItemText(hDlg, IDC_EDIT, ss.str().c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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