home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
inputdlg.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1997-07-29
|
3KB
|
125 lines
/*
Program illustrates using the input dialog box in a
number-guessing game
*/
#include <owl\applicat.h>
#include <owl\framewin.h>
#include <owl\inputdia.h>
#include "inputdlg.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const MaxBuffer = 81;
// declare the custom application class as
// a subclass of TApplication
class TWinApp : public TApplication
{
public:
TWinApp() : TApplication() {}
protected:
virtual void InitMainWindow();
};
// expand the functionality of TWindow by deriving class TMainWindow
class TMainWindow : public TWindow
{
public:
TMainWindow() : TWindow(0, 0, 0) {}
protected:
// handle the Game menu item
void CMGame();
// handle closing the window
virtual BOOL CanClose();
DECLARE_RESPONSE_TABLE(TMainWindow);
};
DEFINE_RESPONSE_TABLE1(TMainWindow, TWindow)
EV_COMMAND(CM_GAME, CMGame),
END_RESPONSE_TABLE;
void TMainWindow::CMGame()
{
char s[MaxBuffer];
int n, m;
int MaxIter = 10;
int iter = 0;
BOOL ok = TRUE;
TInputDialog* pDlg;
randomize();
n = random(1001);
strcpy(s, "500");
// execute the opening dialog box
pDlg = new TInputDialog(this, "Hi-Lo Guessing Game",
"Enter a number between 0 and 1000",
s, sizeof(s));
if (pDlg->Execute() == IDOK) {
m = atoi(s);
iter++;
// loop to obtain the other guesses
while (m != n && iter < MaxIter && ok == TRUE) {
// is the user's guess higher?
if (m > n) {
pDlg = new TInputDialog(this,
"Hi-Lo Guessing Game",
"Enter a lower guess",
s, sizeof(s));
ok = (pDlg->Execute() == IDOK) ? TRUE : FALSE;
}
else {
pDlg = new TInputDialog(this,
"Hi-Lo Guessing Game",
"Enter a higher guess",
s, sizeof(s));
ok = (pDlg->Execute() == IDOK) ? TRUE : FALSE;
}
m = atoi(s);
iter++;
}
// did the user guess the secret number
if (iter < MaxIter && ok == TRUE) {
MessageBeep(MB_ICONEXCLAMATION);
MessageBeep(MB_ICONEXCLAMATION);
sprintf(s, "You guess it! It's %d", n);
MessageBox(s, "Congratulations!", MB_OK);
}
else {
MessageBeep(-1);
sprintf(s, "The secret number is %d", n);
MessageBox(s, "Sorry!", MB_OK);
}
}
}
BOOL TMainWindow::CanClose()
{
return MessageBox("Want to close this application",
"Query", MB_YESNO | MB_ICONQUESTION) == IDYES;
}
void TWinApp::InitMainWindow()
{
MainWindow = new TFrameWindow(0, "Hi-Lo Number-Guessing Game",
new TMainWindow);
// load the menu resource
MainWindow->AssignMenu(TResId(IDM_MAINMENU));
}
int OwlMain(int /* argc */, char** /*argv[] */)
{
TWinApp app;
return app.Run();
}