C++ Change Dispenser (My First C++ Program)

by bryan on October 7, 2009

One of these days I’ll actually know how to program c++ pretty well, and when that happens, I plan to write some kickass software. But when that happens, I figured it would be pretty amusing to look back at my very first c++ program (besides the mandatory “Hello World!” app).

This was our first assignment in Computer Science 210, page 98, #15 in Problem Solving, Abstraction & Design Using C++ (5th Edition). To quote:

Write a program that dispenses change. The program should read the amount of the purchase and the amount paid and then display the number of dollars, quarters, dimes, nickels, and pennies given in change.

Actually if you keep reading, they tell you the wrong way to do it, as a “hint”. Jerks.

But notwithstanding, this is what I wrote:

// Bryan Simonson
// 10/06/09
// CS210 - Assignment #1
//
// NOTE:
// The book is trying to mislead us into using the % operator for this assignment.
// This shall not be forgotten.
//
#include <iostream>
using namespace std;
int main() {
	int dollars,quarters,dimes,nickels,pennies;
	double due,payment,change;
	// Get the amount due
	cout << "Please enter the price:\n";
	cin >> due;
	// Check the amount due for problems
	while (due <= 0.01) {
		// Somebody entered zero or less, probably trying to get some free money
		cout << "Sorry cheapskate, you must spend at least $0.01\n\n";
		// Ask again for the real price
		cout << "Please enter the ACTUAL price:\n";
		cin >> due;
		cout << endl;
	}
	// Get the payment amount
	cout << "Please provide payment amount (using numbers only):\n";
	cin >> payment;
	// Check payment amount for problems
	while (payment < due) {
		// Somebody is trying to pay less than what's due
		// but we're not accepting ANY money unless we get it all at once
		cout << "No, you owe $" << due << ", not $" << payment << ".\n\n";
		cout << endl << "Please provide FULL payment:\n";
		cin >> payment;
	}
	if (payment > due){
		// They paid more than what's due, time to give out change
		change = payment - due;
		cout << "----------------------------------------------\n";
		cout << "Your change is $" << change << endl;
		// dollars
		for (dollars = 0; change >= 1.00; dollars++){
			change -= 1.00;
		}
		// quarters
		for (quarters = 0; change >= 0.25; quarters++){
			change -= 0.25;
		}
		// dimes
		for (dimes = 0; change >= 0.10; dimes++){
			change -= 0.10;
		}
		// nickels
		for (nickels = 0; change >= 0.05; nickels++){
			change -= 0.05;
		}
		// pennies, just for good measure
		for (pennies = 0; change > 0.00; pennies++){
			change -= 0.01;
		}
		cout << "You get " << dollars << " dollars, " << quarters << " quarters, " << dimes << " dimes, " << nickels << " nickels, and " << pennies << " pennies.\n\n";
	} else {
		// They must have given exact change
		cout << "Thanks for using exact change!\n";
	}
	return 0;
}

I promise, there was lots more whitespace in the actual program. WordPress is just being stupid. Here’s the file for download: ChangeDispenser.cpp.

Warning: Don’t cheat off me.

UPDATE 10-19-09: Fixed a bug in how pennies were calculated.
UPDATE 11-01-09: Fixed formatting / lack of whitespace.

Leave a Comment

Connect with Facebook

Previous post:

Next post: