Pointer Help

Discussion in 'C Programming' started by sivapc, Sep 24, 2008.

  1. sivapc

    sivapc

    Joined:
    Sep 24, 2008
    Messages:
    1
    Hi Guys.. I'm very new to c++ and tbh, i've struggled in c++ since the start. I just want to ask about the difference between pointers and nodes. i just don;t understand the nodes function.

    I have an assignment to complete, i have done it half way but still don't know where to head after that. (I'm not asking for solution, just want an idea how can i work from it).

    Here's the question


    here's my solution (Obviously doesn't work well)

    Code:
    # include <iostream>
    
    
    struct town {
           char townName[20];
           int pos;
           double distance;
           
           town *next;
           
           
    };
    
    using namespace std;
    typedef town *townPtr;//datatype
    
    void insert (townPtr &destination, int inPos);
    
    int main()
    {
        townPtr destination; //(Variable)
        //initialize 
        destination = NULL;
        
        town *t1Ptr;
        t1Ptr = new town;
        
        if(t1Ptr == NULL)
        {
                 cout<<"Insufficient memory"<<endl;
                 exit(1);
        }
        
        char townName[20];
        double distance = 0.0;
        double total = 0.0;
        
        cout<<"Enter Town:>"<<flush;
        cin.getline(townName,20);
        
        while (townName != "Halt")
        {
              
              cout<<"Enter town:>"<<flush<<endl;
              cin.getline(townName,20);
              cout<<"Enter Distance:>"<<flush<<endl;
              cin>>distance ;
              total  = total + distance;
              cout<<"Total distance is:"<< total<<endl;
              cin.ignore ('n', 1);
              
              
        }
        
           
        
        cout<<"destination:"<<destination<<endl;
        insert(destination,1);
        
        cout<<"destination:"<<destination<<endl;
        insert(destination,2);
      
    
       
            
              
       
        system("pause");
        delete t1Ptr;
        return 0;
    }
    
    void insert (townPtr &destination, int inPos)
    {
        townPtr temp;
         //allocate space
         temp = new town;
         if (temp == NULL)
         {
                  cout<<"Insufficiene memory space"<<endl;
                  exit(1);
         }
         
         //store data
         temp -> pos = inPos;
         temp -> next = NULL;
         
         if(destination == NULL)
         destination = temp;
         else
         {
             temp -> next= destination;
             destination = temp;
         }
         
         }
    
     
    sivapc, Sep 24, 2008
    #1
    1. Advertising

  2. sivapc

    xusan

    Joined:
    Oct 30, 2013
    Messages:
    1
    Need help

    have you completed this question, if possible please can you provide me a solution .. will be thankful to you
    Thank you
     
    xusan, Oct 30, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. masood.iqbal@lycos.com

    Pointer-to-pointer-to-pointer question

    masood.iqbal@lycos.com, Feb 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    741
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    875
    Jordan Abel
    Mar 28, 2006
  3. bansalvikrant@gmail.com

    Pointer to pointer Vs References to Pointer

    bansalvikrant@gmail.com, Jul 2, 2009, in forum: C++
    Replies:
    4
    Views:
    1,325
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    656
  5. subramanian100in@yahoo.com, India

    pointer to an array vs pointer to pointer

    subramanian100in@yahoo.com, India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    496
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page