Pointer Help

Joined
Sep 24, 2008
Messages
1
Reaction score
0
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

Problem statement

A car journey consists of a set of towns and distances (nodes) that are visited in a particular sequence. We require a C++ program that ‘manages’ a collection of nodes and reports the total distance for a given journey. To achieve this the program will request the user to enter each town name and its distance from the last point entered. The first town name entered is ‘start’. When a town name entry of ‘end’ is encountered the program will output to the screen each of the towns visited and the total distance covered by the current journey. The following listing provides an example of how the program is to operate:

Enter town> staRT
Enter town> starT
Enter town> west wyalong
Enter distance from start> 140
Enter town> forbes
Enter distance from west wyalong> 110
Enter town> paRKes
Enter distance from forbes> 60
Enter town> end

Towns visited> west wyalong, forbes, parkes
Distance travelled> 310

Enter town> dubbo
Enter distance from parkes> 110
Enter town> end

Towns visited> west wyalong, forbes, parkes, dubbo
Distance travelled> 420

Enter town> halt

Towns visited> west wyalong, forbes, parkes, dubbo
Distance travelled> 420

Program Terminated.

If ‘start’ is entered at any point for the town name it will cause the logging of towns and distances to be re-initialised. Thus if ‘start’ (any lower or upper case combination accepted) was the provided entry for the last prompt above it would cause the commencement of a new journey.

An entry of ‘halt’ causes your program to terminate. Prior to terminating your program will list the towns in the current journey and the total accumulated distance via the linked structure. In effect an entry of ‘halt’ has the same effect as entering ‘end’ and then terminating.

If an entry other than ‘start’, ‘halt’ or ‘end’ was provided then the new entry would be appended to those shown. Refer carefully to the example to note some potential entries that need to be handled by your program.

In completing this assignment you must create a dynamically linked structure. The nodes in your linked structure will store as data a town name and a distance. In addition each node will need to maintain a pointer to the next node. If there is no next node, a null will be stored. The structure is depicted with the following diagram:


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;
     }
     
     }
 
Joined
Oct 30, 2013
Messages
1
Reaction score
0
Need help

have you completed this question, if possible please can you provide me a solution .. will be thankful to you
Thank you
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top