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
}