Hi, sorry, I didn't use the search
I prefer to get real answers from people, not look at already made answers. Anyway, here's the problem I'm having. My search tree isn't being created properly (or at all!). The problem lies with the 'searchPtr' in the 'sift' function. Anytime I use it for any comparison or assignment other than strcmp, the program bombs. I ran it through debug, and it looks like it's all OK, the value gets assigned just like it should, but it dies anyways!
I will post the important sections of the program, this is actually a Windows dialog program though, so it's kinda long and I don't want to take too much space. Any help would be appreciated!
(sorry for the lack of comments )
Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "phones.h"
#include "resource.h"
BOOL MainDialog_OnCommand(HWND, WORD, WORD, HWND);
BOOL MainDialogProc(HWND, UINT, WPARAM, LPARAM);
BOOL GetFileName(TCHAR *, int, HWND);
void initialize (HWND);
void sift(PTREE*);
void printAll(PTREE*, HWND);
TCHAR szFilename[_MAX_PATH];
HBITMAP hBitmap;
PTREE* index; //yeah yeah, it's global. Really the easiest (and best) way to do it.
....goes on with the windows stuff....
void initialize(HWND hWnd)
{
PTREE* newNodePtr;
FILE* ffp;
//try to open the file, Clean this up
if( (ffp = fopen( szFilename, "rb")) == NULL )
{
//Create popup dialog error box
}
SetDlgItemText(hWnd, IDT_TEXT, "In init");
index = (PTREE*) malloc (sizeof(PTREE));
fread(index, sizeof(PREC), 1, ffp);
index->left=NULL;
index->right=NULL;
newNodePtr = (PTREE*) malloc (sizeof(PTREE));
fread(newNodePtr, sizeof(PREC), 1, ffp);
newNodePtr->left=NULL;
newNodePtr->right=NULL;
while(!feof(ffp))
{
sift(newNodePtr);
newNodePtr = (PTREE*) malloc (sizeof(PTREE));
fread(newNodePtr, sizeof(PREC), 1, ffp);
newNodePtr->left=NULL;
newNodePtr->right=NULL;
}
free(newNodePtr);
fclose(ffp);
printAll(index, hWnd);
}
void sift(PTREE* newNodePtr)
{
PTREE* searchPtr, * attachHerePtr;
int done = 0;
searchPtr = index;
while(searchPtr != NULL);
{
attachHerePtr = searchPtr;
if(strcmp(newNodePtr->lname,searchPtr->lname)==-1)
searchPtr = searchPtr->left;
else
searchPtr = searchPtr->right;
}
if(strcmp(newNodePtr->lname,searchPtr->lname)==-1)
attachHerePtr->left = newNodePtr;
else
attachHerePtr->right = newNodePtr;
}
void printAll(PTREE* currentPtr, HWND hWnd)
{
static int index;//posibly use this for choice selection.
if(currentPtr->left != NULL)
printAll(currentPtr->left, hWnd);
index = SendDlgItemMessage(hWnd, IDT_TEXT, LB_ADDSTRING, 0, (LPARAM)currentPtr->lname);
if(currentPtr->right != NULL)
printAll(currentPtr->right, hWnd);
}