Change Calculator

Howdy folks,

Just started my second course in C++ at my local community college, second lab into the class and started having a bit of trouble. I want to thank anyone for reading this beforehand as I wish to be involved more in the community as my own skills evolve.

Problem statement below

Problem Statement

Modern grocery stores now often have a "U-Scan" checkout lane, allowing the customer to scan and check out their own groceries, without the need of a human checker. These lanes require that change be provided automatically, after the customer sticks their cash in a slot. You are to write a program that computes the bills and coins to be dispensed, minimizing the total number of bills and coins. (That is, for change totaling $5.50, you should not dispense 5 ones and 50 pennies, but a $5 bill and a 50-cent piece instead.)

The bills and coins available for you to dispense are as follows: $100 bill, $50 bill, $20 bill, $10 bill, $5 bill, $1 bill, 50-cent coin, 25-cent coin, 10-cent coin, 5-cent coin, 1-cent coin.

The console-based program, which can easily be converted to an internet CGI program (Links to an external site.) at some future date, prompts the user to input 2 numbers. The first number is the amount of the purchase, and the second one is the amount tendered by the customer. You may assume that the amount tendered is greater than or equal to the amount of purchase. The console output will be a series of lines showing the amount of change returned and detailing the number of bills and coins that will be dispensed as change, in descending order of monetary amount, one unit per line. If a bill/coin is not needed in the change returned, no output is produced for that bill/coin. (In other words, do not display "0 $1 bills".)

Plural logic. Proper use of plurals is required, as shown in the sample below. This will require some if-else logic to decide whether or not to append an "s" to the end of a denomination name.

Here the sample -- for a purchase of 42.15, the amount tendered is 50. There are no $'s or commas in the input -- just positive real numbers that may or may not contain a decimal.



NOTE: Instructor doesn't want the use of division nor the modulus control...

So far what i got is this

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
 #include <iostream>
using std::cin;
using std::cout;
using std::ios;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>

int main()

{
   //Program begins here
  double total;
  double tendered;
  double change;

  string buf;
  cout << "Please enter the PURCHASE TOTAL and AMOUNT TENDERED with a SPACE in between the two amounts (example 5.85 10.50)" << endl;
  cin >> buf; total = atof(buf.c_str());
  cin >> buf; tendered = atof(buf.c_str());

  change = tendered - total;

  cout << change ;
  
  


Any help is much appreciated!

Cheers!
Last edited on
You've already calculated how much change you'll need to dispense, so let's start from there.
change = tendered - total;
Think about how you would give this change piece-by-piece. In order to give the fewest bills and coins, you would hand over your biggest denomination that doesn't exceed the change you have left to give.
changeLeftToGive = change;

1
2
3
4
5
6
int numHundredBills = 0;
while(changeLeftToGive >= 100) //for $100 bill
{
    numHundredBills++; //this is like adding 1 more hundred dollar bill to the change you're giving
    changeLeftToGive -= 100; //if you've done that, it means you have $100 less to go to satisfy the change amount
}

Once you've given all the $100 bills in change that you can, move to the next largest denomination. In your case, that would be $50.
In order to achieve this result;
Enter Purchase, Tendered : 179.92 200

Change: 27.08

1 20
1 5
2 1's
1 0.05
3 0.01's


Done


You need something like this
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>
#include	<vector>

using namespace std;

	vector <double> Denom  { 100, 50, 20, 10, 5, 1, 0.5, 0.25, 0.1, 0.05, 0.01 };
	
int main (void) {
	double	Tendered, Purchase;
	
	cout << "Enter Purchase, Tendered : ";
	cin >> Purchase >> Tendered;	
	Tendered -= Purchase;	
	cout << "\n\n\tChange:  " << Tendered << "\n\n";
	
	for ( auto Value : Denom) {

	/*  YOUR CODE HERE */
	
	}
	
	cout << "\n\nDone" << endl;
	return 0;
}

For obvious reasons I've redacted the entire code, but think about successive subtraction and you should be able to come up with something @ /* YOUR CODE HERE */


Hello all,

I appreciate the input in this problem, with a little help from both of the posts I was able to finalize the code. Which I will be putting up shortly, if anyone needs help with this problem statement shoot me a message!

Again thanks for all the help, words cannot express the positive learning achieved from this forum!

Topic archived. No new replies allowed.