Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Works in console, doesn't in VC++

Status
Not open for further replies.

wireFrame9

Technical User
Joined
Jun 13, 2003
Messages
2
Location
EG
I wrote the program initially for console. The contents of the onButton1 method should add an element to a linked list. It WORKS in the console. All I did was to copy the contents of the console function to the onButton1 method. I also copied the structure definition and the declaration of pointers as is.

OnButton2 is used to check that the first 2 elements were entered correctly, which I input their ->value as 0 and 7.

It sometimes crashes with illegal operation. Sometimes it doesn't, but adding only 1 element, and sometimes it works!!




#include "stdafx.h"
#include "112.h"
#include "112Dlg.h"

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

class mintermTag
{
public:
int value;
int group;
int isEssential;
int coverage;

mintermTag *next;
};

mintermTag *headMintermA, *headMintermB, *newMinterm, *nextMinterm,
*currentMinterm, *currentMinterm2, *temporaryMinterm,
*nextGroupMinterm, *headOfEssentials;

/////////////////////////////////////////////////////////////////////////////
// CMy112Dlg dialog

CMy112Dlg::CMy112Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy112Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy112Dlg)
m_nScannedValue = 0;
m_nNumberOfVariables = 4;
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy112Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy112Dlg)
DDX_Text(pDX, IDC_EDIT1, m_nScannedValue);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy112Dlg, CDialog)
//{{AFX_MSG_MAP(CMy112Dlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy112Dlg message handlers

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

SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

CenterWindow(GetDesktopWindow()); // center to the hpc screen

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}



void CMy112Dlg::OnButton1()
{
UpdateData(TRUE);

// if ( !test_input_validity(decimalValue) )
// return 101;

int decimalValue = m_nScannedValue;
int requiredGroup;
int numberOfOnes = 0;

while (decimalValue)
{
if ( decimalValue& 1 )
numberOfOnes++;
decimalValue = decimalValue>>1;
}

requiredGroup = numberOfOnes;

// if ( check_existance(decimalValue, requiredGroup) )
// return 102;

newMinterm = new mintermTag;
newMinterm->value = decimalValue;
newMinterm->group = requiredGroup;
newMinterm->isEssential = 1;
newMinterm->coverage = 0;

while ( currentMinterm->next != NULL)
currentMinterm = currentMinterm->next;

currentMinterm->next = newMinterm;
newMinterm->next = NULL;
}
else
{
currentMinterm = headMintermA;
nextMinterm = currentMinterm->next;

while ( ( currentMinterm->group != requiredGroup )
&& ( nextMinterm->group <= requiredGroup )
&& ( nextMinterm != NULL ) )
{
currentMinterm = currentMinterm->next;
nextMinterm = currentMinterm->next;
}

if ( currentMinterm->next != NULL )
{
newMinterm->next = currentMinterm->next;
currentMinterm->next = newMinterm;
}
else
{
newMinterm->next = NULL;
currentMinterm->next = newMinterm;

}

}
}

void CMy112Dlg::OnButton2()
{
CString YES = &quot;HELLOW WORLD!&quot;;
int counter = 0;
currentMinterm = headMintermA;
if ( currentMinterm->value == 0 )
counter++;
currentMinterm = currentMinterm->next;
if ( currentMinterm->value == 7 )
counter++;
if ( counter == 2 )
MessageBox(YES);
}
 
Hi,
i think your code for the following lines in
void CMy112Dlg::OnButton1() are not clear

Code:
    while ( currentMinterm->next != NULL)
      currentMinterm = currentMinterm->next;

    currentMinterm->next = newMinterm;
    newMinterm->next = NULL;
  }
  else
  {

where is if(..) for the above else
 
Sorry, I must have done something when I copied. Here's the correct one I'm using...

void CMainDlg::OnButton1()
{
UpdateData(TRUE);

// if ( !test_input_validity(decimalValue) )
// return 101;

int decimalValue = m_nScannedValue;
int requiredGroup;
int numberOfOnes = 0;

while (decimalValue)
{
if ( decimalValue& 1 )
numberOfOnes++;
decimalValue = decimalValue>>1;
}

requiredGroup = numberOfOnes;

// if ( check_existance(decimalValue, requiredGroup) )
// return 102;

newMinterm = new mintermTag;
newMinterm->value = decimalValue;
newMinterm->group = requiredGroup;
newMinterm->isEssential = 1;
newMinterm->coverage = 0;

if ( ( requiredGroup == 0 ) || ( headMintermA == NULL )
|| ( headMintermA->group > requiredGroup) )
{
newMinterm->next = headMintermA;
headMintermA = newMinterm;
}
else if ( requiredGroup == m_nNumberOfVariables )
{
currentMinterm = headMintermA;

while ( currentMinterm->next != NULL)
currentMinterm = currentMinterm->next;

currentMinterm->next = newMinterm;
newMinterm->next = NULL;
}
else
{
currentMinterm = headMintermA;
nextMinterm = currentMinterm->next;

while ( ( currentMinterm->group != requiredGroup )
&& ( nextMinterm->group <= requiredGroup )
&& ( nextMinterm != NULL ) )
{
currentMinterm = currentMinterm->next;
nextMinterm = currentMinterm->next;
}

if ( currentMinterm->next != NULL )
{
newMinterm->next = currentMinterm->next;
currentMinterm->next = newMinterm;
}
else
{
newMinterm->next = NULL;
currentMinterm->next = newMinterm;

}

}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top