GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2008, 06:24
phylao phylao is offline
New Member
 
Join Date: Mar 2008
Posts: 12
phylao is an unknown quantity at this point

Problem with linked list and combo box


Hello,

I have written an application that allows add/retrieve/delete to the linked list and in the same time, update in the MFC dialog's combo box.

It has no problem deleting the first item in the combo box, but whenever I tried to delete the subsequent item, it shows in the combo box that the item is deleted, but in reality, only the first item in the linked list is deleted. It seems to delete the first item ONLY.

the code for the Add/Retrieve/Delete
CPP / C++ / C Code:
int CMainFrame::Add(AO_info *NewItem)
{
	AO_info *new_item = new AO_info;

	new_item = NewItem;
	new_item->AO_Next = AO_Head;
	AO_Head = new_item;

	// get linked list size
	int size = ((CMainFrame*)AfxGetMainWnd())->GetAO_Size();

	// increment size 
	size++;

	// set linked list size
	((CMainFrame*)AfxGetMainWnd())->SetAO_Size(size);

	return size;

}

AO_info *CMainFrame::Retrieve(int position)
{
	AO_info *Current = AO_Head;
	// get linked list size
	int size = ((CMainFrame*)AfxGetMainWnd())->GetAO_Size();

	for (int i = size - 1; i > position && Current != NULL; i--)
	{
		Current = Current->AO_Next;
	}
	return Current;
}

bool CMainFrame::Delete(int position)
{
	// get linked list size
	int size = ((CMainFrame*)AfxGetMainWnd())->GetAO_Size();

	if (Retrieve(position) == NULL)
		return false;
	else if (position == 0)
	{
		AO_info *current = Retrieve(1);
		current->AO_Next = NULL;
		size--;
		// set linked list size
		((CMainFrame*)AfxGetMainWnd())->SetAO_Size(size);
		delete current;
		return true;
	}
	else
	{
		Retrieve(position + 1)->AO_Next = Retrieve(position - 1);

		// decrement size
		size--;

		// set linked list
		((CMainFrame*)AfxGetMainWnd())->SetAO_Size(size);

		return true;
	}
}


here's the code that manage the dialog's GUI.

CPP / C++ / C Code:
BOOL CMap0::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// TODO: Add extra initialization here

	// retrieve linked list size
	unsigned int size = ((CMainFrame*)AfxGetMainWnd())->GetAO_Size();
	m_numcount = size;

	if (size > 0)
	{
		for (int i = 0; i < size; i++)
		{
			AO_info* one = ((CMainFrame*)AfxGetMainWnd())->Retrieve(i);

		// insert the item to the end of the list
			m_AO_range0.InsertString(-1, one->start_stop_azimuth);

		}


	}


	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}



void CMap0::OnAoadd() 
{
	// TODO: Add your control notification handler code here
	// add to the linked list
	AO_info *NewItem = new AO_info;

	CString range;
	char *range_local;

	range = "";
	strcpy(NewItem->start_stop_azimuth, range);

	UpdateData(TRUE);

	NewItem->start_azimuth = m_AO_StartAzimuth;
	NewItem->stop_azimuth = m_AO_StopAzimuth;

	sprintf(range_local, "%f to %f", m_AO_StartAzimuth, m_AO_StopAzimuth);
	range = range_local;
	strcpy(NewItem->start_stop_azimuth, range);

	int size = ((CMainFrame*)AfxGetMainWnd())->Add(NewItem);

	m_AO_range0.InsertString(-1, range);

	m_M0DataValid = range + " added";
	
	m_numcount = size;
	UpdateData(FALSE);
	return;	
}

void CMap0::OnAodelete() 
{
	// TODO: Add your control notification handler code here
	int pos = m_AO_range0.GetCurSel();

	int size = ((CMainFrame*)AfxGetMainWnd())->GetAO_Size();

	if (size == 0)
	{
		m_M0DataValid = "There is no data to be deleted!";
		UpdateData(FALSE);
	}
	else
	{
		((CMainFrame*)AfxGetMainWnd())->Delete(pos);
		m_M0DataValid = "Entry deleted!";
		UpdateData(FALSE);

		size = ((CMainFrame*)AfxGetMainWnd())->GetAO_Size();
	
		// update the new size 
		m_numcount = size;
		
		// delete string in combo box
		m_AO_range0.DeleteString(pos);

		// clear the other boxes
		m_AO_StartAzimuth = 0;
		m_AO_StopAzimuth = 0;

		UpdateData(FALSE);
	}

}


void CMap0::OnAoview() 
{
	// TODO: Add your control notification handler code here

	int	size = ((CMainFrame*)AfxGetMainWnd())->GetAO_Size();
	int pos = m_AO_range0.GetCurSel();
	if (pos < size)
	{
		AO_info* one = ((CMainFrame*)AfxGetMainWnd())->Retrieve(pos);

		m_AO_StartAzimuth = one->start_azimuth;
		m_AO_StopAzimuth = one->stop_azimuth;
		UpdateData(FALSE);
	}

}

I have tired numerous way to modify the Add/Retrieve/Delete function that are called in the CMainFrame but it always deleting the first item added to the list ONLY.

Please help! I am very desperate now... Thank you so much.
  #2  
Old 05-Apr-2008, 21:05
phylao phylao is offline
New Member
 
Join Date: Mar 2008
Posts: 12
phylao is an unknown quantity at this point

Re: Problem with linked list and combo box


can anyone give me some idea?
i almost did all trial and error and still can't make it work
  #3  
Old 24-Apr-2008, 13:56
phylao phylao is offline
New Member
 
Join Date: Mar 2008
Posts: 12
phylao is an unknown quantity at this point

Re: Problem with linked list and combo box


I still cannot solve this problem...is anyone able to help please? Thanks so much!
  #4  
Old 24-Apr-2008, 14:11
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Problem with linked list and combo box


Here is at least one of the problems that your code has:

CPP / C++ / C Code:
int CMainFrame::Add(AO_info *NewItem)
{
	AO_info *new_item = new AO_info;

	new_item = NewItem;// What are you trying to do here?
This is how you create memory leaks. If your goal is to make a copy of NewItem, then you should change that line to:
CPP / C++ / C Code:
*new_item = *NewItem;
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C 2D array problem. Noob. Weasel C Programming Language 4 17-Oct-2007 02:26
Combo box problem. benbuleong MS Visual C++ / MFC Forum 0 13-Jul-2007 13:34
help with ClistCtrl with combo boxes dvieb MS Visual C++ / MFC Forum 1 14-Feb-2007 06:39
how to create a combo box and list banur22 MS Visual C++ / MFC Forum 1 06-Apr-2005 11:44
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.