简体   繁体   中英

Reading double from dialog edit control

I'm currently working on a program (for fun, this is not an assignment) that has multiple functions. I have never used Win32 prior to yesterday and so I am rather new. I used TheForger's tutorials to get started. Right now, I have a dialog form with four edit boxes on it, charge1, charge2, charge3, and distance between particles. I am getting this information and plugging it into the formula to solve for the amount of force between the particles.

When I get to the part where I am getting the data from the edit box, I am receiving 0.

Here is my current code:

case ID_SOLVE:
{
    ZeroMemory(coulombDisplay, sizeof(coulombDisplay));

    GetDlgItemText(g_hCoulombs, IDC_DISTANCE, value1, 10);
    coulombsDistance = atof(value1);

    GetDlgItemText(g_hCoulombs, IDC_CHARGE1, value2, 10);
    coulombsStrength1Base = atof(value2);

    GetDlgItemText(g_hCoulombs, IDC_CHARGE2, value3, 10);
    coulombsStrength2Base = atof(value3);

    if(coulombsDistance == 0.0)
    {
        MessageBox(NULL, "WHAT", "WHAT", MB_OK | MB_ICONEXCLAMATION);
        DestroyWindow(g_hCoulombs);
    }

    coulombsResult = (coulombsStrength1Base * coulombsStrength2Base);
    coulombsResult /= (pow(coulombsDistance, 2));
    coulombsResult *= kConstant;

    sprintf(coulombDisplay, "%g", coulombsResult);
    SendDlgItemMessage(g_hCoulombs, IDC_FORCE, WM_SETTEXT, 0, (LPARAM)(LPCSTR)coulombDisplay);
}
break;

value1 , value2 , value3 , and coulombDisplay are all char[] that have been zero'd

coulombsResult , coulombsDistance , coulombsStrength1Base , coulombsStrength2Base are all double s

The MessageBox stating "WHAT" is popping up each and every time that I run the program. I am using the multi-byte character set of VC++ 2010.

Any help would be greatly appreciated.

STATUS_ACCESS_DENIED has a good point. If you look at the documentation for atof you'll see that a error condition will result in 0.0 being returned. I'd recommend writing to a log file or something to see what the data is going into the atof function. I'm wondering if your allocated char buffer is big enough.

Try GetDlgItemTextA instead of GetDlgItemText to make sure you're getting back 8-bit characters and not 16-bit characters. A 16-bit character will usually have a zero in the upper half, and will be interpreted as an empty 8-bit string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM