fork download
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
 
using namespace std;
 
int main()
{
    unsigned change = time(0);
        int quarters = 0;
        int dime = 0;
        int nickel = 0;
        int penny = 0;
 
        //Get a random number from 0-100
        srand(change);
        change = change % 100;
 
 
        // If change is greater than or equal to one run the program
        if (change != 0)
        {
       
                // using fixed and set precision to eliminate need for decimal fixing.
                cout << fixed << setprecision(2);
               
                cout << "Change Due:" << setw(4) << "$" << change / 100.0 << "\n";
                cout << "Coin Dispenser will dispense:\n";
 
                    // Calculate and adjust variables
                        quarters = change / 25;
                        change = change % 25;
                       
                        dime = change / 10;
                        change = change % 10;
               
                        nickel = change / 5;
                        change = change % 5;
                       
                        penny = change / 1;
 
                        if (quarters != 0)
                        {              
                                cout << " Quarters: " << quarters << " (" << "$" << quarters * 25 / 100.0 << ")" << "\n";
                        }
 
                                if (dime != 0)
                                {
                                        cout << " Dimes:" << setw(5) << dime << " (" << "$" << dime * 10 / 100.0 << ")" << "\n";
                                }
 
 
                                        if (nickel != 0)
                                        {
                                                cout << " Nickels:" << setw(3) << nickel << " (" << "$" << nickel * 5 / 100.0 << ")" << "\n";
                                        }
 
       
                                if (penny != 0)
                                 {
                                        cout << " Pennies:" << setw(3) << penny << " (" << "$" << penny * 1 / 100.0 << ")" << "\n";
                                 }
        }
        // If change is = to 0, don't run the entire program, just run this portion.
        else  
        {       cout << "Change Due" << "$0.00" << "\n";
                cout << "Coin Dispenser will dispense:\n";
                cout << "No Coins\n";
        }
 
}
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Change Due:   $0.43
Coin Dispenser will dispense:
 Quarters: 1 ($0.25)
 Dimes:    1 ($0.10)
 Nickels:  1 ($0.05)
 Pennies:  3 ($0.03)