Printing Denomination to User

I am having trouble on a hw problem. I must compute total amount a user inputs into cents. And display the amount needed in quarters, dimes, ect. But am having difficulty with the division portion. Here is my code

Dollars = Total/100;
Quarters = Dollars%4;
Dimes = Quarters/10;
Nickels = Dimes%5;
Pennies = Nickles/1;

My integers are
int Total;
int Dollars;
int Quarters;
int Dimes;
int Nickels;
int Pennies;
First, When working with money instead of using int you use float so it can give you the values beyond the dot Example:
1
2
INT A = 1; // cannot have a value like this 6.98
FLOAT B = 9.99; // CAN have values like this > 9.9 < AND this > 13 < 


{Still Needed in this case if inputted goes below 100 in the code below}

EDIT: Why do you divide nickels by 1 if its gonna return the same value again ???, Why do you put % instead of / ??? Why do you divide from the last obtained value instead of keep dividing by Total ??? i think im gonna give you a sample code-- (Status: Working)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main ()
{
    float Total; 
    float Dollars; 
    float Quarters; 
    float Dimes; 
    float Nickels; 
    
    cout << "Input the amount of cents(This will calculate how many dollars,Quartes,Dimes (etc...).. needed: ";
    cin >> Total;
    Dollars = Total/100;
    Quarters = Total/25;
    Dimes = Total/10;
    Nickels = Total/5;
    cout << "Total Dollars Needed: " << Dollars;
    cout << "\nTotal Quarters Needed: " <<  Quarters;
    cout << "\nTotal Dimes Needed: " <<  Dimes;
    cout << "\nTotal Nickels Needed: " << Nickels;
    getchar();
    cin.ignore();
}    


Last edited on
okay I will try that. Right now I have it compiled and it runs great SOMEWHAT. It asks for an amount I enter lets say " 100 " but when it computes i get 0 for Quarters/Dimes/Nickels/Pennies. so where is the computation going wrong?
mpruisu wrote:
First, When working with money instead of using int you use float so it can give you the values beyond the dot

Bull. When working with money you use fixed-point numbers. Using floating-point causes no end of headaches.

@Pizzaboi
The problem you are having is in your calculations. Think about the math some:

Subtotal = Total;

Dollars = Subtotal / 100;
Subtotal = Subtotal - Dollars;

Quarters = Subtotal / 25;
Subtotal = Subtotal - (Quarters * 25);

...

Hope this helps.
So I have another issue. I am working the numbers but when I put in 100 in the total cents when running the program it comes back saying

Dollars = 1
Quarters = 4
Dimes = 10
Nickels = 20
Pennies = 100

My goal is to make it where I put in 100 it just uses the first Int. which is Dollars. So it would just be

Dollars = 1
Quarters = 0
Dimes = 0
Nickels = 0
Pennies = 0

I need to use the Modulus function but am unsure how to use it in my script.

For instance if it use puts in 137 cents it should come up

Dollars = 1
Quarters = 1
Dimes = 1
Nickels = 0
Pennies = 2

If somebody could explain the modulus function a little more clear to me that would be great.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int total_cents = ...;

int dollars = total_cents / 100; //integer division, will truncate the quotient
total_cents %= 100; //remove the money added to dollars from total_cents

int quarters = total_cents / 25;
total_cents %= 25; //remove all quarters from total_cents

int dimes = total_cents / 10;
total_cents %= 10; //remove all dimes

int nickels = total_cents / 5;
total_cents %= 5; //remove all nickels

int pennies = total_cents; //add remaining cents to pennies, no need to divide by 1
total_cents = 0; //same as total_cents %= 1; 
Topic archived. No new replies allowed.