Hey everyone I'm new here and I need help with a homework assignment that allows the user to make change.

The assignment is as follows: You will write a program that will calculate the minimal amount of coins needed to make change for some amount of cents. The program will accept as input an integer amount in cents. If the amount is positive, it will calculate and display the minimal number of quarters, dimes, nickels, and pennies required to make that amount.


So far this is what I have and I'm completely lost on what to do next. I was thinking that I either need an if statement or a do while loop but i'm stumped.


Here is what I have so far.

#include <iostream>
using namespace std;
int main()
{
	const int QUARTER=25;
	const int DIME=10;
	const int NICKEL=5;
	const int PENNY=1;
	int coin=0;

cout << "This program will calculate the minimal number of coins needed\n:;
cout << "in order to make change for a given amount of cents.\n\n";

cout << "Please enter an amount in cents: \n\n";
cin >> coin;
while (coin > 0)
{

I was thinking that I either need an if statement or a do while loop but i'm stumped.

You're thinking in terms of code. Try jumping up a level and think in terms of steps. If you have a bag full of each kind of coin, how would you make change? Here's how I'd do it:

  1. Find the largest coin that's less than the total
  2. Grab one coin of the selected size out of its bag and put it into a "change" pile
  3. Subtract the amount of the coin from the total
  4. Repeat the process until the total is 0. The coins in the "change" pile are the end result

You can't write code until you have a clear idea of how to solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.