Thread: Please help me

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

    Please help me

    When I use the code as follows:

    struct node{
    int data;
    int expn;
    node *next;
    }
    node *input(){
    cout<<"Please input the datas:"<<endl;
    cout<<"data: expn:"<<endl;
    node *root,*p;
    root=NULL;
    int data,expn;
    while((data!=0)&&(expn!=0)){
    cin>>data>>expn;
    if (data==0 && expn==0) break;
    p=new node;
    p->data=data;
    p->expn=expn;
    p->next=NULL;
    insert(root,p);
    }
    return root;
    }

    The fuction is used to creating a linked list .When I use the function twice:
    Just like
    node *A,*B;
    A=input();
    B=input();
    The second call will be not in use.
    Could someone help me?
    Thanks a lot!

  2. #2
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    When I use the code as follows:

    Code:
    struct node
    { 
        int data; 
        int expn; 
        node *next; 
    }
     
    node *input()
    { 
        cout<<"Please input the datas:"<<endl; 
        cout<<"data: expn:"<<endl; 
        node *root,*p; 
        root=NULL; 
        int data,expn;
     
        while((data!=0)&&(expn!=0))
        { 
            cin>>data>>expn; 
            if (data==0 && expn==0) break; 
            p=new node; 
            p->data=data; 
            p->expn=expn; 
            p->next=NULL; 
            insert(root,p); 
        }
     
        return root; 
    }
    The fuction is used to creating a linked list .When I use the function twice:
    Just like
    node *A,*B;
    A=input();
    B=input();
    The second call will be not in use.
    Could someone help me?
    Thanks a lot!

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    If I haven't misunderstood you, I have made some modifications to your initial program.
    Code:
    struct node
    { 
       int data; 
       int expn; 
       node *next;
    }; <---- YOU NEED A SEMICOLON
    
    //---------------------------------------------------------
    node*  input()
    {
      node *head, *curr;
      int data, expn; 
      
      head=curr=NULL; 
    
      cout<<"Please input the datas:"<<endl; 
      cout<<"data: expn:"<<endl; 
      cin >> data >> expn;  // <-- USED FOR INITIAL LOOP ENTRY
    
      while((data!=0)&&(expn!=0))
      { 
    
          if (head==NULL)  //  <--- USED ONLY THE FIRST TIME
          {
              head=new node;
              head->data=data;
              head->expn=expn;
              head->next=NULL;
              curr=head;
          }
          else     // <-- ALL OTHER NODES WILL BE CREATED HERE
          {
              curr->next=new node; 
              curr=curr->next;
              curr->data=data; 
              curr->expn=expn; 
              curr->next=NULL; 
          }
    
          clrscr ();
          cout << "Please input the datas: " << endl; 
          cout << "data: expn: " << endl; 
          cin >> data >> expn;
       } 
       return head;
    }
    Hope this helps

    Sophie

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    5
    Thank you.
    Infact the first call
    A=input();
    works,
    but it doesn't work a second time.
    the call B=input(); could output the
    string,but I cannot input anything at
    all,it passes to next line.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    So......have you still got a problem or is it solved?
    simple is always an understatement.....

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    5
    I know now, thanks.
    I got a mistake and I did not initilize the
    data and expn.
    Is it?
    Thanks for your help!

Popular pages Recent additions subscribe to a feed