I put the counters but still doesn't work 
Author Message
 I put the counters but still doesn't work

Hi,

Okay, I put 3 counters for the 3 fruit but now I'm getting a very long
number like "Apple was pressed 64864768768 times". I declare the three
counters in the class declaration, initialized them in OnInitDialog, and in
the ChangeDialogTitle function, I receive it as int x (which I also declared
and initialized to zero).

Is it because it's not being passed properly. It has something to do with
that or in the format string of Cstring. In the Format string, the words
print out all right, but not the number where I have the %d specifier.
strCaption.Format(" was pressed %d times", x);

So now that I've done as you suggested, could you suggest something else
that is wrong, and how I might fix it.

Many thanks

// Buttons1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Buttons1.h"
#include "Buttons1Dlg.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()

Quote:
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
 //{{AFX_DATA_INIT(CAboutDlg)
 //}}AFX_DATA_INIT

Quote:
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CAboutDlg)
 //}}AFX_DATA_MAP

Quote:
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 //{{AFX_MSG_MAP(CAboutDlg)
  // No message handlers
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////////
/
// CButtons1Dlg dialog

CButtons1Dlg::CButtons1Dlg(CWnd* pParent /*=NULL*/)
 : CDialog(CButtons1Dlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CButtons1Dlg)
  // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

Quote:
}

void CButtons1Dlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CButtons1Dlg)
  // NOTE: the ClassWizard will add DDX and DDV calls here
 //}}AFX_DATA_MAP

Quote:
}

BEGIN_MESSAGE_MAP(CButtons1Dlg, CDialog)
 //{{AFX_MSG_MAP(CButtons1Dlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDC_SHOW_HIDE, OnShowHide)
 ON_BN_CLICKED(IDC_ENABLE_DISABLE, OnEnableDisable)
 ON_BN_CLICKED(IDC_APPLE, OnApple)
 ON_BN_CLICKED(IDC_BANANA, OnBanana)
 ON_BN_CLICKED(IDC_STRAWBERRY, OnStrawberry)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////////
/
// CButtons1Dlg message handlers

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

 // Add "About..." menu item to system menu.

 //THE COUNTERS ARE GIVEN AN INITIAL VALUE OF ZERO

 int x = 0;
 int m_StrawberryCount = 0;
 int m_BananaCount = 0;
 int m_AppleCount = 0;

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

Quote:
}

void CButtons1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialog::OnSysCommand(nID, lParam);
 }

Quote:
}

// 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 CButtons1Dlg::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();
 }

Quote:
}

// The system calls this to obtain the cursor to display while the user
drags
//  the minimized window.
HCURSOR CButtons1Dlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;

Quote:
}

