# coding problem and need help

this is the question:

A third grade teacher at Hinsbrook Elementary School would like you to create a program that will help her students learn how to make change. The program should allow the student to enter the amount the customer owes and the amount of money the customer paid. The program should calculate and display the amount of change, as well as how many dollars, quarters, dimes, nickels, and pennnies to return to the customer. For now, you don't have to worrie about the situation where the price is greater than what the customer pays. You can always assume that the customer paid either the exact amount or more than the exact amount.

I need to code the program and this is what I have so far and from my conclusions my calculations or something is off for I get negative numbers as the change. Here is the code I did:

#include
//#include "stdafx.h"
using std:: cout;
using std:: cin;
using std:: endl;

int main()
{

//declare variables
double totalamount = 0.0;
double amountdue = 0.0;
double amountpaid= 0.0;
int DOLLARS = 0.0;
int QUARTERS = 0.0;
int DIMES = 0.0;
int NICKELS = 0.0;
int PENNIES = 0.0;

//enter input items
cout << "Enter amount due: ";
cin >> amountdue;
cout << "Enter amount paid: ";
cin >> amountpaid;

//calculate change
changeamount = amountdue - amountpaid;
DOLLARS = changeamount / 1 ;
QUARTERS = (dollars * 1) /.25 ;
DIMES = (changeamount - (dollars * 1) - (quarters * .25) / .1 );
NICKELS = (changeamount - (dollars * 1) - (quarters* .25) - (dimes* .1) / .05) ;
PENNIES = (changeamount - (dollars * 1) - (quarters * .25) - (dimes * .1) - (nickels * .05)/.01) ;

//display amount due
cout << "dollars: " << Dollars << endl;
cout << "quarters: " << Quarters << endl;
cout << "dimes: " << Dimes << endl;
cout << "nickels: " << Nickels << endl;
cout << "pennies: " <<Pennies<< endl;

return 0;
//end of main function

}

can some one help me correct this coding. Thanks

• : this is the question:
:
: A third grade teacher at Hinsbrook Elementary School would like you to create a program that will help her students learn how to make change. The program should allow the student to enter the amount the customer owes and the amount of money the customer paid. The program should calculate and display the amount of change, as well as how many dollars, quarters, dimes, nickels, and pennnies to return to the customer. For now, you don't have to worrie about the situation where the price is greater than what the customer pays. You can always assume that the customer paid either the exact amount or more than the exact amount.
:
:
: I need to code the program and this is what I have so far and from my conclusions my calculations or something is off for I get negative numbers as the change. Here is the code I did:
:
[code]
: #include
: //#include "stdafx.h"
: using std:: cout;
: using std:: cin;
: using std:: endl;
:
: int main()
: {
:
: //declare variables
: double totalamount = 0.0;
: double amountdue = 0.0;
: double amountpaid= 0.0;
: int DOLLARS = 0.0;
: int QUARTERS = 0.0;
: int DIMES = 0.0;
: int NICKELS = 0.0;
: int PENNIES = 0.0;
:
: //enter input items
: cout << "Enter amount due: ";
: cin >> amountdue;
: cout << "Enter amount paid: ";
: cin >> amountpaid;
:
:
: //calculate change
: changeamount = amountdue - amountpaid;
: DOLLARS = changeamount / 1 ;
: QUARTERS = (dollars * 1) /.25 ;
: DIMES = (changeamount - (dollars * 1) - (quarters * .25) / .1 );
: NICKELS = (changeamount - (dollars * 1) - (quarters* .25) - (dimes* .1) / .05) ;
: PENNIES = (changeamount - (dollars * 1) - (quarters * .25) - (dimes * .1) - (nickels * .05)/.01) ;
:
:
: //display amount due
: cout << "dollars: " << Dollars << endl;
: cout << "quarters: " << Quarters << endl;
: cout << "dimes: " << Dimes << endl;
: cout << "nickels: " << Nickels << endl;
: cout << "pennies: " <<Pennies<< endl;
:
: return 0;
: //end of main function
:
: }
[/code]
:
:
: can some one help me correct this coding. Thanks

First off C is case sensitive, so decide how you want to spell your variable names...

