I'm working on a generator and I've put the code (see code below) at every item and I come on a final ammount of 18k lines of code. I want to short this, since I need to make a lot of code more. Is there some way to put this code above or down the file and make a command look back at it?
Name = as above: M240, M249 etc.
AMMOUNTBUY = 1 till 9
CURRENCYBUY = Silver,silver10oz,gold,gold10oz
AMMOUNTSELL = 1 till 9
CURRENCYSELL = Silver,silver10oz,gold,gold10oz
How do I make that code generate? With arrays or ..? Or do I use functions?
I tried:
1. Using if () {} statement for every weapon, but then I end up with a load of lines.
2. Using Array's int weapon1[5] = {1,2,3,4,5}, but it didn't work since it only took the first weapon "M240".
3. Using the 'switch', but same as the first one, I end up with a load of lines.
I'll see if I can make this work, since I'm really a noob. I'm sorry if I annoy you with my lack of information, but I'm not sure which information to give you, since all of my code is almost the same except the weapon names.
Honestly what I think you should do is incorporate some object oriented programming(OOP). It can be something very simple like a plain old data (POD) struct like:
1 2 3 4 5 6 7 8 9 10
enum Currency { silver = 10, silver10oz = 100, gold = 50, gold10oz = 500 }; //what ever they are worth
struct Weapon
{
std::string name;
unsigned price;
}
//now you can use the silver, silver10oz, gold, or gold10oz to pay for the weapon (they must add up to the price)
you then want to put the weapons in a container of some sort. Possibly a vector
you could also use a std::map like std::map<std::string, unsigned> Weapons = { {"M240", 1250}, {"M249", 3210"} };
I like to have an uniform design when I code. So something like:
1 2 3 4 5 6 7 8 9
//output the list of weapons
//get user input on the desired weapon
//display the price of the weapon
//ask the user how they want to pay to get the sum (can be a few silver few gold ect..or all silver) if it is the correct sum accept payment, if it is more then give them a refund of lower currency
//give them weapon
Instead of rewriting the code several times with only minimal changes (the price, name of weapon, ect..)
Something like this where it doesn't matter what the weapon is. Keep in mind though it is still missing a bunch of stuff I tried to just create a simple example for you.