Quick Question on if statement.

Hello, yeah I am trying to work on program and I feel like there could be a better way of writing this? I am Trying to learn good techniques.t
Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    if(haul >= bestHP) {
      if (haul == bestHP) {
        if (theDistance > bestDistance) {
          bestA = coordinateA;
          bestB = coordinateB;
          bestC = coordinateC;
          bestDistance = theDistance;
          bestHP = haul;
          bestPlanet = planetName;
        }
      }
      bestA = coordinateA;
      bestB = coordinateB;
      bestC = coordinateC;
      bestDistance = theDistance;
      bestHP = haul;
      bestPlanet = planetName;
    }
Last edited on
You can read about the switch statement here:
http://www.cplusplus.com/doc/tutorial/control/
(scroll down towards the bottom)

convert.at(i) is basically the same as convert[i] in this case.
Thank you much appreciated, I didn't know they were similar.
I added one more quick question if anyone knows the answer to it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(haul >= bestHP) 
{
     if (haul == bestHP && theDistance > bestDistance) 
     {
          bestA = coordinateA;
          bestB = coordinateB;
          bestC = coordinateC;
          bestDistance = theDistance;
          bestHP = haul;
          bestPlanet = planetName;
      }
      bestA = coordinateA;
      bestB = coordinateB;
      bestC = coordinateC;
      bestDistance = theDistance;
      bestHP = haul;
      bestPlanet = planetName;
}
Last edited on
What is the point of lines 2 - 11 in the OP (or 3 - 11 in Mats' reworking)? What do they achieve that isn't being done anyway in lines 12 - 17? The entire thing could be written as:

1
2
3
4
5
6
7
8
9
if(haul >= bestHP) 
{
  bestA = coordinateA;
  bestB = coordinateB;
  bestC = coordinateC;
  bestDistance = theDistance;
  bestHP = haul;
  bestPlanet = planetName;
}


Is this a homework question?
Last edited on
no I am writing a program. This is my first language and I am working on trying to simplify and edit my code. Now that I know what needs to be done I need to work on simplifying my code.
I put your code and replaced it with mine MikeyBoy. I think it was late when I was coding this, as my code was just crappy logic. Thank you.
Topic archived. No new replies allowed.