Thread: Dialog update conundrum

  1. #1
    Registered User
    Join Date
    Mar 2002

    Dialog update conundrum

    I'm coding a small Wind app to help my kids practise their fractions (and get me into Win app development). The app is based on a series of Dialogs because I have no need to save data. The dialog has some read-only edit boxes which display two randomly-generated fractions, for example 2/5 * 3/8, and edit boxes which receive the user's answer input.

    Each fraction is generated from a GenericFraction class which has a private numerator and denominator and overloaded arithmetical operators + - * /. The dialog initialization is shown below.

    BOOL CAddDlg::OnInitDialog()

    // on dialog initialization, disable the 'Next Question' button.

    // call for appropriate text for the Practise Results screen title
    ? "Addition Practise Results (Random Session)"
    : "Addition Practise Results");

    // Create values for first question. Instantiate two fractions for
    // the first question and populate the numerators and denominators
    GenericFraction *gf1Ptr = new GenericFraction(m_Scope);
    m_Num1 = gf1Ptr->getNumerator();
    m_Den1 = gf1Ptr->getDenominator();

    GenericFraction *gf2Ptr = new GenericFraction(m_Scope);
    m_Num2 = gf2Ptr->getNumerator();
    m_Den2 = gf2Ptr->getDenominator();

    // show the generated fraction values

    return TRUE; }

    Two new fractions are instantiated and their data stored. I have traced the program using debug and different fractions are generated (I have ensured this with code elsewhere), but when the dialog is instantiated, the second fraction numerator and denominator values are the same as the first fraction values on the screen. Why would that be? I have tried approaching this several ways. I must be missing something simple here.

  2. #2
    Registered User
    Join Date
    Aug 2001

    post some more code

    post the code for


  3. #3
    Registered User
    Join Date
    Mar 2002
    Thanks very much for taking an interest in my question.

    Here's the class declaration...

    // GenericFraction.h: interface for the GenericFraction class.

    #if !defined(AFX_GENERICFRACTION_H__1EAFFF63_2ECF_4253 _A17A_207FE05A4FF5__INCLUDED_)

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    class GenericFraction
    int getDenominator() const;
    int getNumerator() const;
    // Standard constructor receives scope value
    // Default destructor
    virtual ~GenericFraction();

    // overloaded operators
    GenericFraction operator +(const GenericFraction &);
    GenericFraction operator -(const GenericFraction &);
    GenericFraction operator *(const GenericFraction &);
    GenericFraction operator /(const GenericFraction &);


    // utility functions
    void generateValues(int); // upon object instantiation creates values
    // for numerator and denominator
    // form
    // data
    int m_Denominator;
    int m_Numerator;

    #endif // !defined(AFX_GENERICFRACTION_H__1EAFFF63_2ECF_4253 _A17A_207FE05A4FF5__INCLUDED_)

    and here's the class definition - note that I have only copied one of the four overloaded operators here since the code for calling on them isn't written yet and they therefore cannot play a part in the problem...

    // GenericFraction.cpp: implementation of the GenericFraction class.

    #include "stdafx.h"
    #include "Fractions.h"
    #include "GenericFraction.h"

    #include "stdlib.h" // for srand() function
    #include "time.h" // for time() function

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

    // Construction/Destruction

    // No validation of received values within the constructor
    GenericFraction::GenericFraction(int scope)
    { generateValues(scope); }

    // Default destructor
    GenericFraction::~GenericFraction() {}

    void GenericFraction::generateValues(int scope)
    // Randomly generate of numerator and denominator
    // seed the randomizer

    m_Numerator = (1 + (rand() % scope));
    m_Denominator = (2 + (rand() % scope));

    } while(m_Numerator == m_Denominator);

    int GenericFraction::getNumerator() const
    { return m_Numerator; }

    int GenericFraction::getDenominator() const
    { return m_Denominator; }

    GenericFraction GenericFraction::operator +(const GenericFraction &right)
    (*this).m_Numerator = (((*this).m_Numerator * right.getNumerator()) +
    (right.getNumerator() + (*this).m_Denominator));

    (*this).m_Denominator = ((*this).m_Denominator * right.getDenominator());

    // reduce the fraction to its lowest form

    return *this;

    I hope this helps! Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  3. Getting the position of a dialog without parent on the screen
    By stormbringer in forum Windows Programming
    Replies: 1
    Last Post: 08-27-2003, 02:59 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. MFC dialog application - how to update two windows
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 10-23-2001, 01:54 AM