Doubles in Edit Boxes

This is a discussion on Doubles in Edit Boxes within the Windows Programming forums, part of the Platform Specific Boards category; Hi I'm hoping someone can help me with this. I've been searching google for several hours and I can't find ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Doubles in Edit Boxes

    Hi I'm hoping someone can help me with this. I've been searching google for several hours and I can't find a satisfactory answer to make my code work.
    I'm trying to create an edit box within a dialog window that will recieve a double input from a user and then display that double value in a different edit box when a button is pressed. I've seen the use of the GetDlgItemText() function to get the information as a string and then using the atof() function to convert it to double, but when I try this I get 0 every time. For some reason, visual c++ 2008 makes me typecast my char arrays to LPWSTR in order for it to compile properly. (Not sure why or if I'm just not setting them up correctly to begin with). Can someone suggest a different way to do this or tell me what is wrong with my code so I can fix it?

    Here is my code:

    //global variables
    double AccountBalance;
    char temp[MAX_PATH];
    //this code is within my button case in my switch statement.
    char buffer[MAX_PATH];
    GetDlgItemText(AccountDialog,IDC_ACCOUNTBALANCE,(L PWSTR)buffer,MAX_PATH);
    AccountBalance = atof(buffer);
    AccountBalance = AccountBalance*2;
    char temp2[100];
    wsprintf((LPWSTR)temp2,L
    "%d",AccountBalance);
    SetDlgItemText(AccountDialog,IDC_EDIT1,(LPWSTR)tem p2);

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Try using %f (not %d)

    %d is for integers, so any digits after the decimal point will be truncated (ie 0.99999999999 will become 0)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    The path of least resistance if you've got lots of other code that's fine is to:
    Open the project properties
    Go to Configuration Properties->General and change the 'Character Set' option to Not Set.
    Remove all those casts
    Do what novacain said
    Change wsprintf to sprintf
    and remove the L before your new %f string.

    That should do it.

    To go along with it:
    Don't change any project properties
    Still remove the casts
    Change your 3 'char' buffers to 'wchar_t' buffers
    Make atof into _wtof
    Still do what novacain said
    and finally change wsprintf to swprintf

    If you've put those casts anywhere else in your program to make it work, then it needs reinvestigating because it probably won't be safe or do what you want it to.

    As to what it's all about, this is a brief article about it but may be a bit technical depending on how ensconced you are into your programming career.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codenewbie View Post
    Hi I'm hoping someone can help me with this. I've been searching google for several hours and I can't find a satisfactory answer to make my code work.
    I'm trying to create an edit box within a dialog window that will recieve a double input from a user and then display that double value in a different edit box when a button is pressed. I've seen the use of the GetDlgItemText() function to get the information as a string and then using the atof() function to convert it to double, but when I try this I get 0 every time. For some reason, visual c++ 2008 makes me typecast my char arrays to LPWSTR in order for it to compile properly. (Not sure why or if I'm just not setting them up correctly to begin with). Can someone suggest a different way to do this or tell me what is wrong with my code so I can fix it?

    Here is my code:

    //global variables
    double AccountBalance;
    char temp[MAX_PATH];
    //this code is within my button case in my switch statement.
    char buffer[MAX_PATH];
    GetDlgItemText(AccountDialog,IDC_ACCOUNTBALANCE,(L PWSTR)buffer,MAX_PATH);
    AccountBalance = atof(buffer);
    AccountBalance = AccountBalance*2;
    char temp2[100];
    wsprintf((LPWSTR)temp2,L
    "%d",AccountBalance);
    SetDlgItemText(AccountDialog,IDC_EDIT1,(LPWSTR)tem p2);
    The reason it's forcing you to typecast is that someplace you've got #define UNICODE in your source. Hence all windows api calls need wide strings to work...

    Either
    A) remove the UNICODE define
    B) change all your strings over to WCHAR .... and use the unicode vesions of all string functions in wchar.h
    c) Explicitly call the Ansi version of Windows functions... as in GetDlgItemTextA() etc.

    Unicode programming is a whole different animal than you are likely to be used to with console programming.
    Code:
    //global variables
    #include <wchar.h>
    #define MAX_EDIT 64
    
    double AccountBalance;  
    char temp[MAX_EDIT];   
    
    //this code is within my button case in my switch statement.
    
    WCHAR buffer[MAX_EDIT];
    GetDlgItemText(AccountDialog,IDC_ACCOUNTBALANCE,buffer,MAX_EDIT);
    AccountBalance = wcstod(buffer);
    AccountBalance = AccountBalance*2;
    WCHAR temp2[100];
    wsprintf(temp2,L"%d",AccountBalance);
    SetDlgItemText(AccountDialog,IDC_EDIT1,temp2);
    As matters of general practice to reduce errors it's not a good idea to use any more global variables than you absolutely have to. It's also a good idea to put as little code as possible in switch statements of message handlers; you should instead create functions for things like the above and call them from the message handler. Also... when working in Windows API, you should try to use windows types (variables, structs, etc) whenever possible.
    Last edited by CommonTater; 12-21-2011 at 10:58 PM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Thanks everyone for the suggestions. I did what novacain and adeyblue suggested and everything is working properly now.

    Here is the corrected code in case anyone else needs it:
    wchar_t buffer[MAX_PATH];
    GetDlgItemText(AccountDialog,IDC_ACCOUNTBALANCE,bu ffer,MAX_PATH);
    AccountBalance = _wtof(buffer);
    AccountBalance = AccountBalance*2;
    wchar_t temp2[100];
    swprintf(temp2,L"%f",AccountBalance);
    SetDlgItemText(AccountDialog,IDC_EDIT1,temp2);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Edit boxes, and list boxes
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-07-2004, 12:34 PM
  2. EDIT boxes
    By krappykoder in forum Windows Programming
    Replies: 2
    Last Post: 12-24-2003, 12:44 PM
  3. Edit Boxes
    By bc17 in forum Windows Programming
    Replies: 6
    Last Post: 02-16-2003, 09:08 PM
  4. Edit Boxes
    By ColdFire in forum Windows Programming
    Replies: 2
    Last Post: 02-13-2002, 01:54 PM
  5. edit boxes
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 01-25-2002, 04:47 PM

Tags for this Thread


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