-2

The part where you supposed to get your cents back doesn't want to work, I was trying to optimize as best as I can my code to have less code line.

Any help?

#include <iostream>
#include <math.h>
using namespace std;

int main() 
{

   double iAmount_due { 0 };

   double iGiven_money { 0 };

   double iMoney_back; 

   iMoney_back = iGiven_money - iAmount_due;

   int iMoney100 { 0 };
   int iMoney50  { 0 };
   int iMoney20  { 0 };
   int iMoney10  { 0 };
   int iMoney5   { 0 };
   int iMoney2   { 0 };
   int iMoney1   { 0 };
   int iCent25   { 0 }; 
   int iCent10   { 0 };
   int iCent5    { 0 };
   int iCent1    { 0 };

   cout << "Enter the amount due please: " << endl;

   cin >> iAmount_due;

   cout << "Enter the amount given please: " << endl;

   cin >> iGiven_money;


   if (iGiven_money >= iAmount_due) {

      iMoney_back = iGiven_money - iAmount_due;

      cout << "We will give you : " << iMoney_back << " $ back" << endl;

   }

   else {

      cout << "No money back" << endl;

   }


   while (iGiven_money >= iAmount_due) {

       iMoney100 = iMoney_back / 100;
       iMoney_back = (int)iMoney_back % 100;

       cout << "You will get: " << iMoney100 << " X 100 $ " << endl;

       iMoney50 = iMoney_back / 50;
       iMoney_back = (int)iMoney_back % 50;

       cout << "You will get: " << iMoney50 << " X 50 $ " << endl;

       iMoney20 = iMoney_back / 20;
       iMoney_back = (int)iMoney_back % 20;

       cout << "You will get: " << iMoney20 << " X 20 $ " << endl;

       iMoney10 = iMoney_back / 10;
       iMoney_back = (int)iMoney_back % 10;

       cout << "You will get: " << iMoney10 << " X 10 $ " << endl; 

       iMoney5 = iMoney_back / 5;
       iMoney_back = (int)iMoney_back % 5;

       cout << "You will get: " << iMoney5 << " X 5 $ " << endl; 


       iMoney2 = iMoney_back / 2;
       iMoney_back = (int)iMoney_back % 2;

       cout << "You will get: " << iMoney2 << " X 2 $ " << endl; 

       iMoney1 = iMoney_back / 1;
       iMoney_back = (int) iMoney_back % 1;

       cout << "You will get: " << iMoney1 << " X 1 $ " << endl;


       iCent25 = iMoney_back / 0.25;
       iMoney_back = fmod ((int)iMoney_back, 0.25);

       cout << "You will get: " << iCent25 << " X 0.25 $ " << endl;


       iCent10 = iMoney_back / 0.10;
       iMoney_back = fmod ((int)iMoney_back , 0.10);

       cout << "You will get: " << iCent10 << " X 0.10 $ " << endl;


       iCent5 = iMoney_back / 0.5;
       iMoney_back = fmod ((int)iMoney_back , 0.5);

       cout << "You will get: " << iCent5 << " X 0.5 $ " << endl;

       iCent1 = iMoney_back / 0.1;
       iMoney_back = fmod ((int)iMoney_back, 0.1);

       cout << "You will get: " << iCent1 << " X 0.1 $ " << endl;

       break;

   }

   return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    What does "doesn't want to work" mean? – scohe001 Jan 31 '20 at 21:07
  • Like the part where it’s supposed give you back the cents doesn’t want to work – Computer Guy Jan 31 '20 at 21:08
  • And sometimes the decimals doesn’t appear properly sometimes it’s 0.5 and sometimes 0.05 – Computer Guy Jan 31 '20 at 21:10
  • Okay...but what does that mean? It doesn't compile? There's some kind of runtime error? You expect it to say "25 cents" and it says "27 cents"? – scohe001 Jan 31 '20 at 21:10
  • It does compile but at the end it doesn’t give you the amount of cents like you will get 145.57$ they will give you the 145$ but not the 0.57€ – Computer Guy Jan 31 '20 at 21:11
  • This sounds like you need to learn how to use a debugger. – drescherjm Jan 31 '20 at 21:13
  • What do you mean ? – Computer Guy Jan 31 '20 at 21:13
  • With a debugger you can step through the code line by line and look at the result of the calculation line by line. That way you can easily spot where the bug is. – drescherjm Jan 31 '20 at 21:14
  • Is there any reason why you can’t use a modulus with a double ? And in which situation do you really need to use double ? – Computer Guy Jan 31 '20 at 21:19
  • ***Is there any reason why you can’t use a modulus with a double ?*** Because it does not work for doubles. – drescherjm Jan 31 '20 at 21:22
  • You’re absolutely right It doesn’t work I was talking more in a technical way like why according to the logic of c++ it doesn’t make sense – Computer Guy Jan 31 '20 at 21:24
  • Related: [https://stackoverflow.com/questions/9138790/cant-use-modulus-on-doubles](https://stackoverflow.com/questions/9138790/cant-use-modulus-on-doubles) – drescherjm Jan 31 '20 at 21:26
  • 1
    You should not use `double` for money. Instead use `unsigned long long` and `dollars = money / 100; cents = money % 100;` – S.S. Anne Jan 31 '20 at 21:28
  • Is unsigned long a int type that can deal with decimals ? – Computer Guy Jan 31 '20 at 21:30
  • 1
    ***Is unsigned long a int type that can deal with decimals ?*** @scohe001 explained how to do that in the last part of the answer. Basically you multiply the numbers * 100 so all of your math is in whole cents. When you go to display you then convert cents to dollars. – drescherjm Jan 31 '20 at 21:32

1 Answers1

1

Here's your problem:

iMoney_back = (int)iMoney_back % 100;

Let me guess: you tried iMoney_back % 100 and found out that you can't use mod (%) with a double. So you just cast it to an int.

While this will compile just fine, the result of that modular arithmetic will still be an int. In other words, you just lost the decimal component of your iMoney_back, so to your program, it looks like no change is due!

You probably want fmod():

iMoney_back = fmod(iMoney_back, 100);

In addition, it looks like you've actually written .1 for your 1 cent values instead of .01. Fixing those and a slight rounding error on the cent will show you this works in action: https://ideone.com/0cWcuQ


Alternatively, you could make an iMoney_back_cents variable that's an int so that you don't even need to worry about doubles (which can cause some problems with imprecision):

int iMoney_back_cents = (iMoney_back * 100) + .5;
scohe001
  • 15,110
  • 2
  • 31
  • 51