Thread: calculations

  1. #1
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28

    Talking calculations

    is there a way to calculate an amount
    on visual c++ ?
    using a dialog box mfc(exe)
    hello its me Wayko

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    could you be more specific?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28
    i want to subtract the current year 2001 from a year that a person inputs on a edit box.

    i called the edit box IDC_YEAR and the the member variable is m_Year

    i added the m_Age as a double
    i want m_Age to represnt 2001
    hello its me Wayko

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    you need to make this part of the class to work as it is.


    Code:
    #include <stdlib.h>
    
    void ClassNameHere::MakeCalcuation(void)
    {
        char Buf[21];
        char* stopper;
        CWnd* Item = GetDlgItem(IDC_YEAR);
        Item.GetWindowText(Buf,20);
        m_Age =  2001 - strtod(buf,stopper);
    }
    Last edited by no-one; 10-09-2001 at 12:52 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28
    this is my code i don't see where i am going wrong
    the answer suppose to subtract m_Age which is set to 2001 and m_Year where the user insert the number. after they hit submit they should get a popup window that states you are x amount years old but i get you are 0.00 years old

    // ageDlg.cpp : implementation file
    //

    #include "stdafx.h"
    #include "age.h"
    #include "ageDlg.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CAgeDlg dialog

    CAgeDlg::CAgeDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CAgeDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CAgeDlg)

    m_Year = NULL;
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    //initialize m_Age field
    m_Age = 2001;
    }


    void CAgeDlg:: DoDataExchange(CDataExchange* pDX)
    {
    CDialog:: DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAgeDlg)
    DDX_Control(pDX, IDC_SUBMIT, m_Submit);

    DDX_Text(pDX, IDC_year, m_Year);
    DDV_MaxChars(pDX, m_Year, 9999);
    //}}AFX_DATA_MAP
    }

    BEGIN_MESSAGE_MAP(CAgeDlg, CDialog)
    //{{AFX_MSG_MAP(CAgeDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_EN_CHANGE(IDC_year, OnChangeyear)
    ON_BN_CLICKED(IDC_SUBMIT, OnCancel)
    ON_BN_CLICKED(IDC_Submit1, OnSubmit)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CAgeDlg message handlers

    BOOL CAgeDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // 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
    }

    // 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 CAgeDlg::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 CAgeDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }

    void CAgeDlg::OnChangeyear()
    {
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO: Add your control notification handler code here
    }
    void CAgeDlg::OnCancel()
    {
    // TODO: Add your control notification handler code here
    CString s;
    s.Format("You are %.2f years old", m_Age);
    MessageBox(s, "Your age is...");

    CDialog::OnCancel();
    }

    void CAgeDlg::OnSubmit()
    {
    // 1.
    CalculateAge();
    // 2.
    CString s;
    s.Format("You are %.2f years old", m_Age);

    }
    void CAgeDlg::CalculateAge()
    {
    if (m_Year < 2001)
    {
    m_Age -= m_Year;
    }
    }

    hello its me Wayko

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    change the OnSubmit() function to this

    Code:
    void CAgeDlg::OnSubmit() 
    { 
    // 1. 
        char Buf[21];
        char* stopper;
    // Get a pointer to the EditBox Control
        CWnd* Item = GetDlgItem(IDC_YEAR);
    // Get its Text 20 characters max
        Item.GetWindowText(Buf,20);
    // convert the string to a double
        m_Year =  strtod(buf,stopper);
        CalculateAge(); 
    // 2. 
        CString s; 
        s.Format("You are %.2f years old", m_Age); 
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    or use the GetDlgItemInt to simplify your code

    Code:
    void CAgeDlg::OnSubmit() 
    { 
    // 1. 
        m_Year =  GetDlgItemInt(IDC_YEAR);
        CalculateAge(); 
    // 2. 
        CString s; 
        s.Format("You are %.2f years old", m_Age); 
    }

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..or just call UpdateData() -

    Code:
    void CAgeDlg::OnSubmit() 
    { 
    // 1. 
        UpdateData(TRUE);
        CalculateAge(); 
    // 2. 
        CString s; 
        s.Format("You are %.2f years old", m_Age); 
    }
    zen

  9. #9
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28
    it works
    thanx for the help
    hello its me Wayko

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  2. calculations with pointers
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 01:46 PM
  3. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 08:43 AM
  4. simple arithmetic calculations
    By viciousv322 in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 07:13 AM
  5. How do I get these calculations correct?
    By nadeni0119 in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2003, 11:09 AM