Thread: Help please im stuck

  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.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    cdata = malloc(30);
    Start by changing that line to: cdata = malloc( sizeof(cdata) );

    Also, you need a break in the first switch case. That's all I can see for the moment.

    [EDIT]
    Oh man, check out your init functions, they are bad. As you are sending a pointer, there is no need to return the value.

    In fact, you don't need to send it at all, as head is declared a global variable.
    Last edited by bivhitscar; 06-14-2006 at 03:37 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Also, you are using the 'new' keyword which is not used in C.

    And it would also help if you posted the complete code.

    Basically, the code is a bit of a mess.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    Sorry guys have edited to show the latest version i had before you guys posted.
    I know its all over place but im new to this so please be patient

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Well, your add() function is completely munted. I think you need to break the program up and tackle each problem one at a time. From here it looks like you just jumped right in and started writing bits and pieces all over the place.

    What you need to do is step through the code logically and find the obvious errors first and then ask for help with a specific area rather than the whole project.

    [EDIT]
    And if you are new to this, then I'd suggest leaving linked lists for another day and start with simpler applications of structs and dynamic memory allocation.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    Thanks for the pointers guys (no pun inteded )....started from fresh and managed to get it sorted......been at college (night classes) and have had 4 different lecturers this year with vairying knowlage....feel like ive learned nothing.....so please be gentle with me when i post something stupid again
    The help here is appreciated
    Last edited by ItsMeHere; 06-15-2006 at 03:19 AM.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    Just a quick point when i input a name it only sees the first name but not the second ie

    "Enter Customer Name to add to record" i add billy bob but when you display the record only billy shows
    any ideas?

    Code:
              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;

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Using scanf with the %s modifier will scan input until it detects a space, tab or newline character. Try using fgets instead, you can find it in the faq on this site.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

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