Hello everbody
Simply put; rand() is driving me nuts.
I've been trying to get my teeth into Dev c++ (v. 4.9.9.2) for the first time and couldn't find anything anywhere which helped me anymore. Nearly all this code is IDE generated or copied from examples - I'm just trying to understand it all.
I'm tryiing to rebuild an old prog that I built with Builder_5 (I know, I know!) for personal use a long time ago.
My prog sometimes produces results which are "out-of-bounds" - look for the red bits down below - it sometimes outputs values < 1 and > 90.
The prog should (eventually) put a value in an Editbox. There are no other complaints from the compiler.
Is it perhaps because A_function() is improperly declared?
Any pointers would be appreciated.
David
#include <windows.h>
#include <time.h>
<snip>
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
srand (time(NULL));
HWND hwnd; /* This is the handle for our window */
etc, etc, . . .
<big snip>
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
char A_function();
static int len;
static HWND hwndEdit;
static HWND hwndButton;
static TCHAR text[30];
case WM_CREATE:
{
//the two buttons
CreateWindow(TEXT("button"), TEXT("Beep"),
WS_VISIBLE | WS_CHILD ,
20, 100, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Quit"),
WS_VISIBLE | WS_CHILD ,
120, 100, 80, 25,
hwnd, (HMENU) 2, NULL, NULL);
//the checkbox
CreateWindow(TEXT("button"), TEXT("Show Title"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
20, 20, 185, 35,
hwnd, (HMENU) 3, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
CheckDlgButton(hwnd, 3, BST_CHECKED);
//edit box
hwndEdit = CreateWindow(TEXT("Edit"),
NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 70, 150, 20, hwnd, (HMENU) 4,//ID_EDIT,
NULL, NULL);
//editbox button
hwndButton = CreateWindow(TEXT("button"), TEXT("Set editbox"),
WS_VISIBLE | WS_CHILD,
250, 70, 80, 25,
hwnd, (HMENU) 5/*ID_BUTTON*/, NULL, NULL);
//test function access button
CreateWindow(TEXT("button"), TEXT("FuncTest"),
WS_VISIBLE | WS_CHILD ,
220, 100, 80, 25,
hwnd, (HMENU) 6, NULL, NULL);
break;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == 1) {
Beep(400, 50);
}
if (LOWORD(wParam) == 2) {
PostQuitMessage(0);
}
if (LOWORD(wParam) == 3) {
BOOL checked = IsDlgButtonChecked(hwnd, 3);
if (checked) {
CheckDlgButton(hwnd, 3, BST_UNCHECKED);
SetWindowText(hwnd, TEXT(""));
} else {
CheckDlgButton(hwnd, 3, BST_CHECKED);
SetWindowText(hwnd, "Check Box");//title);
}
}
if ((HIWORD(wParam) == BN_CLICKED) && (LOWORD(wParam) == 5)) {
len = GetWindowTextLength(hwndEdit) + 1;
GetWindowText(hwndEdit, text, len);
SetWindowText(hwnd, text);
}
if (LOWORD(wParam) == 6) {
/////////////these won't work at all - and/or throw up more error messages
// SetWindowText(hwnd, text);
// SetWindowText(hwnd, (LPCSTR)temp);
////////////////////////////////////
//the following is an attempt to 'see' what was going on
char temp = A_function();
if (temp == 0)
SetWindowText(hwnd, "null");//(LPCSTR)temp);//text);
else
if (temp > 0 && temp <= 45)
SetWindowText(hwnd, "1 - 45");//(LPCSTR)temp);//text);
else
if (temp > 45 && temp <= 89)
SetWindowText(hwnd, "46 - 89");
else
if (temp == 90)
SetWindowText(hwnd, "is 90");
else
if (temp < 0)
SetWindowText(hwnd, "< 0");
else
if (temp > 90)
SetWindowText(hwnd, "> 90");
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}//end - LRESULT CALLBACK WindowProcedure
char A_function()//int aNum)
{
int bNum = rand()%90;
char globChar;
switch (bNum)
{
<super snip>
}
return globChar;
}