View Single Post
Old 09-26-2007, 06:55 AM   #6 (permalink)
natelowery
Recruit
 
Join Date: Sep 2007
Posts: 2
natelowery is on a distinguished road
this should help

I just had to do this problem in my Intro to C++ class and here's how I wrote it. You can see from the test output at the bottom that it works correctly.

//Ch4AppE10.cpp - calculates and displays the amount of change due a customer
//Created by Nathanael Lowery on September 24, 2007

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
//declare variables
double amountDue = 0.0;
double amountPaid = 0.0;
double change = 0.0;
double remainder = 0.0;
double dollars = 0.0;
double quarters = 0.0;
double dimes = 0.0;
double nickels = 0.0;
double pennies = 0.0;

//get input items from user
cout << "Enter the amount due: ";
cin >> amountDue;
cout << "Enter the amount paid: ";
cin >> amountPaid;

//calculate change due
change = amountPaid - amountDue;
dollars = int(change);
remainder = change - dollars;
quarters = int(remainder / .25);
remainder = remainder - quarters * .25;
dimes = int(remainder / .1);
remainder = remainder - dimes * .1;
nickels = int(remainder / .05);
remainder = remainder - nickels * .05;
pennies = remainder / .01;

//display output items
cout << "The change due is $" << change << "." << endl;
cout << "That's " << dollars << " dollars, " << quarters << " quarters, " << dimes << " dimes, " << nickels
<< " nickels, and " << pennies << " pennies." << endl;

return 0;
} //end of main function

//Test Output 1
/*Enter the amount due: 75.34
Enter the amount paid: 80.00
The change due is $4.66.
That's 4 dollars, 2 quarters, 1 dimes, 1 nickels, and 1 pennies.
Press any key to continue*/

//Test Output 2
/*Enter the amount due: 39.67
Enter the amount paid: 50.00
The change due is $10.33.
That's 10 dollars, 1 quarters, 0 dimes, 1 nickels, and 3 pennies.
Press any key to continue*/

//Test Output 3
/*Enter the amount due: 45.55
Enter the amount paid: 45.55
The change due is $0.
That's 0 dollars, 0 quarters, 0 dimes, 0 nickels, and 0 pennies.
Press any key to continue*/
natelowery is offline   Reply With Quote