Thread: simple math in msvc++ 6

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    simple math in msvc++ 6

    hello im trying to do some simple math in a dlg window in msvc++ 6.

    anyway what i want to do is get 2 numbers input from a user multiply the first by 1 and the second by 1.5 and display the results. but it keeps putting them as 0. i am a newbie so i maybee doing this the wrong way.

    btw i know it seems pointless to be multiplying something by 1 but i this is just the number i want to use while figuring the program out.

    here is the code:

    Code:
    // battlesimv2Dlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "battlesimv2.h"
    #include "battlesimv2Dlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // 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()
    
    /////////////////////////////////////////////////////////////////////////////
    // CBattlesimv2Dlg dialog
    
    CBattlesimv2Dlg::CBattlesimv2Dlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CBattlesimv2Dlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CBattlesimv2Dlg)
    	m_DEF_POINTS = _T("");
    	m_OFFENSIVE_POINTS = _T("");
    	m_OPPONENTS_TROOPS = _T("");
    	m_RESULTS = _T("");
    	m_YOUR_TROOPS = _T("");
    
    	//}}AFX_DATA_INIT
    	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CBattlesimv2Dlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CBattlesimv2Dlg)
    	DDX_Text(pDX, IDC_DEFENSIVE_POINTS, m_DEF_POINTS);
    	DDX_Text(pDX, IDC_OFFENSIVE_POINTS, m_OFFENSIVE_POINTS);
    	DDX_Text(pDX, IDC_OPPONENTS_TROOPS, m_OPPONENTS_TROOPS);
    	DDX_Text(pDX, IDC_RESULTS, m_RESULTS);
    	DDX_Text(pDX, IDC_YOUR_TROOPS, m_YOUR_TROOPS);
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CBattlesimv2Dlg, CDialog)
    	//{{AFX_MSG_MAP(CBattlesimv2Dlg)
    	ON_WM_SYSCOMMAND()
    	ON_WM_DESTROY()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(IDC_BTN_CALCULATE, OnBtnCalculate)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CBattlesimv2Dlg message handlers
    
    BOOL CBattlesimv2Dlg::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
    	
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    void CBattlesimv2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else
    	{
    		CDialog::OnSysCommand(nID, lParam);
    	}
    }
    
    void CBattlesimv2Dlg::OnDestroy()
    {
    	WinHelp(0L, HELP_QUIT);
    	CDialog::OnDestroy();
    }
    
    // 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 CBattlesimv2Dlg::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 CBattlesimv2Dlg::OnQueryDragIcon()
    {
    	return (HCURSOR) m_hIcon;
    }
    
    void CBattlesimv2Dlg::OnBtnCalculate() 
    {
    	// TODO: Add your control notification handler code here
    	double offTroops, defTroops, multi1, multi2;
    	multi1 = 1;
    	multi2 = 1.5;
    	offTroops = atof(m_YOUR_TROOPS) * multi1;
    	defTroops = atof(m_OPPONENTS_TROOPS) * multi2;
    
    	
    	m_OFFENSIVE_POINTS.Format("%.0f", offTroops);
        m_DEF_POINTS.Format("%.0f", defTroops);
    
    	if (m_OFFENSIVE_POINTS > m_DEF_POINTS)
    		MessageBox("congrats you should win.");
    	else
    		MessageBox("sorry you will lose.");
    
    	UpdateData(FALSE);
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    void CBattlesimv2Dlg::OnBtnCalculate() 
    {
        // TODO: Add your control notification handler code here
        UpdateData(); // load up member vars with current set of values
        ....
    }
    gg

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    thanks mate it works... i can't beleave i struggled so long on something so simple... better go read myself a c++ book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to do simple math with float and int type
    By demuro1 in forum C Programming
    Replies: 16
    Last Post: 09-24-2008, 11:58 AM
  2. Getting bizarre results from simple math
    By mmyers1 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2004, 06:39 PM
  3. Simple Math Program
    By RazielX in forum C Programming
    Replies: 3
    Last Post: 03-04-2004, 06:22 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Simple, yet hard math computation
    By TheUrbanChico in forum C++ Programming
    Replies: 0
    Last Post: 08-31-2001, 06:45 PM