the temp struct should contain the name and phone number but it working correctly
i will post the entire program and the segment of the code i think i am having trouble with.
btw, it is trying to input the name and phone number into a structure which is part of a node which is a part of a BST
this is the whole program for starters
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <crtdbg.h>
#define SUCCESS 1
#define FAIL 0
#define ADDRESSBOOK "input1.txt"
typedef struct listnode
{
void *dataPtr;
struct listnode *right;
struct listnode *left;
}NODE;
typedef struct
{
char name[25];
char phone[20];
}DATA;
void menuFn (NODE *root);
void userInput (NODE *root);
NODE *inputFile (NODE *root);
NODE *addBST (NODE *root, DATA *dataIn);
NODE *searchBST (NODE *root, char *name);
void printFn (NODE *root);
int main(void)
{
NODE *root = NULL;
if ((root = inputFile (root)))
{
menuFn (root);
}
if (_CrtDumpMemoryLeaks ())
{
printf("Memory Leak\n");
}
else
{
printf("\nNo Leak\n");
}
return 0;
}
NODE *inputFile (NODE *root)
{
FILE *fp;
DATA *temp;
char name[25];
char phone[20];
if ((fp = fopen (ADDRESSBOOK, "r")) != NULL)
{
while (fscanf(fp, "%s", name) != EOF)
{
temp = ((DATA*) malloc (sizeof (DATA)));
fscanf(fp, "%s", phone);
strcpy (temp->name, name);
strcpy (temp->name, phone);
printf("a)%s \t%s\n", temp->name, temp->phone);
printf("b)%s \t%s\n", name, phone);
root = addBST (root, temp);
}
}
else
{
printf("\a\nERROR OPENING FILE. PLEASE CHECK PATH.\n\n");
return 0;
}
fclose (fp);
return root;
}
void menuFn (NODE *root)
{
char option;
char upper;
char cut = 0;
while (option != 'Q')
{
printf("\t\tMenu\t\t\n\n");
printf("P.\tPrint all records in the tree\n");
printf("S.\tSearch for a name\n");
printf("Q.\tQuit and save the tree in the output text file\n\n");
printf("Please choose your option: ");
upper = getchar ();
option = toupper (upper);
switch (option)
{
case 'P':
{
printFn (root);
printf("\n");
break;
}
case 'S':
{
userInput (root);
break;
}
case 'Q':
{
printf("\nGoodbye\n");
return;
}
default:
{
printf("\n\aInvalid Option. Please try again\n");
break;
}
}
fflush (stdin);
}
return;
}
void userInput (NODE *root)
{
char inputName [25];
printf("Enter name of monster to search for: ");
gets (inputName);
root = searchBST (root, inputName);
return;
}
NODE *searchBST (NODE *root, char *name)
{
DATA *dataIn;
if (!root)
{
printf("\nNO DATA EXISTS\n\n\a");
return NULL;
}
else
{
dataIn = root->dataPtr;
if (strcmp (dataIn->name, name) < 0)
{
return searchBST (root->left, name);
}
else if (strcmp(dataIn->name, name) > 0)
{
return searchBST (root->right, name);
}
else
{
printf("DATA FOUND\n");
printf("%s\t\t%s\n", dataIn->name, dataIn->phone);
return root;
}
}
}
NODE *addBST (NODE *root, DATA *dataIn)
{
DATA *dataCmp;
if (!root)
{
root = ((NODE *)malloc (sizeof(NODE)));
root->dataPtr = dataIn;
root->left = NULL;
root->right = NULL;
}
else
{
dataCmp = root->dataPtr;
if (strcmp (dataIn->name, dataCmp->name) < 0)
{
root->left = addBST (root->left, dataIn);
}
else if (strcmp (dataIn->name, dataCmp->name) > 0)
{
root->right = addBST (root->right, dataIn);
}
}
return root;
}
void printFn (NODE *root)
{
if (!root)
{
DATA *x;
x = root->dataPtr;
printFn (root->left);
printf("%s\t%s\n", x->name, x->phone);
printFn (root->right);
}
}
now the problem i am experiencing seems to be somwhere around the inputFile function
Code:
NODE *inputFile (NODE *root)
{
FILE *fp;
DATA *temp;
char name[25];
char phone[20];
if ((fp = fopen (ADDRESSBOOK, "r")) != NULL)
{
while (fscanf(fp, "%s", name) != EOF)
{
temp = ((DATA*) malloc (sizeof (DATA)));
fscanf(fp, "%s", phone);
strcpy (temp->name, name);
strcpy (temp->name, phone);
printf("a)%s \t%s\n", temp->name, temp->phone);
printf("b)%s \t%s\n", name, phone);
root = addBST (root, temp);
}
}
else
{
printf("\a\nERROR OPENING FILE. PLEASE CHECK PATH.\n\n");
return 0;
}
fclose (fp);
return root;
}
here is the input file
http://www.mdofit.com/labs/15c/lab3/input.txt
i dont get it why it is printing garbage and furthermore, why isnt it going into the BST????
any suggestions?
[code][/code]tagged by Salem
Yes! its "code" not "quote"