# Using a Temp Variable, Map

Page 1 of 1

## 1 Replies - 301 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=c899e72502bfd2a05641855321e6803a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 smilesthunder

• New D.I.C Head

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!

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

• Code herder

Reputation: 4373
• Posts: 14,118
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }