So I am making a game in C++ and this is a portion of the code i am having a rough time with. So what i am trying to do is if your at a certain planet the fuel that you spend to go to the other planets change depending on where you are. pleae don't go too complicated
//variables of the game
string PlayerName;
string Planet = "Earth";
int fuel1; //fuel to earth
int fuel2; //fuel to mars
int fuel3; //fuel to Jupiter
int fuel4; //fuel to Saturn
int fuel5; //fuel to pluto
int fuel6; //fuel to system #467
int fuel7; //fuel to Langoon
int fuel8; //fuel to Pizzariatan
int fuel9; //fuel to Troiavis
int fuel10; //fuel to Skoustea
int fuel11; //fuel to Xopra
int fuel12; //fuel to Yolanda
int fuel13; //fuel to Divinity
int fuel14; //fuel to CandyLand
int fuel15; //fuel to Zeus
switch (planetN)
{
case 1: //this is earth
int fuel1 = 0; //here
int fuel2 = 100;
int fuel3 = 200;
int fuel4 = 300;
int fuel5 = 400;
int fuel6 = 750;
int fuel7 = 1000;
int fuel8 = 1500;
int fuel9 = 2500;
int fuel10 = 5000;
int fuel11 = 7500;
int fuel12 = 12000;
int fuel13 = 15000;
int fuel14 = 30000;
int fuel15 = 50000;
break;
case 2: //this is mars
int fuel1 = 100;
int fuel2 = 0; //here
int fuel3 = 100;
int fuel4 = 200;
int fuel5 = 300;
int fuel6 = 650;
int fuel7 = 900;
int fuel8 = 1400;
int fuel9 = 2400;
int fuel10 = 4900;
int fuel11 = 7400;
int fuel12 = 11900;
int fuel13 = 14900;
int fuel14 = 29900;
int fuel15 = 49900;
break;
case 3: //this is jupiter
int fuel1 = 200;
int fuel2 = 100;
int fuel3 = 0; //here
int fuel4 = 100;
int fuel5 = 200;
int fuel6 = 550;
int fuel7 = 800;
int fuel8 = 1300;
int fuel9 = 2300;
int fuel10 = 4800;
int fuel11 = 7300;
int fuel12 = 11800;
int fuel13 = 14800;
int fuel14 = 29800;
int fuel15 = 49800;
break;
case 4: //this is saturn
int fuel1 = 300;
int fuel2 = 200;
int fuel3 = 100;
int fuel4 = 0; //here
int fuel5 = 100;
int fuel6 = 450;
int fuel7 = 700;
int fuel8 = 1200;
int fuel9 = 2200;
int fuel10 = 4700;
int fuel11 = 7200;
int fuel12 = 11700;
int fuel13 = 14700;
int fuel14 = 29700;
int fuel15 = 49700;
break;
case 5: //this is pluto
int fuel1 = 400;
int fuel2 = 300;
int fuel3 = 200;
int fuel4 = 100;
int fuel5 = 0; //here
int fuel6 = 350;
int fuel7 = 600;
int fuel8 = 1100;
int fuel9 = 2100;
int fuel10 = 4600;
int fuel11 = 7100;
int fuel12 = 11600;
int fuel13 = 14600;
int fuel14 = 29600;
int fuel15 = 49600;
case 6: //this is system 467
int fuel1 = 750;
int fuel2 = 650;
int fuel3 = 550;
int fuel4 = 450;
int fuel5 = 350;
int fuel6 = 0; //here
int fuel7 = 250;
int fuel8 = 750;
int fuel9 = 1750;
int fuel10 = 4250;
int fuel11 = 6750;
int fuel12 = 11250;
int fuel13 = 14250;
int fuel14 = 29250;
int fuel15 = 49250;
break;
case 7: //this is Langoon
int fuel1 = 1000;
int fuel2 = 900;
int fuel3 = 800;
int fuel4 = 700;
int fuel5 = 600;
int fuel6 = 250;
int fuel7 = 0; //here
int fuel8 = 500;
int fuel9 = 1500;
int fuel10 = 4000;
int fuel11 = 6500;
int fuel12 = 11000;
int fuel13 = 14000;
int fuel14 = 29000;
int fuel15 = 49000;
break;
case 8: //this is Pizzariatan
int fuel1 = 1500;
int fuel2 = 1400;
int fuel3 = 1300;
int fuel4 = 1200;
int fuel5 = 1100;
int fuel6 = 750;
int fuel7 = 500;
int fuel8 = 0; //here
int fuel9 = 1000;
int fuel10 = 3500;
int fuel11 = 6000;
int fuel12 = 10500;
int fuel13 = 13500;
int fuel14 = 28500;
int fuel15 = 48500;
break;
case 9: //this is Troiavis
int fuel1 = 2500;
int fuel2 = 2400;
int fuel3 = 2300;
int fuel4 = 2200;
int fuel5 = 2100;
int fuel6 = 1750;
int fuel7 = 1500;
int fuel8 = 1000;
int fuel9 = 0; //here
int fuel10 = 2500;
int fuel11 = 5000;
int fuel12 = 9500;
int fuel13 = 12500;
int fuel14 = 27500;
int fuel15 = 47500;
break;
case 10: //this is skoustea
int fuel1 = 5000;
int fuel2 = 4900;
int fuel3 = 4800;
int fuel4 = 4700;
int fuel5 = 4600;
int fuel6 = 4250;
int fuel7 = 4000;
int fuel8 = 3500;
int fuel9 = 2500;
int fuel10 = 0; //here
int fuel11 = 2500;
int fuel12 = 7000;
int fuel13 = 10000;
int fuel14 = 25000;
int fuel15 = 45000;
break;
case 11: //this is Xopra
int fuel1 = 7500;
int fuel2 = 7400;
int fuel3 = 7300;
int fuel4 = 7200;
int fuel5 = 7100;
int fuel6 = 6750;
int fuel7 = 6500;
int fuel8 = 6000;
int fuel9 = 5000;
int fuel10 = 2500;
int fuel11 = 0; //here
int fuel12 = 4500;
int fuel13 = 7500;
int fuel14 = 22500;
int fuel15 = 42500;
case 12: //this is yolanda
int fuel1 = 12000;
int fuel2 = 11900;
int fuel3 = 11800;
int fuel4 = 11700;
int fuel5 = 11600;
int fuel6 = 11250;
int fuel7 = 11000;
int fuel8 = 10500;
int fuel9 = 9500;
int fuel10 = 7000;
int fuel11 = 4500;
int fuel12 = 0; //here
int fuel13 = 3000;
int fuel14 = 18000;
int fuel15 = 38000;
case 13: //this is divinty
int fuel1 = 15000;
int fuel2 = 14900;
int fuel3 = 14800;
int fuel4 = 14700;
int fuel5 = 14600;
int fuel6 = 14250;
int fuel7 = 14000;
int fuel8 = 13500;
int fuel9 = 12500;
int fuel10 = 10000;
int fuel11 = 7500;
int fuel12 = 3000;
int fuel13 = 0; //here
int fuel14 = 15000;
int fuel15 = 35000;
break;
case 14: //this is candyland
int fuel1 = 30000;
int fuel2 = 29900;
int fuel3 = 29800;
int fuel4 = 29700;
int fuel5 = 29600;
int fuel6 = 29250;
int fuel7 = 29000;
int fuel8 = 28500;
int fuel9 = 27500;
int fuel10 = 25000;
int fuel11 = 22500;
int fuel12 = 18000;
int fuel13 = 15000;
int fuel14 = 0; //here
int fuel15 = 20000;
break;
case 15: //this is zeus
int fuel1 = 50000;
int fuel2 = 49900;
int fuel3 = 49800;
int fuel4 = 49700;
int fuel5 = 49600;
int fuel6 = 49250;
int fuel7 = 49000;
int fuel8 = 48500;
int fuel9 = 47500;
int fuel10 = 45000;
int fuel11 = 42500;
int fuel12 = 38000;
int fuel13 = 35000;
int fuel14 = 20000;
int fuel15 = 0; //here
break;
default:
break;
}
1)
First off, I would say you are missing angle brackets {} to encase every complete case block, so a case could look like this:
case 15:
{
fuel1 = 50000;
... // this means and so on
fuel15 = 0;
break;
}
2)
Another potential issue I see is that you are declaring each integer in each case. You should do this only once (best programming practice) as you do so in the beginning before the switch statement begins.
3)
The third and final thing I notice is that your rough time may be with where setting fuel to 0 is the issue. I am GUESSING that it has to do with the logic of the code elsewhere in the game (costing 0 fuel to travel to the same destination planet as the starting planet).
If you need more clarification, we need more info :)
Disregard this if this isn't the entire program, but you need to declare your entry point (int main) somewhere.
Also, as nickeeromo said, you need angle brackets to encase each block of code in you're monolithic switch statement. You're also declaring each of your fuel variables again in each switch block when you don't need to. The way you're doing it, they are local to and only to the scope of the switch case. When the case ends, the variable is destroyed and the variables that you actually want to change are not modified. To remedy this, simply remove the type from before each identifier in the switch statement.
Lastly, indentation will make your life 100 times better I promise. ;)
Is it considered good programming practice to include them?
Heh...sorry...kinda new at this... That's what I do to help me look at it a little easier. I don't think it is (or is not for that matter) best programming practice. The last tutorial thing I was looking at had that (from an instructor), so I assumed it was good practice.
Anyways, I also noticed that the original post is missing a break statement at the ends of cases 5, 11, and 12. Another issue?