 I am trying to edit my current homework assignment to include a loop that lets the user repeat the computation for new values until the user says they want to end the program. I was given a hint to use integer division and the % operator to implement this function. I have created the program from scratch and researched my book for the last 3 hours (plus online), but cannot figure out how to use integer division and modulus as a loop control. Can someone help explain it to me? Below is the code I have written (it compiles and runs with no errors), and it exits properly if the integer entered is more than 100 or less than zero. Any guidance would be a great help, or a link to C++ documentation describing how to use int division and modulus as a loop control.

Thank you,
Griff0527

``````#include <iostream>
using namespace std;

void computeCoin(int coinValue, int& number, int& amountLeft);
//Precondition: 0 < coinValue < 100; 0 <= amountLeft < 100.
//Postcondition: number has been set equal to the maximum number
//of coins of demonination coinValue cents that can be obtained
//from amountLeft cents.  amountLeft has been decreased by the
//value of the coings, that is, decreased by number*coinValue.

void quartersOut(int number);
void dimesOut(int number);
void penniesOut(int number);

int main( )
{
int number = 0, // counter for the coins
amountLeft; // amount remainig to be disbursed

cout << "Enter the amount of change between 0 and 100\n to determing the proper coinage to be disbursed: ";
cin >> amountLeft; // input amount to be dispensed

if ((amountLeft < 100) && (amountLeft > 0))
{

cout << amountLeft << " cents can be given as:" << endl << endl; // prepatory output based on input

computeCoin(25, number, amountLeft); // call the function to calculate quarters
quartersOut(number);

number = 0; // reset counter to zero
computeCoin(10, number, amountLeft); // call the function to calculate dimes
dimesOut(number);

number = 0; // reset counter to zero
computeCoin(1, number, amountLeft); // call the function to calculate pennies
penniesOut(number);
}

else
{
cout << "I'm sorry, you have entered a number outside the scope of this program" << endl;
}

return(0);
}

void computeCoin(int coinValue, int& number, int& amountLeft)
{
while (amountLeft >= coinValue)
{
amountLeft = amountLeft - coinValue;
number++;
//cout << "amountLeft = " << amountLeft << endl;
//cout << "number = " << number << endl;
//cout << "coinValue = " << coinValue << endl;

}
}

void quartersOut(int number)
{
cout << number << " quarter(s) "; // output for quarters
}

void dimesOut(int number)
{
cout << number << " dime(s) and "; // output for dimes
}

void penniesOut(int number)
{
cout << number << "penny(pennies)" << endl << endl; // output for pennies
}``````

## All 8 Replies

well if you want the user to be able to keep inputting a number for the change to be computed the you can wrap the whole thing in a while loop

``````char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
// lines 17-44 here
cout << "Enter 'y' to continue 'n' to stop: ";
cin >> ch;
}`````` Right,
I know how to use a character to control the loop, but for the sake of the assignment, I am trying to understand how to use integer division and modulus to do the same thing as using a character to control the loop. I am going off the suggested "Hint" that our instructor gave us. Thank you though for the tip.
If anyone can assist on the integer division, that would be great. I am tempted to use a char as the control in order to be only one day late instead of two.

well if you want the user to be able to keep inputting a number for the change to be computed the you can wrap the whole thing in a while loop

``````char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
// lines 17-44 here
cout << "Enter 'y' to continue 'n' to stop: ";
cin >> ch;
}``````

Was the hint for how to calculate the change or for how to let the user keep entering in more numbers? The exact hint said "Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. (Hint: Use integer division and the % operator to implement this function.)"

So, I suppose it could be read either way... I, of course, choose to take it the most complex way so that I can learn more. Is loop control via integer division possible without a pre-set limit? ie. for (i < 10000, i/10, i++) or some other nonsense where you would much rather use simple counter addition to control the loop? Perhaps I am over-thinking the issue?

I think the hint might be for the the change function and not for the user input loop. the change function you wrote could be rewritten like this

``````void computeCoin(int coinValue, int& number, int& amountLeft)
{
if (amountLeft % coinValue == 0)
{
number += amountLeft / coinValue;
amountLeft = 0;
}
else
{
number += amountLeft / coinValue;
amountLeft = amountLeft % coinValue;
}
}`````` That makes sense. Thanks for the help Nathan. I appreciate you straightening me out a bit.

NathanOliver, don't you think that condition is unnecessary?

Yes it is unnecessary. Didn't really think about it I just wrote what first came to mind. Thanks for pointing that out invisal. It could easily be written this way and it is probably faster.

``````void computeCoin(int coinValue, int& number, int& amountLeft)
{
number += amountLeft / coinValue;
amountLeft = amountLeft % coinValue;

}``````
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.