0

I have an assignment where the user enters some money, and it supposed to give them the number of bills and coins they need, but I am stuck in the storing the change and getting the amount of change required.

The issue I have is I know I can't store coins as an integer, but if I try storing it as a double, it gets the error that is %mod can't be used with a double. Is there a way to extract the remainder once the initial bills have been accounted? With a double

The way it currently is if I enter 456.56 I will get 4 hundred dollar bills, 2 twenties, 1 ten etc. but nothing about the .56

/*This program will convert the amount of money entered by the user into the amount of bills and change*/

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
    const int HUNDRED = 100;
    const int TWENTY = 20;
    const int TEN = 10;
    const int FIVE = 5;
    const int DOLLAR = 1;
    const int QUARTER = 25;
    const int DIME = 10;
    const int NICKEL = 05;
    const int PENNY = 01;
    int changeAmount; 

    cout << "Enter amount of money to convert: $"; 
    cin >> changeAmount;

    cout << "\n";
    
    cout << "Numbe of 100 dollar bills: " << (int)changeAmount / HUNDRED << endl; 
    changeAmount = changeAmount % HUNDRED; 

    cout << "Numbe of 20 dollar bills: " << (int)changeAmount / TWENTY << endl;
    changeAmount = changeAmount % TWENTY;

    cout << "Numbe of 10 dollar bills: " << (int)changeAmount / TEN << endl;
    changeAmount = changeAmount % TEN;

    cout << "Numbe of 5 dollar bills: " << (int)changeAmount / FIVE << endl;
    changeAmount = changeAmount % FIVE;

    cout << "Numbe of 1 dollar bills: " << (int)changeAmount / DOLLAR << endl;
    changeAmount = changeAmount % DOLLAR;

    cout << "Numbe of Quarters: " << (int)changeAmount / QUARTER << endl;
    changeAmount = changeAmount % QUARTER;

    cout << "Numbe of Dimes: " << (int)changeAmount / DIME << endl;
    changeAmount = changeAmount % DIME;

    cout << "Numbe of Nickles: " << (int)changeAmount / NICKEL << endl;
    changeAmount = changeAmount % NICKEL;

    cout << "Numbe of Pennies: " << (int)changeAmount / PENNY << endl;
    changeAmount = changeAmount % PENNY;

    

        return 0;
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • `int` means _integer_. Your `cin >> changeAmount;` will not read `.56`, it will only read the _integer_. – Drew Dormann Sep 26 '22 at 00:16
  • 1
    " i know i can't store coins as an integer" -- who told you that, that's absolutely incorrect? 45656 is a perfectly cromulent integer representing 456.56, and it makes pretty much the shown code work, as is, with only cosmetic changes. Your job consists of simply figuring out the trivial way to accept input of "456.56", and turning it into "45656", and leave the rest of the shown code as is, shouldn't that be very obvious? – Sam Varshavchik Sep 26 '22 at 00:18
  • 1
    *but if I try storing it as a double* -- You should be working in pennies throughout the program. The `456.56` is only a textual representation of what you are trying to convert -- your goal would then be to turn `456.56` into the `int` value of `45656` and work with that. – PaulMcKenzie Sep 26 '22 at 00:26
  • Hmm I never saw it that way. But it makes complete sense. I would suspect there's a better way to do this because uicould see it being a disaster if this was a math problem. – needusername Sep 26 '22 at 01:12
  • `const int PENNY = 01;` note that numbers starting with a leading 0 is in octal. It wont hurt you with 01 but its best not to use it if you did not want octal. – drescherjm Sep 26 '22 at 01:52

2 Answers2

0

One thing you can do is multiply everything by 100 and deal with everything in cents. If you enter 456.56, the program will internally convert it to 45,656. Then, it will check for multiples of 100 dollars, also known as 10,000 cents. This continues for the rest of the dollar bills and coins. Note that you also must initialize changeAmount as a float or double since you are receiving a decimal value from cin. This is one way you can implement it:

/*This program will convert the amount of money entered by the user into the amount of bills and change*/

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
    const int HUNDRED = 10000;
    const int TWENTY = 2000;
    const int TEN = 1000;
    const int FIVE = 500;
    const int DOLLAR = 100;
    const int QUARTER = 25;
    const int DIME = 10;
    const int NICKEL = 5;
    const int PENNY = 1;
    double changeAmountDouble;

    cout << "Enter amount of money to convert: $"; 
    cin >> changeAmountDouble;

    int changeAmount = round(changeAmountDouble * 100);

    cout << "\n";
    
    cout << "Number of 100 dollar bills: " << (int)changeAmount / HUNDRED << endl; 
    changeAmount = changeAmount % HUNDRED; 

    cout << "Number of 20 dollar bills: " << (int)changeAmount / TWENTY << endl;
    changeAmount = changeAmount % TWENTY;

    cout << "Number of 10 dollar bills: " << (int)changeAmount / TEN << endl;
    changeAmount = changeAmount % TEN;

    cout << "Number of 5 dollar bills: " << (int)changeAmount / FIVE << endl;
    changeAmount = changeAmount % FIVE;

    cout << "Number of 1 dollar bills: " << (int)changeAmount / DOLLAR << endl;
    changeAmount = changeAmount % DOLLAR;

    cout << "Number of Quarters: " << (int)changeAmount / QUARTER << endl;
    changeAmount = changeAmount % QUARTER;

    cout << "Number of Dimes: " << (int)changeAmount / DIME << endl;
    changeAmount = changeAmount % DIME;

    cout << "Number of Nickles: " << (int)changeAmount / NICKEL << endl;
    changeAmount = changeAmount % NICKEL;

    cout << "Number of Pennies: " << (int)changeAmount / PENNY << endl;
    changeAmount = changeAmount % PENNY;

    

    return 0;
}
Nathan Jiang
  • 131
  • 1
  • 9
  • 1
    When you try to compile your version of the shown code, does it actually compile? Did you try compiling what you wrote? – Sam Varshavchik Sep 26 '22 at 00:26
  • 1
    Are you also aware that [floating point math is broken](https://stackoverflow.com/questions/588004/) and multiplying a double value, that appears to have only two digits after the decimal point, by 100 may not, necessarily, after a truncation to an `int` value, produce the expected results? That, for example, multiplying 456.56 by 100 might result in something like 45655.9999999, truncating the result to 45655? May not be this specific decimal point value, but it shouldn't be too hard to find the right one where the magic happens. – Sam Varshavchik Sep 26 '22 at 00:30
0

The % operator is for integers. You're looking for the fmod() function.

#include <cmath>

int main()
{
    double x = 6.3;
    double y = 2.0;
    double z = std::fmod(x,y);

}
ecraig12345
  • 2,328
  • 1
  • 19
  • 26