Calculating coin combinations using do-while loops.

Hello! I'm new at programming; I just started it last month. I have an assignment that involves using do-while loops for calculating combinations of coins. I'm kind of lost and I would really appreciate it if someone could help me on how to start doing this:

~~~~
E.1. Change calculator.
This lab will simply ask the user for a value under $5 and then calculate the number of toonies, loonies, quarters, dimes, nickel, and pennies required to make up this amount. Although there are many possible combinations of coins to obtain this amount, the idea is (like a cashier) to first start taking toonies, then loonies, then quarters, dime, nickels, and finally pennies. That is, for this lab we will attempt to find the minimum number of coins needed to obtain the amount the user inputs.

Examples: Here are two examples showing the numbers of each coin, given an amount under $5.00.
Example 1
- Assume value is $3.46
- Number of toonies needed without going over is 1, leaving $1.46 remainder.
- Number of loonies needed without going over is 1, leaving $0.46 remainder.
- Number of quarters needed without going over is 1, leaving $0.21 remainder.
- Number of dimes needed without going over is 2, leaving $0.01 remainder.
- Number of nickels needed without going over is 0, leaving $0.01 remainder.
- Number of pennies needed without going over is 1.
Thus you need 1 toony, 1 loony, 1 quarter, 2 dimes, and 1 penny for $3.46


Program Requirements:

- The program will be begin by displaying a header with your name, Lab #3, and the date.
- The program will ask the user to input a value under $5.00.
- If the amount is negative, or is over $5.00, then the program will display a message saying the input amount is not acceptable, and ask the user again to input another number. The program will continue to give error messages until a correct amount is entered.
- The program will then calculate the minimum number of coins required to obtain the value that the user entered.
- The program will ask the user if they wish to enter another amount. The program will continue to run as many times as the user requests, until the user specifies that they wish to end the program. HINT: Use a Do-While loop (although a While could also work) which checks the user input to determine if the loop should continue.
~~~

Thank you. :)
Last edited on
I started it with this:

#include <iostream>
using namespace std;
int main ()
{
int value (0), remainder (0), toonies (0), quarters (0), dimes (0), nickel (0), pennies (0);
do
{
cout << "Please enter a value under $5.00: ";
cin >> value;

//Calculations for the combinations here

while (value > 5.00 || value == 0)
{
cout << "This value is not acceptable. Please enter a different value: ";
cin >> value;
}
} while (value < 5.00 && value > 0);

}

It's the calculations for the combinations I need help with. Can someone please give me any hints? Thanks!
Funny enough. We are currently having a discussion about an algorithm designed to do just this.

http://www.cplusplus.com/forum/general/7692/
http://en.wikipedia.org/wiki/Greedy_algorithm
I noticed that, too. :)

Anyway, you don't even need to think about much, the problem states exactly what you have to do. It goes nearly step by step...read the problem carefully.
Thanks, guys! :)

So, I tried doing that greedy algorithm method, but I don't really know how to write the code for it. I wrote the one below and it compiles, but when I run it and I input a number, it just keeps going on and on. Can you tell me what I'm doing wrong? Also, can you suggest some modifications to tidy it up a bit? Thank you. :)

#include <iostream>
using namespace std;
int main ()
{
double value (0), remainder (0), toonies (0), loonies (0), quarters (0), dimes (0), nickels (0), pennies (0);
do
{
cout << "Please enter a value under $5.00: ";
cin >> value;

value - 0 == remainder;

do
{
toonies = toonies++;
cout << toonies << " toonies" << endl;
} while (remainder - 2 != 0);
do
{
loonies = loonies++;
cout << loonies << " loonies" << endl;
} while (remainder - 1 != 0);
do
{
quarters = quarters++;
cout << quarters << " quarters" << endl;
} while (remainder - 0.25 != 0);
do
{
dimes = dimes++;
cout << dimes << " dimes" << endl;
} while (remainder - 0.10 != 0);
do
{
nickels = nickels++;
cout << nickels << " nickels" << endl;
} while (remainder - 0.05 != 0);
do
{
pennies = pennies++;
cout << pennies << " pennies" << endl;
} while (remainder - 0.01 != 0);


while (value > 5.00 || value == 0)
{
cout << "This value is not acceptable. Please enter a different value: ";
cin >> value;
}
} while (value < 5.00 && value > 0);

}
Ten lines down from the start of the code: value - 0 == remainder;
That is invalid I believe. If you are trying to assign value - 0 to remainder you would have to code it using the assignment operator ( single '=' sign ) like this:
remainder = value - 0;

All of those do-while loops are unnecessary.
Indent all of your code consistently
Parenthesize! (i.e. while( (value > 5.0) || (value == 0) ) )
Use code tags when posting code so line numbers appear (It's easier to refer to certain code)

Last edited on
Oh! Okay, thank you. I figured it out. It works now! Yay!

Thanks, again! :)
I'll be interested to see what you've written?
Sorry! I haven't been here in a while. Anyway, here is the code:

#include <iostream>
using namespace std;
int main ()
{
// Declared variables
double value (0), toonies (0), loonies (0), quarters (0), dimes (0), nickels (0), pennies (0);
char again = 'y';

cout << "Change Calculator" << endl;

// Start of the loop
while (again == 'y')
{
cout << "Please enter a value under $5.00: ";
cin >> value;
// Where the user will be asked for a different value if the initial value is unacceptable.
while ( (value < 0) || (value >= 5.00) )
{
cout << "This value is not acceptable. Please enter a different value: ";
cin >> value;
}

cout << "$" << value << " is broken down as:" << endl;

// Calculations for the combinations of coins

while (value >= 2)
{
value -= 2;
toonies++;
}
while (value >= 1)
{
value -= 1;
loonies++;
}
while (value >= 0.25)
{
value -= 0.25;
quarters++;
}
while (value >= 0.10)
{
value -= 0.10;
dimes++;
}
while (value >= 0.05)
{
value -= 0.05;
nickels++;
}
while (value >= 0.01)
{
value -= 0.01;
pennies++;
}
// End of calculations

// Results that will be seen on the screen
if (toonies != 0) cout << toonies << " toonie(s)" << endl;
if (loonies != 0) cout << loonies << " loonie(s)" << endl;
if (quarters != 0) cout << quarters << " quarter(s)" << endl;
if (dimes != 0) cout << dimes << " dime(s)" << endl;
if (nickels != 0) cout << nickels << " nickel(s)" << endl;
if (pennies != 0) cout << pennies << " pennie(s)" << endl;

cout << "Would you like to enter another value?(y/n) ";
cin >> again;
// Resetting variables
toonies = 0;
loonies = 0;
quarters = 0;
dimes = 0;
nickels = 0;
pennies = 0;
} // End of loop

cout << "Goodbye!" << endl;

return 0;

}

I don't know how to put code tags, sorry. :P
For future reference place [code] [ /code] around the text. Without the space before the slash ("/") or click the # sign to the right of the reply box.
Topic archived. No new replies allowed.