SetDlgItemInt Doesn't take negative numbers.

Aug 22, 2011 at 10:25am
closed account (Gz64jE8b)
I'm using SetDlgItemInt to use recieved input from the user, do some calculations then return the answer but it doesnt work with negative numbers, this is a problem due to the fact that this particular program uses negative numbers 90% of the time.

How can I get it to recognise negative numbers?


Edit: Also I'm sometimes getting numbers like 1.34-e00, solution for that?
Last edited on Aug 22, 2011 at 1:42pm
Aug 22, 2011 at 11:52am
Last parameter to GetDlgItemInt must be true,
GetDlgItemInt(hDlg,nIDDlgItem,lpTranslated,true);
see http://msdn.microsoft.com/en-us/library/ms645485(v=vs.85).aspx
Aug 22, 2011 at 1:41pm
closed account (Gz64jE8b)
It's my SetDlgItemText that's the problem actually, when I use SetDlgItemInt I can work with negative numbers but not decimals, when I use SetDlgItemText I can use decimals but not negatives.

Is there a way to use decimals with SetDlgItemInt?
Aug 22, 2011 at 3:50pm
closed account (Gz64jE8b)
Nobody? :(
Aug 22, 2011 at 4:37pm
You say SetDlgItemInt() can work with negatives but not decimals. Does decimals mean floating point? Because the INT part of the function's name pretty much says it works with integers only.

As for SetDlgItemText(), it can display any text. Just use the stringstream method to convert whatever number (positive, negative, integer, floating point, etc.) to a string and then pass this string to SetDlgItemText().

Google up "c++ number to string stringstream".
Aug 22, 2011 at 5:59pm
closed account (Gz64jE8b)
I can't find anything of use (most of the stuff is beyond my understanding).

Could you just supply a code snippit for me to work with?

(I'm not in a programming class so don't worry about doing my homework).
Aug 22, 2011 at 6:07pm
Like this:
1
2
3
4
std::basic_stringstream<TCHAR> ss;
ss << number;
SetDlgItemText(hwnd, nIDDlgItem ,ss.str().c_str() );
......
Aug 23, 2011 at 10:03am
closed account (Gz64jE8b)
Nothing has worked so far ^_^
Aug 23, 2011 at 11:37am
closed account (DSLq5Di1)
..and what do you expect from us? there is nothing wrong with the suggestions given, more likely your implementation has a problem. The more information (code/output/expected output/errors/etc..) you provide, the better our feedback can be.
Aug 23, 2011 at 2:03pm
closed account (Gz64jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#define ID_TEXTBOXA 1
#define ID_TEXTBOXB 2
#define ID_TEXTBOXC 3
#define ID_TEXTBOXD 4
#define ID_TEXTBOXE 6
#define ID_BUTTON 5
#define BUFFER 14

TCHAR tchBuffer[BUFFER];

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  
    {
        case WM_CREATE:{
             {
             CreateWindow(TEXT("EDIT"), TEXT("A"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 
             20, 10, 200, 20, hwnd, (HMENU) ID_TEXTBOXA, NULL, NULL);
             }
             {
             CreateWindow(TEXT("EDIT"), TEXT("B"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 
             20, 40, 200, 20, hwnd, (HMENU) ID_TEXTBOXB, NULL, NULL);
             }
             {
             CreateWindow(TEXT("EDIT"), TEXT("C"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 
             20, 70, 200, 20, hwnd, (HMENU) ID_TEXTBOXC, NULL, NULL);
             }
             {
             CreateWindow(TEXT("STATIC"), TEXT("X"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 
             10, 300, 200, 20, hwnd, (HMENU) ID_TEXTBOXD, NULL, NULL);
             }
             {
             CreateWindow(TEXT("STATIC"), TEXT("X"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 
             10, 250, 200, 20, hwnd, (HMENU) ID_TEXTBOXE, NULL, NULL);
             }
             
             
             
             {
             CreateWindow(TEXT("BUTTON"), TEXT("Solve"), WS_VISIBLE | WS_CHILD, 
             250, 300, 130, 20, hwnd, (HMENU) ID_BUTTON, NULL, NULL);
             
             }
             break;
             }
        case WM_COMMAND:{
             
             if(LOWORD(wParam) == ID_BUTTON){
             double a;
             double b;
             double c;
             double d;
             double f;
             double g;

             
        a = GetDlgItemInt(hwnd, ID_TEXTBOXA, NULL, TRUE);
                     if( a == 0)
             {
                 MessageBox(hwnd, "Cannot divide by zero", "Error", MB_OK | MB_ICONERROR);
                 break;
                 }
        b = GetDlgItemInt(hwnd, ID_TEXTBOXB, NULL, TRUE);
        c = GetDlgItemInt(hwnd, ID_TEXTBOXC, NULL, TRUE);
        d = sqrt(((b*b)-(4*a*c)));
        f = ((-b+d)/(2*a));
        g = ((-b-d)/(2*a));
        if (((b*b)-(4*a*c)) < 0 )
        {
        MessageBox(hwnd, "Cannot square root a negative number", "Error", MB_OK | MB_ICONERROR);
        break;
        }
        SetDlgItemText(hwnd, ID_TEXTBOXD, _gcvt((f), 13, tchBuffer));
        SetDlgItemText(hwnd, ID_TEXTBOXE, _gcvt((g), 13, tchBuffer));
        }

             
             break;
             }
        case WM_DESTROY:
            PostQuitMessage (0);       
            break;
        default:                      
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}




Knock yourselves out
Last edited on Aug 23, 2011 at 2:04pm
Aug 23, 2011 at 4:27pm
closed account (DSLq5Di1)
Using a stringstream as webJose mentioned,
1
2
#include <sstream>
#include <string> 

1
2
3
4
5
6
7
8
template <typename T>
inline std::basic_string<TCHAR> t_str(const T& val)
{
    std::basic_ostringstream<TCHAR> oss;
    oss << val;

    return oss.str();
}

SetDlgItemText(hwnd, ID_TEXTBOXD, t_str(f).c_str());


There is no TCHAR mapping for _gcvt as far as I can see..
http://msdn.microsoft.com/en-us/library/tsbaswba

So if you wish to use a C function instead, I'd suggest _sntprintf_s.
http://msdn.microsoft.com/en-us/library/f30dzcf6
Aug 23, 2011 at 4:30pm
closed account (Gz64jE8b)
The stringstream didn't work, I tried it.
Aug 23, 2011 at 4:38pm
Can you post the code where you try to use stringstream?
Aug 24, 2011 at 6:45am
closed account (Gz64jE8b)
Everything you'll ever need is in the code above.
Aug 24, 2011 at 7:23am
LOL! The stubburn kind I see.

A tip: If you are in need of help, especially with a topic above your current skill, you don't say "it's up there, deduce yourself". Instead you promptly volunteer whatever piece of information you were asked.

The stringstream approach works 100% OK. What you need to do is learn how to use it. Null seems willing to point out the correct approach if you show him/her where you are attempting the stringstream method.
Aug 24, 2011 at 7:26am
closed account (Gz64jE8b)
I tried the stringstream but I get an error message that tells me it can't be converted to a certain format (I forgot which one...)
Aug 24, 2011 at 8:06am
Can you post the error message?
Topic archived. No new replies allowed.