[C++ Win32 API] gcvt() Error/Question

My question is at the bottom.

main.cpp:
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//-LeetGamer- (aka xGhostGamerx)'s BlackOps KDR Calculator
//To find your true KDR you can just divide your kills by your deaths in a calculator
//But I made this program that will hopefully be more "user friendly" than the typical calculator
//This program was made in C++ and is open source, and you are allowed to use the source code in any way besides:
//	Making a similar program to this one AND/OR saying you where the original maker of this program
//	Editing the source code and giving it to anyone, keep the source code EXACTLY how it is!
//Other than those two things, feel free to do what with this source code

#include <Windows.h>

#define WS_NO_RESIZE				WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX
#define WS_CHILD_WINDOW				WS_CHILD|WS_VISIBLE|WS_BORDER

#define HandleMessages(a);			TranslateMessage(a); DispatchMessage(a);

#define NUMBER_OF_KILLS				0x1337
#define NUMBER_OF_DEATHS			0x1338
#define CALCULATE					0x1339

LRESULT CALLBACK WindowProcedure (HWND, unsigned int, WPARAM, LPARAM);
bool SetUpWindowClass (char*, int, int, int);

double dResult = 0;
char *cpResult = "";
bool bPrintResult = false;
int iRate = 0, iKills = 0, iDeaths = 0;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow)
{
	if (!SetUpWindowClass ("1", 255, 255, 255))
	{
		MessageBox (NULL, "Window class failed", NULL, MB_OK);
		return 0;
	}
	HWND hWnd = CreateWindow ("1", "-LeetGamer- (aka xGhostGamerx)'s BlackOps KDR Calculator", WS_NO_RESIZE,
							  315, 115, 700, 480, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{
		MessageBox (hWnd, "Window creation failed", NULL, MB_OK);
		return 0;
	}
	ShowWindow (hWnd, SW_SHOW);
	MSG uMsg;
	while (GetMessage (&uMsg, NULL, 0, 0) > 0)
	{
		HandleMessages (&uMsg);
		if (VK_ESCAPE == uMsg.wParam) PostQuitMessage (0);
	}
	return 0;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, unsigned int uiMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uiMsg)
	{
		case WM_CLOSE:
			DestroyWindow (hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage (0);
			break;
		case WM_CREATE:
			CreateWindow ("edit", "Replace this with Kills", WS_CHILD_WINDOW, 5, 5, 180, 25, hWnd,
						  (HMENU) NUMBER_OF_KILLS, GetModuleHandle (NULL), NULL);
			CreateWindow ("edit", "Replace this with deaths", WS_CHILD_WINDOW, 5, 35, 180, 25, hWnd,
						  (HMENU) NUMBER_OF_DEATHS, GetModuleHandle (NULL), NULL);
			CreateWindow ("button", "Calculate", WS_CHILD_WINDOW, 5, 100, 130, 25, hWnd,
						  (HMENU) CALCULATE, GetModuleHandle (NULL), NULL);
			break;
		case WM_COMMAND:
			switch (LOWORD (wParam))
			{
				case CALCULATE:
					{
						TCHAR tcKills [256], tcDeaths [256];
						GetWindowText (GetDlgItem (hWnd, NUMBER_OF_KILLS), tcKills, 256);
						GetWindowText (GetDlgItem (hWnd, NUMBER_OF_DEATHS), tcDeaths, 256);
						itoa (iKills, tcKills, 10);
						itoa (iDeaths, tcDeaths, 10);
						dResult = (float) iKills / iDeaths;
						MessageBox (hWnd, "gcvt() not ran yet", "Test", MB_OK);
						gcvt (dResult, 16, cpResult);
						MessageBox (hWnd, "gcvt() ran good", "Test", MB_OK);
						if (dResult < 1.0) iRate = 0;
						else if (dResult < 1.5) iRate = 1;
						else if (dResult < 2.0) iRate = 2;
						else iRate = 3;
						bPrintResult = true;
						InvalidateRect (hWnd, NULL, TRUE);
					}
					break;
			}
			break;
		case WM_PAINT:
			{
				PAINTSTRUCT ps;
				HDC hDC = BeginPaint (hWnd, &ps);
				int iY = 0;
				char *cpaText [9];
				cpaText [0] = "Welcome to -LeetGamer- (aka xGhostGamerx)'s BlackOps KDR Calculator!";
				cpaText [1] = "Enter your kills and your deaths, then click \"Calculate\"";
				cpaText [2] = "Result: ";
				cpaText [3] = "-LeetGamer- (aka xGhostGamerx)'s Rate: ";
				cpaText [4] = "Poor";
				cpaText [5] = "Ok";
				cpaText [6] = "Good";
				cpaText [7] = "Great";
				cpaText [8] = "Amazing";
				for (int i = 0; i < 2; i++, iY += 20)
					TextOut (hDC, 200, iY, cpaText [i], strlen (cpaText [i]));
				if (bPrintResult)
				{
					TextOut (hDC, 200, 100, cpResult, strlen (cpResult));
					char *cpRate;
					switch (iRate)
					{
						case 0: cpRate = cpaText [4]; break;
						case 1: cpRate = cpaText [5]; break;
						case 2: cpRate = cpaText [6]; break;
						case 3: cpRate = cpaText [7]; break;
						case 4: cpRate = cpaText [8]; break;
					}
					TextOut (hDC, 200, 120, cpRate, strlen (cpRate));
				}
				EndPaint (hWnd, &ps);
			}
			break;
	}
	return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}

bool SetUpWindowClass (char* cpClassTitle, int iR, int iG, int iB)
{
	WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = cpClassTitle;
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure;
	WindowClass.hInstance = GetModuleHandle (NULL);
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (iR, iG, iB));
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	if (RegisterClassEx (&WindowClass)) return true;
	else return false;
}


