Help, Any C++ Experts Out There?

LastStop

Senior member
Mar 11, 2002
268
0
0
Here's my problem:

I'm writting a windows app in C++ and I'm testing out the program to see if it works correctly as I go, it does. But low and behold, after adding a few more lines of code, the app isn't displaying what it's suppose to. It looks like a glitch or something. I've used the Visual C++ debugger and it also contradicts what the app is suppose to output. Am I overrunning a stack or something because the program is too large? Could it be that I'm referencing memory more than x bytes away?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Think man! Have you told us anything that would be useful in helping you? No!:)

Post some code.. or post the whole project (zipped and hosted somewhere). That would be a start.
 

LastStop

Senior member
Mar 11, 2002
268
0
0


<< Think man! Have you told us anything that would be useful in helping you? No!:)

Post some code.. or post the whole project (zipped and hosted somewhere). That would be a start.
>>



Here's the main section:


GregorianClass Greg, Orian;
JulianClass Jules, Verne;
char buffer[255];


LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInst;

HDC hDC;
PAINTSTRUCT ps;

switch(msg)
{
case WM_PAINT:
hDC = BeginPaint(hWnd,&ps);

Greg.setYear(2345); //Debug

wsprintf(buffer,"The Gregorian Date is: %02d/%02d/%04d",Greg.getMonth(),Greg.getDay(),Greg.getYear());
TextOut(hDC,10,10,buffer,strlen(buffer));
wsprintf(buffer,"The equivalent Julian Date is: %04d%03d",Verne.getYear(),Verne.getDay());
TextOut(hDC,10,30,buffer,strlen(buffer));

wsprintf(buffer,"The Julian Date is: %04d%03d",Jules.getYear(),Jules.getDay());
TextOut(hDC,10,60,buffer,strlen(buffer));
wsprintf(buffer,"The equivalent Gregorian Date is: %02d/%02d/%04d",Orian.getMonth(),Orian.getDay(),Orian.getYear());
TextOut(hDC,10,80,buffer,strlen(buffer));

EndPaint(hWnd,&ps);
break;

case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
break;

case WM_COMMAND:
switch(wParam)
{
case GREGORIAN:
DialogBox(hInst,"GregorianDlg",hWnd,GregorianProc);
Verne = Greg;
InvalidateRect(hWnd,NULL,TRUE);
break;

case JULIAN:
DialogBox(hInst,"JulianDlg",hWnd,JulianProc);
Orian = Jules;
InvalidateRect(hWnd,NULL,TRUE);
break;

case EXIT:
PostQuitMessage(0);
break;
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return(DefWindowProc (hWnd, msg, wParam, lParam));
}

return 0L;
}

BOOL CALLBACK GregorianProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam)
{

switch(msg)
{
case WM_INITDIALOG:
return TRUE;
break;

case WM_COMMAND:
switch(wParam)
{
case IDOK:
GetDlgItemText(hDlg,MONTH,buffer,strlen(buffer));
Greg.setMonth(atoi(buffer));
GetDlgItemText(hDlg,DAY,buffer,strlen(buffer));
Greg.setDay(atoi(buffer));
GetDlgItemText(hDlg,YEAR,buffer,strlen(buffer));
Greg.setYear(atoi(buffer));

EndDialog(hDlg,NULL);
break;

case IDCANCEL:
EndDialog(hDlg,NULL);
break;
}
break;

default:
return FALSE;
}

return TRUE;
}

BOOL CALLBACK JulianProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
return TRUE;
break;

case WM_COMMAND:
switch(wParam)
{
case IDOK:
GetDlgItemText(hDlg,JULIANBOX,buffer,strlen(buffer));
MessageBox(hDlg,buffer,"Hello",MB_OK);
EndDialog(hDlg,NULL);
break;

case IDCANCEL:
EndDialog(hDlg,NULL);
break;
}
break;

default:
return FALSE;
}

return TRUE;
}


That's pretty much the entire program except for window creation code.

Heres the classes I wrote:

class JulianClass;

class GregorianClass
{
int month, day, year;


public:
GregorianClass(){month=1; day=1; year=1900;}
GregorianClass(GregorianClass &x){month = x.getMonth(); day = x.getDay(); year = x.getYear();}

int getMonth(){return month;}
int getDay(){return day;}
int getYear(){return year;}
void setMonth(int x){month = x;}
void setDay(int x){day = x;}
void setYear(int x){year = x;}
operator JulianClass();
};


class JulianClass
{
int year, day;

public:
JulianClass(){year=1900; day=1;}
JulianClass(JulianClass &x){year = x.getYear(); day = x.getDay();}

void setYear(int x){year = x;}
void setDay(int x){day = x;}
int getYear(){return year;}
int getDay(){return day;}
operator GregorianClass();
};


This is program is suppose to demonstrate data conversion between two objects.

 

LastStop

Senior member
Mar 11, 2002
268
0
0


<< Any reason you're *not* using MFC? >>



I'm still learning the Windows API. The class I'm writting this for goes in to MFC at the end.

BTW I think I've found out what I did wrong. It appears that garbage is still in the buffer when I set the data. Why can't it ever be computer error :)
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Bare API's not bad, except that it take *too long* to develop the interface/visual stuff. That, and the code is usually pretty ugly. I've noticed that many beginning programmers turn away from Windows development when they are introduced directly to API programming.
 

manly

Lifer
Jan 25, 2000
13,589
4,239
136
That's because we all know the Win32 API is wretched.

I'm a weenie Java developer, so I can't help you here, but one part of writing correct code is unit testing.

JUnit is a simple open-source framework for writing unit tests for Java code. It's recently been ported to .NOT and called NUnit. There are commercial unit testing packages you can buy as well.

If you want to read a great book on the subject, get Martin Fowler's Refactoring.