# Using a Temp Variable, Map

Page 1 of 1

## 1 Replies - 324 Views - Last Post: 21 March 2013 - 05:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316264&amp;s=3f61af5194fcb2455630e9a5b09d5a42&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 smilesthunder

Reputation: 0
• 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!

```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

• Code herder

Reputation: 4823
• Posts: 15,946
• 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.