Thread: Random Number problem in number guessing game...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54

    Unhappy Random Number problem in number guessing game...

    Here is my source:

    Code:
    #include "stdafx.h"
    #include "EDRs Number Guessing Game.h"
    #include "EDRs Number Guessing GameDlg.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    unsigned number, tries = 0;
    char buffer[32];
    
    int rand_mid(int low, int high);
    
    /////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App About
    
    class CAboutDlg : public CDialog
    {
    public:
    	CAboutDlg();
    
    // Dialog Data
    	//{{AFX_DATA(CAboutDlg)
    	enum { IDD = IDD_ABOUTBOX };
    	//}}AFX_DATA
    
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CAboutDlg)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    	//}}AFX_VIRTUAL
    
    // Implementation
    protected:
    	//{{AFX_MSG(CAboutDlg)
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
    
    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    	//{{AFX_DATA_INIT(CAboutDlg)
    	//}}AFX_DATA_INIT
    }
    
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CAboutDlg)
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    	//{{AFX_MSG_MAP(CAboutDlg)
    		// No message handlers
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CEDRsNumberGuessingGameDlg dialog
    
    CEDRsNumberGuessingGameDlg::CEDRsNumberGuessingGameDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CEDRsNumberGuessingGameDlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CEDRsNumberGuessingGameDlg)
    	m_input = 0;
    	//}}AFX_DATA_INIT
    	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CEDRsNumberGuessingGameDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CEDRsNumberGuessingGameDlg)
    	DDX_Text(pDX, IDC_INPUT, m_input);
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CEDRsNumberGuessingGameDlg, CDialog)
    	//{{AFX_MSG_MAP(CEDRsNumberGuessingGameDlg)
    	ON_WM_SYSCOMMAND()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(IDC_EXIT, OnExit)
    	ON_BN_CLICKED(IDC_SOLVE, OnSolve)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CEDRsNumberGuessingGameDlg message handlers
    
    BOOL CEDRsNumberGuessingGameDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Add "About..." menu item to system menu.
    
    	// IDM_ABOUTBOX must be in the system command range.
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
    
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		CString strAboutMenu;
    		strAboutMenu.LoadString(IDS_ABOUTBOX);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    	
    	// TODO: Add extra initialization here
    
    	srand(time(NULL));
    	number = rand_mid(0, 25); 
    	
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    void CEDRsNumberGuessingGameDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else
    	{
    		CDialog::OnSysCommand(nID, lParam);
    	}
    }
    
    // If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.
    
    void CEDRsNumberGuessingGameDlg::OnPaint() 
    {
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
    
    		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    
    		// Center icon in client rectangle
    		int cxIcon = GetSystemMetrics(SM_CXICON);
    		int cyIcon = GetSystemMetrics(SM_CYICON);
    		CRect rect;
    		GetClientRect(&rect);
    		int x = (rect.Width() - cxIcon + 1) / 2;
    		int y = (rect.Height() - cyIcon + 1) / 2;
    
    		// Draw the icon
    		dc.DrawIcon(x, y, m_hIcon);
    	}
    	else
    	{
    		CDialog::OnPaint();
    	}
    }
    
    // The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CEDRsNumberGuessingGameDlg::OnQueryDragIcon()
    {
    	return (HCURSOR) m_hIcon;
    }
    
    void CEDRsNumberGuessingGameDlg::OnExit() 
    {
    	// TODO: Add your control notification handler code here
    	OnOK();
    }
    
    void CEDRsNumberGuessingGameDlg::OnSolve() 
    {
    	// TODO: Add your control notification handler code here
        
    		if(m_input < number)
    		{
    			MessageBox("Sorry, but your number is a little bit too low.  Please try again.", 
    				"A Little Low", MB_ICONINFORMATION);
    			tries++;
    		}
    		else
    		{
    			MessageBox("Sorry, but your number is a little bit too high.  Please try again.",
    				"A Little High", MB_ICONINFORMATION);
    			tries++;
    		}
    
    	if(m_input == number)
    	{
    		sprintf(buffer, "Congradulations!  You solved the number which was %d, and it only took you\n %d tries.",
    			number, tries);
    
    		MessageBox(buffer, "You guessed right!", MB_ICONINFORMATION);
    	}
    	
    	
    }
    
    int rand_mid(int low, int high)
    {
    	return low + rand()%(high - low + 1);
    }
    [/b]
    Basically this is a number guessing game which generates a random number at start-up and asks the user for input. The problem i have is that no matter what number the user inputs, it will always give the "your number is too low" MessageBox. In this example, i set it to generate a number between 0 and 25 (using code from this very web site) but even if the user types in his/her guess as "9999" it will still give the "Your number is too low" message.

    Any help would be GREATLY appreciated. Thanks.

    EDIT: included the full source.
    Last edited by -leech-; 01-14-2002 at 06:42 PM.
    Not yet, have to think of one...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guessing Game - Problem
    By Slynet in forum C Programming
    Replies: 12
    Last Post: 02-10-2009, 03:27 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. die game with random number
    By bartinla in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2004, 03:29 PM
  5. Guessing Number Game Problem
    By cgod in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 07:05 AM