Home > Software design >  Program that converts money into bills
Program that converts money into bills

Time:09-26

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;
}

CodePudding user response:

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;
}

CodePudding user response:

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);

}
  •  Tags:  
  • c
  • Related