Lines 81 - 83:
1
2
3
MessageBox (hWnd, "gcvt() not ran yet", "Test", MB_OK);
gcvt (dResult, 16, cpResult);
MessageBox (hWnd, "gcvt() ran good", "Test", MB_OK);


The first message box was executed, the other one was not. Does anyone know why the code execution stops when this function is called?
Last edited on
Hi

You dont appear to have allocated any space for cpResult

eg cpResult = new char[16].

Hope this helps

http://msdn.microsoft.com/en-us/library/26cdxcyz(VS.80).aspx

MSDN: A buffer of length _CVTBUFSIZE is sufficient for any floating point value.


For some reason these guys seem to know the MS library really well...
It says "_CVTBUFSIZE was not declared in this scope". What header should I use to be able to use this?

This is my code now:

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//-LeetGamer- (aka xGhostGamerx)'s BlackOps KDR Calculator
//To find your true KDR you can just divide your kills by your deaths in a calculator
//But I made this program that will hopefully be more "user friendly" than the typical calculator
//This program was made in C++ and is open source, and you are allowed to use the source code in any way besides:
//	Making a similar program to this one AND/OR saying you where the original maker of this program
//	Editing the source code and giving it to anyone, keep the source code EXACTLY how it is!
//Other than those two things, feel free to do what with this source code

#include <Windows.h>

#define WS_NO_RESIZE				WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX
#define WS_CHILD_WINDOW				WS_CHILD|WS_VISIBLE|WS_BORDER

#define HandleMessages(a);			TranslateMessage(a); DispatchMessage(a);

#define NUMBER_OF_KILLS				0x1337
#define NUMBER_OF_DEATHS			0x1338
#define CALCULATE					0x1339

LRESULT CALLBACK WindowProcedure (HWND, unsigned int, WPARAM, LPARAM);
bool SetUpWindowClass (char*, int, int, int);

