Thread: Help please im stuck

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    Help please im stuck

    Hi guys...having problems with a project...its supposed to add names and email addresses but when i go to add a third it falls over....
    Its driving me mental..lol

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX 30
    struct custdata * initcd(struct custdata *cdata);
    struct node * init(struct node *head);
    int menu (void);
    struct custdata * getdetails(struct custdata *,struct node *);
    struct node * add (struct node*,struct custdata *);
    struct node * display (struct node *head);
    struct node * sort (struct node *head);
    struct node * del (struct node *head);
    struct custdata
    {
    char cname[30];
    int numoforders;
    };
    
    struct node 
     {
           
     	struct node *next;
     	struct custdata *cdata;
     	
     }arrofpoints[MAX];
    
    int i;
    
    main()
    {
    int choice;
    struct node *head;
    struct custdata *cdata;
    i = 0;
    
    
    head = init(head);
    cdata = initcd(cdata);
    
    
       do
       {
        choice = menu();
           switch(choice)
           {
            case 1 : 
                   cdata = getdetails(cdata,head);
                   head = add(head,cdata);
                   break;
            case 2 :
                   head = del(head);
                   break;
            case 3 :
                   head = display(head);
                   break;
            case 4 :
                   head = sort(head);
                   break;
            case 5 :
                   break;
           }
       }while(choice !=5);
     
    }
    
    struct node * init(struct node *head)
    {
    head = NULL;
    
    return head;
    }
    
    struct custdata * initcd(struct custdata *cdata)
    {
    cdata = NULL;
    return cdata;
    }
    
    struct custdata * getdetails(struct custdata *cdata,struct node *head)
    {
    
      cdata = malloc(100);
    
              printf("Enter Customer Name to add to record \n");
              scanf("%s",cdata->cname);
    		   fflush ( stdin );
              printf("Enter Number of Orders required to add to record \n");
              scanf("%d",&cdata->numoforders);
    		   fflush ( stdin );
              return cdata;
     
    }
    
    
    
    
    int menu (void) 
    {
    int choice;
    
    printf("Enter number 1 to add to queue \n");
    printf("Enter number 2 to delete from queue \n");
    printf("Enter number 3 to display contents \n");
    printf("Enter number 4 to sort contents \n");
    printf("Enter number 5 to exit program \n");
    scanf("%d",&choice);
    
    return choice;
    
    }
    
    struct node * add (struct node *head,struct custdata *cdata)
    {
    
    
    //iterate to last record arrpoints
    
    int z;
    z = 0;
      if(head == NULL)
      {
    
    
        head=malloc(30);
        head->cdata = cdata;
        head->next = NULL;
    
      
       arrofpoints[z].cdata = cdata;
       
       z++;
        
         return head;
      }else
       
      {
      
    
    head->next=add(head->next , cdata);
    
      
      }
    }
    
    struct node * display (struct node *head)
    {
    int j;
      for(j=0; j<=MAX; j++)
      {
       if(arrofpoints[j].cdata !=NULL)
       {
       printf(" Customer Name is : %s \n",arrofpoints[j].cdata->cname);
       printf(" Num of Orders is : %d \n",arrofpoints[j].cdata->numoforders);
       } else {
       printf("End of Information  ! \n");
       return head;
       }
      }
    
    
    
          
    
      
    }
    
    struct node * sort (struct node *head)
    {
    int z;
    int i;
    int x;
    int value;
     char tempcname[20];
      char tempemail[30];
      int tempnumoforders;
    
     
      for ( z=0; z<MAX; z++) /*initial for which controls the number of passes and the initial numbering*/
      {
      value=0;
     
    
     
        for (x=z+1; x<MAX; x++) /*this for loop is always one ahead from the previous for thus controlling numbers to be sorted*/
        {
       //<
          if(arrofpoints[x].cdata != NULL)
          {
         
            
           
         if (strcmp(arrofpoints[z].cdata->cname,arrofpoints[x].cdata->cname)>0)
         {
          printf("in if \n");
         
        strcpy(tempcname,arrofpoints[z].cdata->cname);
        tempnumoforders = arrofpoints[z].cdata->numoforders;
        value=1;
          
               while (value==1) 
               {
          
                strcpy(arrofpoints[z].cdata->cname,arrofpoints[x].cdata->cname);
                arrofpoints[z].cdata->numoforders=arrofpoints[x].cdata->numoforders;
                
                strcpy(arrofpoints[x].cdata->cname,tempcname);
                arrofpoints[x].cdata->numoforders = tempnumoforders;
               
                value=0;
              }
          
        }
        
         
        }   
      } 
    }
      
    }
    
    
    
    
    struct node * del (struct node *head)
    {
    char tcname[20];
    int i;
    int j;
    int x;
    
    int index;
    int flag;
    int flag2;
    flag = 0;
    flag2 = 0;
    j = 0;
    
    index = 0;
    printf("Enter the Name of the Person you wish to remove from the list \n");
    scanf("%s",tcname);
    printf("Checking ..... \n");
      for(x = 0; x<=MAX; x++)
      {
        if(flag == 0)
        {
        if(arrofpoints[x].cdata != NULL)
        {
    		printf("is not null \n");
          if(strcmp(arrofpoints[x].cdata->cname,tcname)==0)
          {
          printf("deleting user %s from the system!!\n",tcname);
           arrofpoints[x].cdata = NULL;
          flag =1;
          index = x;
          }else {
          printf("There is no user with the name %s \n",tcname);
          return head;
          }
        
        
          }
        }
    
      }
      
      if(flag == 1)
      {
       
          for(j = index; j <MAX; j++)
          {
           if(flag2 == 0)
           {
             if(arrofpoints[j+1].cdata!=NULL)
             {
                printf("in sort \n");
             arrofpoints[j].cdata = arrofpoints[j+1].cdata;
             } else {
    			 printf("in sort \n");
             printf("All elements re-arranged \n");
             arrofpoints[j].cdata = NULL;
             flag2 =1;
            }
           }
          }
        }
    
    
     }
    Any ideas appreciated
    Last edited by ItsMeHere; 06-14-2006 at 04:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM