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

  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...

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    can you paste all your code.. i don't think there's enough there to find the problem.

    attach it as a zip or something... and i'll have a look at it.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    Well, i figured that would be enough since the rest is the standard MFC stuff.

    Check the original post for the re-edit.
    Not yet, have to think of one...

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    well... i think i see your problem:

    the only point that m_input is being changed is in the constructor of the class.... and it's set to 0.

    so if number is bigger than zero (which is always is) then:
    if(m_input < number)
    will always come out true, and
    MessageBox("Sorry, but your number is a little bit too low. Please try again.",
    "A Little Low", MB_ICONINFORMATION);
    tries++;

    will always get executed!

    in OnSolve() you need to extract the value from the user (get the value from an edit field or something?)

    then compare that value to the number.

    hope this helps.
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    Originally posted by Uraldor

    in OnSolve() you need to extract the value from the user (get the value from an edit field or something?)
    How would i extract the value? I've done other Windows programs that invole edit boxes before and i've never had a problem like this...
    Not yet, have to think of one...

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    try something like this:

    Code:
    void CEDRsNumberGuessingGameDlg::OnSolve()
    {
    	// TODO: Add your control notification handler code here
    	char buffer[64];
    	editBox.GetLine(0, buffer, sizeof(buffer));
    	m_input = atoi(buffer);
       
    	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);
    	}
    }
    good luck!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  7. #7
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    editBox.GetLine(0, buffer, sizeof(buffer));

    My compiler (using VC++ 6 by the way) says that "EditBox" is an undeclared identifier...
    Not yet, have to think of one...

  8. #8
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    Oh man, i'm so stupid. I found out what i forgot to do:

    UpdateData(TRUE);

    I always forget that when i'm working with Windows...

    Here's the final code:
    Code:
    	UpdateData(TRUE);
    
    		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);
    }
    Thanks for your help.
    Not yet, have to think of one...

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    My compiler (using VC++ 6 by the way) says that "EditBox" is an undeclared identifier...
    thats a pretty stupid thing to say, it was an example!!! i didn't say "cut and paste this into your code and it will work"... the idea behind it was to get you to get the value out of the edit box first so you could test it against your generated number. It's not the code, it's the principle behind it.

    AH!! SO UpdateData() is a function that gets all the data from your dialog box and stores it into variables for you!!!

    No wonder i can't stand MFC!!!!

    I hate MFC... i think i'll say that again

    I hate MFC!!!

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

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