double dResult = 0;
char *cpResult = new char [16];
bool bPrintResult = false;
int iRate = 0, iKills = 0, iDeaths = 0;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow)
{
	if (!SetUpWindowClass ("1", 255, 255, 255))
	{
		MessageBox (NULL, "Window class failed", NULL, MB_OK);
		return 0;
	}
	HWND hWnd = CreateWindow ("1", "-LeetGamer- (aka xGhostGamerx)'s BlackOps KDR Calculator", WS_NO_RESIZE,
							  315, 115, 700, 480, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{
		MessageBox (hWnd, "Window creation failed", NULL, MB_OK);
		return 0;
	}
	ShowWindow (hWnd, SW_SHOW);
	MSG uMsg;
	while (GetMessage (&uMsg, NULL, 0, 0) > 0)
	{
		HandleMessages (&uMsg);
		if (VK_ESCAPE == uMsg.wParam) PostQuitMessage (0);
	}
	return 0;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, unsigned int uiMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uiMsg)
	{
		case WM_CLOSE:
			delete [] cpResult;
			DestroyWindow (hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage (0);
			break;
		case WM_CREATE:
			CreateWindow ("edit", "Replace this with Kills", WS_CHILD_WINDOW, 5, 5, 180, 25, hWnd,
						  (HMENU) NUMBER_OF_KILLS, GetModuleHandle (NULL), NULL);
			CreateWindow ("edit", "Replace this with deaths", WS_CHILD_WINDOW, 5, 35, 180, 25, hWnd,
						  (HMENU) NUMBER_OF_DEATHS, GetModuleHandle (NULL), NULL);
			CreateWindow ("button", "Calculate", WS_CHILD_WINDOW, 5, 100, 130, 25, hWnd,
						  (HMENU) CALCULATE, GetModuleHandle (NULL), NULL);
			break;
		case WM_COMMAND:
			switch (LOWORD (wParam))
			{
				case CALCULATE:
					{
						TCHAR tcKills [256], tcDeaths [256];
						GetWindowText (GetDlgItem (hWnd, NUMBER_OF_KILLS), tcKills, 256);
						GetWindowText (GetDlgItem (hWnd, NUMBER_OF_DEATHS), tcDeaths, 256);
						itoa (iKills, tcKills, 10);
						itoa (iDeaths, tcDeaths, 10);
						dResult = (float) iKills / iDeaths;
						gcvt (dResult, 16, cpResult);
						if (dResult < 1.0) iRate = 0;
						else if (dResult < 1.5) iRate = 1;
						else if (dResult < 2.0) iRate = 2;
						else iRate = 3;
						bPrintResult = true;
						InvalidateRect (hWnd, NULL, TRUE);
					}
					break;
			}
			break;
		case WM_PAINT:
			{
				PAINTSTRUCT ps;
				HDC hDC = BeginPaint (hWnd, &ps);
				int iY = 0;
				char *cpaText [9];
				cpaText [0] = "Welcome to -LeetGamer- (aka xGhostGamerx)'s BlackOps KDR Calculator!";
				cpaText [1] = "Enter your kills and your deaths, then click \"Calculate\"";
				cpaText [2] = "Result: ";
				cpaText [3] = "-LeetGamer- (aka xGhostGamerx)'s Rate: ";
				cpaText [4] = "Poor";
				cpaText [5] = "Ok";
				cpaText [6] = "Good";
				cpaText [7] = "Great";
				cpaText [8] = "Amazing";
				for (int i = 0; i < 2; i++, iY += 20)
					TextOut (hDC, 200, iY, cpaText [i], strlen (cpaText [i]));
				if (bPrintResult)
				{
					TextOut (hDC, 200, 100, cpResult, strlen (cpResult));
					char *cpRate;
					switch (iRate)
					{
						case 0: cpRate = cpaText [4]; break;
						case 1: cpRate = cpaText [5]; break;
						case 2: cpRate = cpaText [6]; break;
						case 3: cpRate = cpaText [7]; break;
						case 4: cpRate = cpaText [8]; break;
					}
					TextOut (hDC, 200, 120, cpRate, strlen (cpRate));
				}
				EndPaint (hWnd, &ps);
			}
			break;
	}
	return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}

bool SetUpWindowClass (char* cpClassTitle, int iR, int iG, int iB)
{
	WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = cpClassTitle;
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure;
	WindowClass.hInstance = GetModuleHandle (NULL);
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (iR, iG, iB));
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	if (RegisterClassEx (&WindowClass)) return true;
	else return false;
}


Changed lines:

24:
char *cpResult = new char [16];

57:
delete [] cpResult;

I want 82 to be this:
gcvt (dResult, _CVTBUFSIZE, cpResult);

but it is this:

gcvt (dResult, 16, cpResult);

Because I'm not including the right headers, and I could find what header to use, but I'm in a hurry to do something else :P

Thanks for the help so far. Also, any time I press the "Calculate" button I get some odd string printed on my window "-1.#IND", any one know a reason for this?

Thanks !
hi again

didnt test the code but I think you might have meant to use atoi not itoa

1
2
3
4
5
6
7
8
9
        TCHAR tcKills [256], tcDeaths [256];
	GetWindowText (GetDlgItem (hWnd, NUMBER_OF_KILLS), tcKills, 256);
	GetWindowText (GetDlgItem (hWnd, NUMBER_OF_DEATHS), tcDeaths, 256);
	
1       itoa (iKills, tcKills, 10);        
2	itoa (iDeaths, tcDeaths, 10);
	
        dResult = (float) iKills / iDeaths;


where 1 & 2 should be
1
2
        iKills = atoi(tcKills);
        iDeaths = atoi(tcDeaths)


I Would also consider changing iKills & iDeaths to double and using atof

Edit: and I think __CVTBUFSIZE is in "stdlib.h"

Hope This helps
Last edited on
Thanks, it works now. I feel stupid after using itoa and not atoi, but, we all have our stupid moments :P

thanks again!
Topic archived. No new replies allowed.