Final answer:
The improved C++ program efficiently calculates and outputs the number of quarters, dimes, and pennies as change for an input amount between 1 and 99 cents. It also validates user input within the specified range.
Step-by-step explanation:
The student is seeking assistance with a C++ program that calculates the change in coins for any given amount from 1 to 99 cents. The program uses a provided function, computeCoin, to determine the number and type of coins needed. The starting code has several logical issues and syntax errors, which need to be corrected by ensuring proper looping for user input validation and the correct usage of the computeCoin function. Here's an improved version of the code:
#include
using namespace std;
void computeCoin(int coinValue, int& number, int& amountLeft) {
number = amountLeft / coinValue;
amountLeft -= number * coinValue;
}
int main() {
int amount, quarters, dimes, pennies;
do {
cout << "Enter the amount desired (1 to 99 cents): ";
cin >> amount;
if(amount < 1 || amount > 99) {
cout << "Error, please enter a value between 1 and 99 cents.\\";
}
} while(amount < 1 || amount > 99);
computeCoin(25, quarters, amount);
computeCoin(10, dimes, amount);
computeCoin(1, pennies, amount);
cout << "Your change is: " << quarters << " quarter(s), "
<< dimes << " dime(s), and " << pennies << " penny (pennies)";
return 0;
}
This revised program first validates the input, then uses the computeCoin function to calculate the number of quarters, dimes, and pennies that make up the entered amount. The output clearly states how many of each coin type is provided as change.