Money Dispenser print out least amount of bills possible

I need some help. I'm supposed to create a code of an atm machine that dispenses the least amount of bills.
I'm running into the problem where program not printing out $80 dollars in the least amount of bills. it just prints out $50.
i need it to print $50 and $20 and $10
Any help is much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
//  main.cpp
//  money dispenser 2
//
//  Created by Jason Pulido on 3/3/15.
//  Copyright (c) 2015 Jason Pulido. All rights reserved.
//

#include <iostream>
void dispense(int moneyEntered,int tens, int twenties, int fiftys);


int main(int argc, const char * argv[]) {
    //variable declaration
    int moneyEntered, tens, twenties, fiftys;
    //
    dispense(moneyEntered, tens, twenties, fiftys);
    
    
    
    return 0;
}

void dispense(int moneyEntered,int tens, int twenties, int fiftys)
{

    
    printf("Please enter amount desired to withdrawl (Increments of 10 only) :$");
    scanf("%d", &moneyEntered);
    
   if(((moneyEntered ==10) && (moneyEntered > 0)))
   {
    tens = moneyEntered / 10;
    printf("tens : %d\n", tens);
   }
    if(((moneyEntered >=20) && (moneyEntered < 50)))
    {
    twenties = moneyEntered / 20;
    printf("twenties : %d\n", twenties);
    }
    
   if(((moneyEntered >=50) && (moneyEntered > 0)))
   {
    fiftys = moneyEntered / 50;
    printf("fiftys : %d\n", fiftys);
   }
    
}
Think of your dispense problem a little differently. Take out the biggest denomination first, and be sure to keep track of what you take!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void dispense(int moneyEntered)
{
    int fifties(0), twenties(0), tens(0);
    //initially, we haven't dispensed any money, so we still
    //have the total money left to dispense
    int moneyLeftToDispense = moneyEntered;

    if(moneyLeftToDispense >= 50)
    {
        //take however many whole fifties you can
        fifties = moneyLeftToDispense / 50;

        //after this, the remainder (% operator) is what is left
        //to dispense with the other bills
        moneyLeftToDispense = (moneyLeftToDispense % 50);
    }

    if(moneyLeftToDispense >= 20)
    {
        twenties = moneyLeftToDispense / 20;
        moneyLeftToDispense = (moneyLeftToDispense % 20);
    }

    if(moneyLeftToDispense >= 10)
    {
        tens = moneyLeftToDispense / 10;
        moneyLeftToDispense = (moneyLeftToDispense % 10);
    }

    if(moneyLeftToDispense > 0)
    {
        std::cout << "Whoops! Must not have entered a multiple of ten.\n";
    }
    
    std::cout << fifties << " x $50\n";
    std::cout << twenties << " x $20\n";
    std::cout << tens << " x $10\n";
}
Last edited on
Topic archived. No new replies allowed.