Thread: why is the input not working

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    why is the input not working

    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?


    &#91;code]&#91;/code]tagged by Salem
    Yes! its "code" not "quote"

  2. #2
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    *Goes into temporary insanity*

    Code tags!!!!!!!

    *manages to break out of the insanity*

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code tags are useless if you don't indent your code (have a block hiearchy).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Code tags are useless if you don't indent your code (have a block hiearchy).
    *looks at previous post*
    oops!!


    Yeah!!! Indent your code!!!!

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    void printFn (NODE *root)
    {
       if (!root)  <-- if(root != NULL)
       {
          DATA *x;
          x = root->dataPtr;
    
          printFn (root->left);
    
          printf("%s\t%s\n", x->name, x->phone);
    
          printFn (root->right);
       }
    }

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    71
    sweet
    got that part figured out as to why it prints and got the temporary variables to go correctly into the structure..


    now the problem is when i do a search, it directly says no data exists

    i am working to solve it but if anyone finds it before me, please do leave a msg

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    71
    forgot to give u the segment of code where the problem seems to lie


    Code:
    NODE *searchBST (NODE *root, char *name)
    {
    	DATA *dataIn;
    	
    	if (root == NULL)
    	{
    		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;
    		}
    	}
    }

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    root = searchBST (root, inputName);
    If searchBST can't find the name, the function returns a NULL pointer and root is pointing to it. I think you should change it to:
    Code:
    searchBST (root, inputName);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  2. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  3. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 12:32 PM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM