who can help me?

This is a discussion on who can help me? within the C Programming forums, part of the General Programming Boards category; i am doing my assignment, but ... here is my code: Code: #include<stdio.h> #include<string.h> #include<stdlib.h> struct node{ char* name; struct ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Angry who can help me?

    i am doing my assignment, but ...

    here is my code:
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct node{
       char* name;
       struct node *next;
       struct node *follow;
    };
    typedef struct node Node;
    
    Node* newNode(char*);
    void Search(char*,char*,char*,Node*);
    void print(Node*);
    
    int main(){
      char line[BUFSIZ];
      Node* student=newNode("");
    
      while(fgets(line,BUFSIZ,stdin)!=NULL){
         char* l= line;
         char person[BUFSIZ]="";
         char* p = person;
         char course[BUFSIZ]="";
         char* c = course;
         char mark[3]="";
         char* m = mark;
         while(isspace(*l)!= 0){
           l++;
         }
         int length = strlen(l);
         char* rLine = strchr(l,' ');
         int rlength = strlen(rLine);
         int i = length - rlength;
         strncpy(p,l,i);  /*name*/
         l += i;
         while(isspace(*l)!=0){
           l++;
         }
         length = strlen(l);
         rLine = strchr(l,' ');
         rlength = strlen(rLine);
         i = length - rlength;
         strncpy(c,l,i);  /*course*/
         l += i;
         while(isspace(*l)!=0){
            l++;
         }
         while(isspace(*l)==0){
           strncpy(m,l,1);
           m++;
           l++;
         }
         m = mark;
         Search(p,c,m,student);
      }
      print(student);
    }
    
    Node* newNode(char* name){
        Node* temp;
        temp=(Node*)malloc(sizeof(Node));
        strcpy(temp->name,name);
        temp->next = NULL;
        temp->follow = NULL;
        return temp;
    }
    
    void Search(char* person,char* course,char* mark,Node* student){
        Node* ptr = student;
        while(strcmp(person,ptr->name) !=0 && (ptr->next != NULL)){
          ptr = ptr->next;
        }
    
        if(strcmp(person,ptr->name)==0){
          Node* ptr1 = ptr->follow;
          while(strcmp(course,ptr1->name)!=0&&ptr1->next!=NULL){
              ptr1 = ptr1->next;
          }
          if(strcmp(course,ptr1->name)==0){
             Node* ptr2 = ptr1->follow;
             while(ptr2->next != NULL){
                ptr2 = ptr2->next;
             }
             ptr2->next = newNode(mark);
          }
          else if(ptr1->next == NULL){
             ptr1->next = newNode(course);
             ptr1 = ptr1->next;
             ptr1->follow = newNode(mark);
          }
       }
       else if(ptr->next == NULL){
         ptr->next = newNode(person);
         ptr = ptr->next;
         ptr->follow = newNode(course);
         ptr->follow->follow = newNode(mark);
       }
    }
    
    void print(Node* student){
       Node* ptr = student;
       ptr = ptr->next;
       for(ptr;ptr!=NULL;ptr=ptr->next){
          char output[BUFSIZ]="";
          char* out = output;
          strcat(out,ptr->name);
          strcat(out,":");
          Node* ptr1 = ptr->follow;
          for(ptr1;ptr1!=NULL;ptr1=ptr1->next){
            strcat(out,"  ");
            strcat(out,ptr1->name);
            Node* ptr2 = ptr1->follow;
            for(ptr2;ptr2!=NULL;ptr2=ptr2->next){
               strcat(out," ");
               strcat(out,ptr2->name);
            }
          }
          strcat(out,"\n");
          fputs(out,stdout);
       }
    }
    here is data:
    Jane CS1100 A
    Art CS1100 D
    Meng CS1100 A
    Art CS1100 C-
    Jane Math1000 B+
    Art CS1100 A-
    Art Math1000 A
    Meng CS1101 A
    Art CS1100 B-
    Meng CS1101 A+

    why i always get Mati1000 instead of Math1000?
    and, why strcmp(person, ptr->next) = -1, when person="Art" and
    ptr->next="Art"?

    thanks

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your function newNode() is wrong. You never allocate any space for name.

    >>and, why strcmp(person, ptr->next) = -1, when person="Art" and
    ptr->next="Art"?

    fgets keeps the '\n' at the end of the string.

    Kurt
    Last edited by ZuK; 04-26-2006 at 04:10 AM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    But, why strcmp(person,ptr->next) = 0, when person="Jane" and ptr->next = "Jane"?
    and by the way, how to allocate space for name?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    It's going to be a lot easier to get going with
    Code:
    struct node{
       char name[30];
       struct node *next;
       struct node *follow;
    };
    You don't have to allocate name, and you can just strcpy in the data when you create each new node.

    Later on, changing this to char* is a pretty simple task.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21