-1

I am creating a currency system that is based in 3 types of coins (Gold, Silver and Copper) it works as 100 copper coins converts into 1 silver coin, and, 100 silver coin converts into 1 gold coin.

I made an struct like this

struct Money
{
    Money();
    Money(int copper, int silver, int gold);
    Money(Money& m);

    Money operator + (Money const& obj);
    Money operator - (Money const& obj);

    void operator += (Money const& obj);
    void operator -= (Money const& obj);

    void Balance();

    void Print();
    int c, s, g;
};

But the problem comes with the method Balance(). This method does the math to convert all the copper to silver and silver to gold.

I made this but I'm looking for a better solution if somebody can find it. I think that the performance of my method is poor.

void Money::Balance()
{
    if (c > 99) { s += c / 100; c = c % 100; }
    if (s > 99) { g += s / 100; s = s % 100; }

    if (c < 0 && s >= 0 && g > 0) { 
        (c += ((c / 101) + 1) * 100); 
        s -= (c / -101) + 1; 
    }
    if (s < 0 && g > 0) { 
        (s += ((s / 101) +1) * 100); 
        g -= (c / -101) + 1; 
    }
}

EDIT: The last 2 if-blocks are for negative money to convert for example (50g 10s -1c) in (50g 9s 99c) and just have negative in the first currency type. To not have (50g -9s -2c)

EDIT: I checked that there is a bug that if you have g > 0, s = 0 and c < 0 it wont take a silver so it will show negative copper.

Thank you for your answers <3

I tried to convert 100 copper to 1 silver and 100 silver to 100 gold. I think that my solution works but the performance is poor.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
JMelter
  • 1
  • 2
  • 13
    Unless you actually care about how many gold, silver and copper pieces you have you can just store the amount of money you have as copper. That makes doing the math so much easier and then when it comes time to display how much you have then do the calculation to determine how much of each coin you have. – NathanOliver Dec 20 '22 at 15:04
  • 1
    The first two lines of `Balance` look fine. Not sure what the rest of it achieves. Is negative money a thing in your world? – john Dec 20 '22 at 15:04
  • Yes, sorry. Actually negative money is a thing in my world. – JMelter Dec 20 '22 at 15:08
  • 2
    @JMelter Store all money in coppers, if all coins are always freely convertible then that's the simple thing to do. When you display money, that's when you convert to gold and silver. (This is just a repeat of Nathan Oliver's comment, but it's the sensible thing to do). – john Dec 20 '22 at 15:32
  • `silver = (money/100) % 100`, then `gold = money / 10000`, and finally `copper = money % 100`. – Rogue Dec 20 '22 at 15:52
  • How should negative money be represented? "Only the highest unit is negative and the rest positive", or "all units negative"? Both are valid, and I'm uncertain which you want. Should `(0, 0, -503)` become `(0, -5, -3)` or should it become `(-1, 94, 97)`? – Mooing Duck Dec 20 '22 at 18:11

1 Answers1

0

Remove the last part of your code and it works fine:

void Money::Balance()
{
  // Convert excess copper coins to silver coins
  s += c / 100;
  c %= 100;

 // Convert excess silver coins to gold coins
  g += s / 100;
  s %= 100;
}
hNq
  • 71
  • 3
  • I think you have your calculations backwards, though if you want to swap your 100 gold coins for 1 silver coin I'm sure you'll be able to find lots of people willing to make that transaction – Alan Birtles Dec 20 '22 at 17:21
  • You are right, edited it sorry about that. – hNq Dec 20 '22 at 17:53
  • I'm pretty sure that this is.... quirky.... for negative values of `c` or `s`. `(0, 0, -503)` balances to `(0, -5, -3)`, which, though technically correct, is probably not what OP wants. https://stackoverflow.com/questions/11630321/why-does-c-output-negative-numbers-when-using-modulo – Mooing Duck Dec 20 '22 at 18:08