1 Replies - 217 Views - Last Post: 21 March 2013 - 05:07 PM Rate Topic: -----

#1 smilesthunder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-October 12

Using a Temp Variable, Map

Posted 21 March 2013 - 05:04 PM

Hello all!

I am trying to have a file that reads in planet locations (completely fictional) and calculates the distance between them all with a roadTrip function. However, I am having trouble when it comes to three or more planets, and I suspect it is how I am using the variables. Galaxy[i][j] is just the distance between planet A and B laid out on an array (chart).

Thanks!


Here is the roadTrip function

int Graph::roadTrip(int input, int start)
{
  int distance = 0; //distance between planets (light years)
  
  
  distance = Galaxy[start][input];
  
  
  return distance;
}


Here is the main function
int main(int argc, char* argv[])
{
  int input = 0;
  int totalDistance = 0;
  int start = 0; //planet you choose to start from
  
  Graph test(argv[1]);
  
  test.printLocations();
  
  
  cout << endl;
  
  cout << "Enter the planets you want to travel to. -1 to end." << endl;
  cout << "What planet do you want to begin at?" << endl;
  cin >> start;
  
  while(input != -1)
  {
    cout << "Next planet?" << endl;
    cin >> input;
    
    totalDistance = test.roadTrip(input, start) + totalDistance;
    
    
  }
  
  
  cout << "Distance is " << totalDistance << " light years.";
  
  
  
  
}


Is This A Good Question/Topic? 0
  • +

Replies To: Using a Temp Variable, Map

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Using a Temp Variable, Map

Posted 21 March 2013 - 05:07 PM

You never update your start variable after you go from planet A to planet B.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1