void CButtons1Dlg::OnShowHide()
{
 BOOL bVisible = GetDlgItem(IDC_LEFT)->IsWindowVisible();

 GetDlgItem(IDC_LEFT)->ShowWindow(bVisible ? SW_HIDE : SW_SHOW);

 GetDlgItem(IDC_CENTER)->ShowWindow(bVisible ? SW_HIDE : SW_SHOW);

 GetDlgItem(IDC_RIGHT)->ShowWindow(bVisible ? SW_HIDE : SW_SHOW);

 GetDlgItem(IDC_SHOW_HIDE)->SetWindowText(bVisible ? "Show Fruits" : "Hide
Fruits");

Quote:
}

void CButtons1Dlg::ChangeDialogTitle(UINT nID, int x)

{
 CString strCaption;

 GetDlgItem(nID)->GetWindowText(strCaption);

 CString name = strCaption;

 strCaption.Format(" was pressed %ld times", x);

 name += strCaption;

 strCaption = name;

 SetWindowText(strCaption);

Quote:
}

void CButtons1Dlg::OnEnableDisable()
{
  BOOL bState = GetDlgItem(IDC_LEFT)->IsWindowEnabled();

  GetDlgItem(IDC_LEFT)->EnableWindow(!bState);

  GetDlgItem(IDC_CENTER)->EnableWindow(!bState);

  GetDlgItem(IDC_RIGHT)->EnableWindow(!bState);

  GetDlgItem(IDC_ENABLE_DISABLE)->SetWindowText(bState ? "Enable" :
"Disable");

Quote:
}

  //*** FUNCTION TO INCREMENT COUNTER***

//DEL void CButtons1Dlg::OnButtonCount()
//DEL {
//DEL  m_nCounter++;
//DEL }

//*** FUNCTION TO COUNT TIMES BUTTON PRESSED AND TO PRINT CAPTION
void CButtons1Dlg::OnApple()
{

 ++m_AppleCount;

 ChangeDialogTitle(IDC_APPLE, m_AppleCount);

Quote:
}

//*** FUNCTION TO COUNT TIMES BUTTON PRESSED AND TO PRINT CAPTION

void CButtons1Dlg::OnBanana()
{
 ++m_BananaCount;
 ChangeDialogTitle(IDC_BANANA, m_BananaCount);

Quote:
}

//*** FUNCTION TO COUNT TIMES BUTTON PRESSED AND TO PRINT CAPTION

void CButtons1Dlg::OnStrawberry()
{
 ++m_StrawberryCount;

 ChangeDialogTitle(IDC_STRAWBERRY, m_StrawberryCount);

Quote:
}



Thu, 09 Jan 2003 03:00:00 GMT  
 I put the counters but still doesn't work

could you suggest something else

Quote:
> that is wrong, and how I might fix it.

I think the main problem you have now is the *scope* of your new
variables.  I cant see where you declared them in your header files,
but I assume that they are in there somewhere?  If not there has to be
declarations of those 3 variables in the CButtons1Dlg.h header file.

You had:
int m_StrawberryCount = 0;   in your OnInitDialog function

but what you SHOULD have done was:

m_StrawberryCount = 0;

Your problem arises out of the fact that instead of setting your
variables equal to zero in OnInitDialog (which is what you think you've
done), youve actually declared new variables that only exist in
OnInitDialog and go out of scope when the OnInitDialog function ends.
That is, instead of having say, a m_StrawberryCount member variable
initialized to 0 what you have is TWO variables with that name, one
being a member of your class (still uninitialized) and the other being
a local variable of OnInitdialog which is 0 and which will soon
disappear!

So what you need to do is, in the .h file for your CButtons1Dlg class,
go:

private:

int m_StrawberryCount;
int m_BananaCount;
int m_AppleCount;

and then initialize them to zero as you did but *without* the "int" in
front of the variables.

As far as the int x = 0; line in OnInitDialog goes, you dont need it
becuase as above, x will soon enough go out of scope.  Also note that
you have "int x" appearing in your OnPaint function.  This wouldnt work
if it werent for variables having scope!  x is a different variable in
both instances and only exists while the function that declares it is
running.

I didnt compile up your source, but were your incredibly large numbers
at least incrementing the way they were supposed to?  If so, fixing the
scoping problems above should fix things.

Tom

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 09 Jan 2003 03:00:00 GMT  
 I put the counters but still doesn't work


Fri, 19 Jun 1992 00:00:00 GMT  
 I put the counters but still doesn't work
Thanks Tom,

The problem was the scope. Now let me see if I understand this. Since I had
in the header file three declarations for the counters, and then in
OnInitDialog I say "int AppleCounter = 0"and same for Banana and Strawberry,
the program interpreted the 3 declarations in OnInitDialog as three NEW
variables??? It must have because as soon as I removed the "ints" in
OnInitDialog, the problem was solved. So if I'm correct, if ever you want a
variable to be local in a function, just put a type in front of it and it
will go out of scope at the end of the function. Conversely, if you want the
variable to be accessible everywhere in the program, declare it only in the
header file and initialize it elsewhere.

So now that that works, I have to change the MFC icon in the "About Box",
and put my own name, version number, program name etc.. Now I have a
problem. I deleted the MFC resource icon and I must have deleted something
else because the little box at the top left of the title bar is now
gone--the one than when you click on it says "Move", "About" and something
else. I have to import an icon from my a:\ drive and put it where the MFC
icon was. It just won't show up. I know that it's somewhere because I can
see in the resource pane in the icon folder my icon. When I double-click on
it, it's there. But when I doubleclick the "AboutBox" resource, it's not.

My book doesn't explain this; only creating bitmap resources.

Would you or anyone else have any ideas as to how I can fix these two
problems.

Once again, many thanks


Quote:

>could you suggest something else
>> that is wrong, and how I might fix it.

>I think the main problem you have now is the *scope* of your new
>variables.  I cant see where you declared them in your header files,
>but I assume that they are in there somewhere?  If not there has to be
>declarations of those 3 variables in the CButtons1Dlg.h header file.

>You had:
>int m_StrawberryCount = 0;   in your OnInitDialog function

>but what you SHOULD have done was:

>m_StrawberryCount = 0;

>Your problem arises out of the fact that instead of setting your
>variables equal to zero in OnInitDialog (which is what you think you've
>done), youve actually declared new variables that only exist in
>OnInitDialog and go out of scope when the OnInitDialog function ends.
>That is, instead of having say, a m_StrawberryCount member variable
>initialized to 0 what you have is TWO variables with that name, one
>being a member of your class (still uninitialized) and the other being
>a local variable of OnInitdialog which is 0 and which will soon
>disappear!

>So what you need to do is, in the .h file for your CButtons1Dlg class,
>go:

>private:

>int m_StrawberryCount;
>int m_BananaCount;
>int m_AppleCount;

>and then initialize them to zero as you did but *without* the "int" in
>front of the variables.

>As far as the int x = 0; line in OnInitDialog goes, you dont need it
>becuase as above, x will soon enough go out of scope.  Also note that
>you have "int x" appearing in your OnPaint function.  This wouldnt work
>if it werent for variables having scope!  x is a different variable in
>both instances and only exists while the function that declares it is
>running.

>I didnt compile up your source, but were your incredibly large numbers
>at least incrementing the way they were supposed to?  If so, fixing the
>scoping problems above should fix things.

>Tom

>Sent via Deja.com http://www.deja.com/
>Before you buy.



Thu, 09 Jan 2003 03:00:00 GMT  
 I put the counters but still doesn't work
Now let me see if I understand this. Since I had

Quote:
> in the header file three declarations for the counters, and then in
> OnInitDialog I say "int AppleCounter = 0"and same for Banana and
Strawberry,
> the program interpreted the 3 declarations in OnInitDialog as three
NEW
> variables??? It must have because as soon as I removed the "ints" in
> OnInitDialog, the problem was solved. So if I'm correct, if ever you
want a
> variable to be local in a function, just put a type in front of it
and it
> will go out of scope at the end of the function. Conversely, if you
want the
> variable to be accessible everywhere in the program, declare it only
in the
> header file and initialize it elsewhere.

Basically.

Scope is useful because you usually want your variable names to make
some sense for the sake of readability.  So if it werent for scope you
could only have a given name once in a program (for example, you would
have to think of a dozen good names for a counter variable
besides "counter").  With scoping you can have 20 functions declaring a
variable "counter" in your class, and each one is has its own scope and
will automatically free the memory for them when the function ends.

Also note that your little program is sitting on top of several
megabytes of MFC source code (like CDialog, which your dialog class
derives from)  What if Microsoft created a private member of CDialog
called m_Counter?  With scoping, you can also have a member of your
class called m_Counter and have a function that declares a variable
called m_Counter and they would all have their own scope!  Also, your
program has something called a "stack" which a is a small amount (64K I
think) of memory that is reserved for variables that it declares.  You
have to be careful about how many variables you declare (and where) to
avoid exhausting this stack.

So if you want to make a counter variable to keep track of how many
times you've executed a loop inside a function, you would probably
create that counter INSIDE the function itself as opposed to making it
a member of the class.  That way the stack space for the variable is
available to other things when that function is not running.  Also it
makes your program easier to understand.  On the other hand things that
you need to keep track of between function calls (like how many times
the Strawberrybutton has been pressed) logically need to be class
members.  Also note that you could declare those variables in your
header file BEFORE the class declaration. That would make them exist
outside the scope of the class and they would still work the same way
they do now.  This wouldnt be good programming practice though. Just
thought Id let you know!

- Show quoted text -

Quote:
> So now that that works, I have to change the MFC icon in the "About
Box",
> and put my own name, version number, program name etc.. Now I have a
> problem. I deleted the MFC resource icon and I must have deleted
something
> else because the little box at the top left of the title bar is now
> gone--the one than when you click on it says "Move", "About" and
something
> else. I have to import an icon from my a:\ drive and put it where the
MFC
> icon was. It just won't show up. I know that it's somewhere because I
can
> see in the resource pane in the icon folder my icon. When I double-
click on
> it, it's there. But when I doubleclick the "AboutBox" resource, it's
not.

> My book doesn't explain this; only creating bitmap resources.

> Would you or anyone else have any ideas as to how I can fix these two
> problems.

Did you create another Icon? If so, try naming it IDR_MAINFRAME.

Sometimes in my more cowardly moments, I just create another project
and transfer my source code pieces over to the new project!

Tom

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 10 Jan 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. SaveBarState (WORKS) LoadBarState(Doesn't Work)?

2. GetPrivateProfileSectionNames works in 2000, doesn't work in 98 or ME

3. Tooltip works and then doesn't work

4. Q147740 CWnd work-around doesn't work with control that uses lic file

5. ?CDaoRecordset::GetFieldValue doesn't put string in .bstrVal

6. VC7: cstdio doesn't put printf in std

7. Calling ADO's GetRows() from VC doesn't work

8. Don't know why this doesn't work, but

9. TERMWAIT Win 3.1 sample doesn't work under '95

10. printf, puts, gets etc don't work when stepping into

11. Wht Doesn't This File download Work?

12. Sort doesn't work on bound data

 

 
Powered by phpBB® Forum Software