Besides that, your math is rather off. For the quarters you probably forgot to type a part of the function, and for the other parts: Look up 'operator precedence', you should know from basic math that 1 + 2 * 3 is 7 and not 9 (=(1+2)*3), C follows these rules. (And for my personal taste: division by and multipication with 1 are nop's, so I think they look weird, but if you think it makes your program look clearer, who am I to complain?;).

I do advice you to fill in the numbers for a simple example, there's a very simple (but very important) mistake in your program, do this very, very litteraly, because, otherwise, you might miss it. (If you haven't encountered the mistake by the line that starts with 'DOLLARS = ...', start over... Somethimes you feel so stupid... Don't worry, I know exactly how you feel;)

Some further points that are not really that important (Especially when you're in a beginners class), but you might want to think about:
- You initalize your integer variables with a value of 0.0, which is a floating point value (it has a decimal point, right?)
- You assign a double-value to a integer variable in your calculation, which truncates the value (in this case exactly what you want, good idea!), which is also a very common cause for bugs in a program, if you use a cast ("intValue = (int)doubleValue;") you can show to yourself that you know and want this value to be truncated, which helps debugging when you write more complicated programs in the future.

This might look like a lot of comments, but you're doing pretty good.
Good luck and happy coding!

Pruyque
:wq
• #include
using std:: cout;
using std:: cin;
using std:: endl;

int main()
{

//declare variables
double amountdue = 0.0;
double amountpaid= 0.0;
double DOLLARS = 0.0;
double QUARTERS = 0.0;
double DIMES = 0.0;
double NICKELS = 0.0;
double PENNIES = 0.0;

//enter input items
cout << "Enter amount due: ";
cin >> amountdue;
cout << "Enter amount paid: ";
cin >> amountpaid;

//calculate total amount
Dollars =
QUARTERS = 1 /.25 ;
DIMES = 1 - .25 / .1 ;
NICKELS = 1 - .25 - .1 / .05 ;
PENNIES = 1 - .25 - .1 - .05/.01 ;

//display change amount
cout <<"Total amount due: " <<amountdue << endl;
cout <<"Total amount paid: " <<amountpaid << endl;
return 0;
//end of main function

}

this is what I have now I know the math is still off but math isn't my best subject can you further help me on this coding so I can turn it in to my professor.
• : #include
: using std:: cout;
: using std:: cin;
: using std:: endl;
:
: int main()
: {
:
: //declare variables
: double amountdue = 0.0;
: double amountpaid= 0.0;
: double DOLLARS = 0.0;
: double QUARTERS = 0.0;
: double DIMES = 0.0;
: double NICKELS = 0.0;
: double PENNIES = 0.0;
:
: //enter input items
: cout << "Enter amount due: ";
: cin >> amountdue;
: cout << "Enter amount paid: ";
: cin >> amountpaid;
:
:
: //calculate total amount
: Dollars =
: QUARTERS = 1 /.25 ;
: DIMES = 1 - .25 / .1 ;
: NICKELS = 1 - .25 - .1 / .05 ;
: PENNIES = 1 - .25 - .1 - .05/.01 ;
:
:
: //display change amount
: cout <<"Total amount due: " <<amountdue << endl;
: cout <<"Total amount paid: " <<amountpaid << endl;
: return 0;
: //end of main function
:
: }
:
:
: this is what I have now I know the math is still off but math isn't my best subject can you further help me on this coding so I can turn it in to my professor.
:
[blue]
Use code tags.

For the math, I would multply the changeamount by 100, and reassign
it to a int, so we can do the operations on pennies instead of dollars, basically whole numbers.

This isn't code that would fit in...use it as a guide
[/blue]
[code]
int change=(int)changeamount*100;
dollars=change/100;
change = change % 100;[blue]/*you should do this for each, you
take out the amount you've already determined and leave the remainder.
Think about the subtraction as if already you given the customer that
part of the change.(Here, the dollars) Its already given(so its subtracted), and whats left must be broken down by lesser coins...
*/
[/blue]
...
[/code]
[blue]
I hope this helps.
{2}rIng[/blue]
• I'm new to programming and this is for a project that is due tomorrow can you help me out more in getting this coding to work so that this program can function correctly. Thanks.
• : I'm new to programming and this is for a project that is due tomorrow can you help me out more in getting this coding to work so that this program can function correctly. Thanks.
:

What problem do you still have? How much have you gotten done? You can't expect anyone here to do your assignment for you, you know.

{2}rIng
• I understand all that but I never done coding before in my entire life and this C++ stuff is very new to me and I'm having a hard time understanding it. The professor himself isn't helping me as much as he should be and that is why I'm asking you all for help.

With the assignment I'm having coding problems with the calculations for the numbers come out negative digits. If you can fully complete this one code for me I will never ask you for any more help to my assignments.

This is the coding I have so far:

#include
using std:: cout;
using std:: cin;
using std:: endl;

int main()
{

//declare constant variables
double amountdue = 0.0;
double amountpaid= 0.0;
double DOLLARS = 0.0;
double QUARTERS = 0.0;
double DIMES = 0.0;
double NICKELS = 0.0;
double PENNIES = 0.0;

//enter input items
cout << "Enter amount due: ";
cin >> amountdue;
cout << "Enter amount paid: ";
cin >> amountpaid;

//calculate change amount
DOLLARS = (amountdue / 1);
QUARTERS = (amountdue - (DOLLARS * 1) / .25);
DIMES = (amountdue - (DOLLARS * 1) - (QUARTERS * .25) / .1);
NICKELS = (amountdue - (DOLLARS * 1) - (QUARTERS * .25) - (DIMES * .1) / .05);
PENNIES = (amountdue - (DOLLARS * 1) - (QUARTERS * .25) - (DIMES * .1) - (NICKELS * .05) / .01);

//display change amount
cout <<"Total amount due: " <<amountdue << endl;
cout <<"Total amount paid: " <<amountpaid << endl;
return 0;
//end of main function
}

But somewhere in the coding is wrong can you help me?

• [b][red]This message was edited by Gregry2 at 2007-2-10 1:9:22[/red][/b][hr]
: I understand all that but I never done coding before in my entire life and this C++ stuff is very new to me and I'm having a hard time understanding it. The professor himself isn't helping me as much as he should be and that is why I'm asking you all for help.
:
:
: With the assignment I'm having coding problems with the calculations for the numbers come out negative digits. If you can fully complete this one code for me I will never ask you for any more help to my assignments.
:
:
: This is the coding I have so far:
:
: #include
: using std:: cout;
: using std:: cin;
: using std:: endl;
:
: int main()
: {
:
: //declare constant variables
: double amountdue = 0.0;
: double amountpaid= 0.0;
: double DOLLARS = 0.0;
: double QUARTERS = 0.0;
: double DIMES = 0.0;
: double NICKELS = 0.0;
: double PENNIES = 0.0;
:
: //enter input items
: cout << "Enter amount due: ";
: cin >> amountdue;
: cout << "Enter amount paid: ";
: cin >> amountpaid;
:
:
: //calculate change amount
: DOLLARS = (amountdue / 1);
: QUARTERS = (amountdue - (DOLLARS * 1) / .25);
: DIMES = (amountdue - (DOLLARS * 1) - (QUARTERS * .25) / .1);
: NICKELS = (amountdue - (DOLLARS * 1) - (QUARTERS * .25) - (DIMES * .1) / .05);
: PENNIES = (amountdue - (DOLLARS * 1) - (QUARTERS * .25) - (DIMES * .1) - (NICKELS * .05) / .01);
:
:
: //display change amount
: cout <<"Total amount due: " <<amountdue << endl;
: cout <<"Total amount paid: " <<amountpaid << endl;
: return 0;
: //end of main function
: }
:
: But somewhere in the coding is wrong can you help me?
:
:

I guess I confused you a bit, or, idk. You are performing calculations with the amount due, and you don't even output/display the dollars, the quarters, whatever...

You need to perform calculations on the change, which is amountDue - amountPaid, just as you did before...

First of all, any number divided by 1 is that number, just as any number times one is that number. Thus, trying to get the dollars by dividing the change by 1 isn't going to help you. This is a C++ program you're making, right? Well, try to

#include math.h

and then call floor() on the change, like

DOLLARS = floor(changeAmount);

It will return the number of dollars, for sure.

Now, after that, you should minus the dollars from the change, AND ASSIGN THAT TO CHANGE, thus, for the next coins, what part of the change is already in dollars isn't accidentally used. Understand?

Then, work on as usual, divide by .25, then .10, then .05, but be sure to minus the each coin's number from the change after you determine that coin's number.

And don't forget to output the coin's numbers. It would be sad if you do all that, but then the output's not even there...

I have to leave somewhere in 45 min. I keep checking to see if you need anymore help.

{2}rIng

• : : this is the question:
: :
: : A third grade teacher at Hinsbrook Elementary School would like you to create a program that will help her students learn how to make change. The program should allow the student to enter the amount the customer owes and the amount of money the customer paid. The program should calculate and display the amount of change, as well as how many dollars, quarters, dimes, nickels, and pennnies to return to the customer. For now, you don't have to worrie about the situation where the price is greater than what the customer pays. You can always assume that the customer paid either the exact amount or more than the exact amount.
: :
: :
: : I need to code the program and this is what I have so far and from my conclusions my calculations or something is off for I get negative numbers as the change. Here is the code I did:
: :
: [code]:
: : #include
: : //#include "stdafx.h"
: : using std:: cout;
: : using std:: cin;
: : using std:: endl;
: :
: : int main()
: : {
: :
: : //declare variables
: : double totalamount = 0.0;
: : double amountdue = 0.0;
: : double amountpaid= 0.0;
: : int DOLLARS = 0.0;
: : int QUARTERS = 0.0;
: : int DIMES = 0.0;
: : int NICKELS = 0.0;
: : int PENNIES = 0.0;
: :
: : //enter input items
: : cout << "Enter amount due: ";
: : cin >> amountdue;
: : cout << "Enter amount paid: ";
: : cin >> amountpaid;
: :
: :
: : //calculate change
: : changeamount = amountdue - amountpaid;
: : DOLLARS = changeamount / 1 ;
: : QUARTERS = (dollars * 1) /.25 ;
: : DIMES = (changeamount - (dollars * 1) - (quarters * .25) / .1 );
: : NICKELS = (changeamount - (dollars * 1) - (quarters* .25) - (dimes* .1) / .05) ;
: : PENNIES = (changeamount - (dollars * 1) - (quarters * .25) - (dimes * .1) - (nickels * .05)/.01) ;
: :
: :
: : //display amount due
: : cout << "dollars: " << Dollars << endl;
: : cout << "quarters: " << Quarters << endl;
: : cout << "dimes: " << Dimes << endl;
: : cout << "nickels: " << Nickels << endl;
: : cout << "pennies: " <<Pennies<< endl;
: :
: : return 0;
: : //end of main function
: :
: : }
: [/code]:
: :
: :
: : can some one help me correct this coding. Thanks
:
: First off C is case sensitive, so decide how you want to spell your
: variable names...
:
: Besides that, your math is rather off. For the quarters you probably
: forgot to type a part of the function, and for the other parts: Look
: up 'operator precedence', you should know from basic math that 1 + 2
: * 3 is 7 and not 9 (=(1+2)*3), C follows these rules. (And for my
: personal taste: division by and multipication with 1 are nop's, so I
: think they look weird, but if you think it makes your program look
: clearer, who am I to complain?;).
:
: I do advice you to fill in the numbers for a simple example, there's
: a very simple (but very important) mistake in your program, do this
: very, very litteraly, because, otherwise, you might miss it. (If you
: haven't encountered the mistake by the line that starts with
: 'DOLLARS = ...', start over... Somethimes you feel so stupid...
: Don't worry, I know exactly how you feel;)
:
: Some further points that are not really that important (Especially
: when you're in a beginners class), but you might want to think about:
: - You initalize your integer variables with a value of 0.0, which is
: a floating point value (it has a decimal point, right?)
: - You assign a double-value to a integer variable in your
: calculation, which truncates the value (in this case exactly what
: you want, good idea!), which is also a very common cause for bugs in
: a program, if you use a cast ("intValue = (int)doubleValue;") you
: can show to yourself that you know and want this value to be
: truncated, which helps debugging when you write more complicated
: programs in the future.
:
: This might look like a lot of comments, but you're doing pretty good.
: Good luck and happy coding!
:
: Pruyque
: :wq
:
I am on the same problem, but I have to use if else statements. How can I use that?

• [color=Blue]This is an algorithm to do it (much less calculations needed):

1. The types of change should be inside the array and they must be sorted in descending order, for example:
[/color]
[code]
int changeVector [] =
{
1000, [color=Green]// \$10 bill in cents[/color]
500, [color=Green]// \$5 bill in cents[/color]
100, [color=Green]// \$1 bill in cents[/color]
25, [color=Green]// a quarter[/color]
10, [color=Green]// a dime[/color]
5, [color=Green]// a nickel[/color]
1 [color=Green]// a penny[/color]
};
[/code]
[color=Blue]Great, now we need another array, where we could count the change (how many of each type we need to give.) and here it is, all items are set to zeroes:[/color]
[code]
int countVector [7] = {0}; [color=Green]// size (7) matches the 1st array[/color]
[/code]
[color=Blue]After this all needs to be done is SUBTRACT from the change the amount from array #1 and count these items in array #2. This loop continues until zero is left instead of the change amount:
[/color]
[code]
[color=Green]//
// 13.73 to give to customer
//[/color]
int changeDollars = 13;
int changeCents = 73;
[color=Green]//
// Convert all to cents:
//[/color]
int amountCents = 100*changeDollars + changeCents;
[color=Green]//
// Loop for all items in the array(s)
//[/color]
for (int i=0; (i<7) && (amountCents > 0); i++)
{
[color=Green]//
// Subtract only if it will not result in negative value
// When negative value is a result - move to next coin type
//[/color]
while (amountCents > 0)
{
if ((amountCents - changeVector [i]) < 0) break;
[color=Green]//
// Make one step
//[/color]
amountCents -= changeVector [i];
++(countVector [i]);
}
}
[color=Green]//
// Print results
//[/color]
for (int idx=0; idx<7; idx++)
{
if (countVector [i])
{
printf ("
%d items of %d cents", countVector [idx], changeVector [idx]);
}
}
[/code]