I have this homework assignment that i need help with and below is the code that i have so far. any help would be appreciated. thank you.


Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents.
For example, if the amount is 86 cents, the output would be something like the following:

86 cents can be given as 3 quarter(s) 1 dime(s) amd 1 penny(pennies).
Use coin denominations of 25 cents (quarters),
10 cents (dimes) and 1 cent (pennies).
Do not use nickel and half-dollar coins.
Your program will use the following function (amoung others): void computeCoin(int coinValue, int& number, int& amountLeft); /* Preconditions:
0 < coinValue < 100;
0 <= amountLeft < 100.
Postconditions: number has been set equal to the maximum number of coins of dem=nomination conValue cents that can be obtained from amountLeft cents.
AmountLeft has been decreased by the value of the coins, that is, decreased by number*coinValue.

For example, suppose the value of the variable amountLeft is 86. Then, after the following call, the value of number will be 3 and the value of amountLeft will be 11 (because if you take three quarters from 86 cents, that leaves 11 cents):computeCoins(25, number, amountLeft);

Include a loop that lets the user repeat this computation for new input values untill the user says he or she wants to end the program.
(Hint: Use integer division and the % operator to implement this function.) p>

here is what i have so far

#include <iostream> 
using namespace std; 
void computeCoin(int coinValue, int& number, int& amountLeft); 

int main() 
{ 
static int inChange [] = { 25, 10, 1 }; 
static char *szChange [] = { "Quarters", "Dimes", "Pennies" } 
int inNumber, inLeft; 

cout << "Enter amount of money (1-99)--->: "; 
cin >> inLeft; 
cout << change << "coins can be given" << endl; 
cout << "-------------------------------------" << endl; 

for ( int i=0, i<3; i++ ) { 
    computeCoin ( inChange, inNumber, inLeft ) 
    if ( inNumber ) cout << inNumber << szChange << endl; 
} 
return 0; 

} //End main 

void computeCoin(int coinValue, int& number, int& amountLeft) { 
number = amountLeft / coinValue; 
if ( number ) amountLeft = amountLeft % coinValue; 
}

Dani AI

Generated

The code fails for two kinds of reasons: simple syntax/typo errors and a wrong function call. The compiler error C2664 comes from passing the whole array to computeCoin instead of a single denomination, and a few lines in the posted snippet use commas where semicolons belong or print the wrong variable (printing an array name prints its address). Build on 's division/modulus idea and 's parameter hint: call the function with an element of the inChange array, and preserve the original amount if you want to display it (because computeCoin reduces amountLeft).

Correct the loop and printing like this:

int original = inLeft;                 // save input before it is consumed
cout << original << " cents can be given as:" << endl;

for (int i = 0; i < 3; ++i) {
    computeCoin(inChange[i], inNumber, inLeft);   // pass one coin value, not the whole array
    if (inNumber) cout << inNumber << " " << szChange[i] << endl;
}

Notes and quick checks:

  • Ensure static int inChange[] = {25, 10, 1}; and static const char* szChange[] = {"Quarters","Dimes","Pennies"}; are terminated with semicolons and indexed with [i] when used.
  • computeCoin should use integer division to set number and then reduce amountLeft by number * coinValue (this is exactly the behavior the assignment expects).
  • Add input validation (1..99) and wrap the input+computation in a loop that asks whether to repeat (do/while or while with a sentinel).
  • 's compact arithmetic approach shows the same math but doing it via the provided computeCoin function keeps the code modular and matches the assignment.

Fixing the indexing, the loop punctuation, and the printed variable will resolve the compile error and produce the expected coin breakdown.

Recommended Answers

All 6 Replies

>>Do not use nickel and half-dollar coins.

half-dollars I can understand because they are not very common. But nickels??? :icon_eek: As a customer I would not like it if the cashier gave me seven pennies when he/she had a nichel in the drawer.

>>Do not use nickel and half-dollar coins.

half-dollars I can understand because they are not very common. But nickels??? :icon_eek: As a customer I would not like it if the cashier gave me seven pennies when he/she had a nichel in the drawer.

this is what the problem says. can you help me on the code please.

I'll start you off by showing you how to compute the number of quarters

Quarters = amountleft / 25;
amountleft %= 25;

Now just do the same thing to get dimes and pennies.

I'll start you off by showing you how to compute the number of quarters

Quarters = amountleft / 25;
amountleft %= 25;

Now just do the same thing to get dimes and pennies.

i am confused. the program above had some errors so i fixed them and now i am getting an Error 1 error C2664: 'computeCoin' : cannot convert parameter 1 from 'int [3]' to 'int' 17

#include <iostream> 
using namespace std; 
void computeCoin(int coinValue, int& number, int& amountLeft); 
 
int main() 
{ 
static int inChange [] = { 25, 10, 1 }; 
static char *szChange [] = { "Quarters", "Dimes", "Pennies" }; 
int inNumber, inLeft; 
 
cout << "Enter amount of money (1-99)--->: "; 
cin >> inLeft; 
cout << inChange << "coins can be given" << endl; 
cout << "-------------------------------------" << endl; 
 
for ( int i=0; i<3; i++ ) { 
    computeCoin ( inChange, inNumber, inLeft ); 
    if ( inNumber ) cout << inNumber << szChange << endl; 
} 
return 0; 
 
} //End main 
 
void computeCoin(int coinValue, int& number, int& amountLeft) { 
number = amountLeft / coinValue; 
if ( number ) amountLeft = amountLeft % coinValue; 
}

Look at your parameters. What does the function require? What are the actual definitions of the variables?

Well, I honestly never did anything like this before, but I thought about it for a bit, and came up with a fairly simple formula for figuring out the change. I am not sure if this is what you are looking for, as your coding is a bit more advanced than what I know..but maybe this will help you:

cout << "Enter the amount of change you want: ";
	cin >> total;
	quarters = total/25;
	dimes = (total-(quarters*25))/10;
	pennies = total-(quarters*25+dimes*10);
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.