# Thread: problem with my program C++

1. ## problem with my program C++

I'm having trouble comeing up with a solution, I need to make a program that will calculate what was owed with how much was paided and show the change due back but also it needs to show exactly how many dollars, quarters, dimes, nickels, and pennies the person will get back.

My program shows how much thats equal to in dollars, quarters, dimes, nickels, and pennies. Can someone help me.

# include <iostream>
using namespace std;
int main ()
{
double owes, paid, change;
double dol = 1.00;
double qua = 0.25;
double dim = 0.10;
double nic = 0.05;
double pen = 0.01;
cout << "Enter amount owed: \$";
cin >> owes;
cout << "Enter amount paid: \$";
cin >> paid;
change = paid - owes;
dol = change / dol;
qua = change / qua;
dim = change / dim;
nic = change / nic;
pen = change / pen;
cout << "Your change is \$" << change << endl;
cout << "That's equal to " << dol << " Dollars, " << qua << " Quarters, "
<< dim << " Dimes, " << nic << " Nickels, " << pen << " Pennies, " << endl;
return 0;
}

Thanks for helping me out.

2. Here's a tip... use the modulus operator (%). Figure out how to use that, and you'll be set.

AJ

3. What you are looking for is integer division.
When you divide an int by an int the result is an int.
Say for example you had \$3.52 change. Multiply that by 100 to make it an integer and divide it by 100 (an integer value for a dollar), and the result will be 3.
Subtract 3 * 100 from 352 to have 53 left. Divide that by 25 (integer value of a quarter) and you get 2, and so on until you are done with pennies, which divide by 1.
Good luck!

4. OK now im really lost %? how does that work out to getting exact dollars quarters dimes nickles and pennies back to equal out to exact change?

You dont have to hand me the answer but I need a good example so I can better understand. thanks.

5. Sorry for confusing you.

Basically we want an integer number of dollars and quarters. Since we need to do division to get these values, we can get an integer result by dividing two integers (this is why I multiplied the numbers by 100, to avoid decimals). We also want to debit our change after we take the number of dollars, quarters... away. So if we owed 2.54, we would give back 2 dollar, then our change would be 0.54, when we would give back 2 quarters so our change would be 0.04...

Say we just wanted the number of dollars and quarters they would be getting as change. I typed up some sample code
...
double owes, paid, change;

int dollarChange, quarterChange;
int dollarVal = 100; //set a variable for 100 times the value of a quarter and dollar
int quarterVal = 25;

cout << "Enter amount owed: \$";
cin >> owes;
cout << "Enter amount paid: \$";
cin >> paid;

change = paid - owes;

cout << "Your change is \$" << change << endl;

change *= 100; //multiply the change by 100 so we can divide two integers and get a whole number answer

dollarChange = change / dollarVal; //divide your change by value of a dollar
change -= dollarVal * dollarChange; //Subtract the number of dollars you are giving back from the amount of change due to the customer.
quarterChange = change / quarterVal;

cout << "That's equal to " << dollarChange << " Dollars, " << quarterChange << " Quarters, "
<< endl;
return 0;

here is a sample run:

Enter amount owed: \$5.44
Enter amount paid: \$10.00
That's equal to 4 Dollars, 2 Quarters,
Press any key to continue

Hopefully this is a bit more helpful!

6. #include <stdio.h>
#include <math.h>
#include<iostream.h>
int main(void)
{
int i=0;
double result,x,paid,owes,sum=0.0,total=0.0;
double a[] = {1.00,.25,.10,.05,.01};
char s[5][10]={"Dollars","Quarters","Dimes","Nickels","Pennies"};
cout << "Enter amount paid: \$";
cin >> paid;
cout << "Enter amount owed: \$";
cin >> owes;

if(paid>=owes)printf("\nyour change sir: %lf\$\n",x = paid - owes);
else{ printf("\nyou still owe me: %lf\$\n",x=owes-paid);
}
total=x;
printf("\n");
do{
result = fmod(x,a[i]);
double d=x-result;
sum=sum+d;
if(d>0.0) printf("%lf\t%lf\t%s\t\n",d,d/a[i],s[i]);
x=result;
i++;
}while(i<=4);
printf("error :\t%lf",total-sum);
return 0;
}

7. change -= dollarVal * dollarChange
What does the - before the equals sign do to the problem? I think if I could understand this I will understand the process and be able to solve my problem.

8. E1 -= E2 is the same as E1 = E1 - E2

here E2=(dollarVal * dollarChange ) --> change-=dollarVal * dollarChange
E1=change

9. # include <iostream>
using namespace std;
int main ()
{
double owes, paid, change;
int dollar, quarter, dime, nickel, pennies;
int dollarVal = 100, quarterVal = 25;
int dimeVal = 10, nickelVal = 5, penniesVal = 1;
{
cout << "Enter amount owed: \$";
cin >> owes;
cout << "Enter amount paid: \$";
cin >> paid;
change = paid - owes;
cout << "Your change is \$" << change << endl;
}
change = change * 100;
dollar = change / dollarVal;
change = change - dollarVal * dollar;
quarter = change / quarterVal;
change = change - quarterVal * quarter;
dime = change / dimeVal;
change = change - dimeVal * dime;
nickel = change / nickelVal;
change = change - nickelVal * nickel;
pennies = change / penniesVal;
cout << "That's equal to " << dollar << " Dollars, " << quarter << " Quarters, "
<< dime << " Dimes, " << nickel << " nickels, " << pennies << " pennies, " << endl;
return 0;
}

How does this look? I understand it now. The change would become a new value every time I declared it as so: change = change - dimeVal * dime; and so on and so on. correct?

10. Black_death is correct about the -= thing. It's just a shorthand way to write expressions.
x -= 2;
is the same as
x = x - 2;

In fact, if you are incrementing by only 1 you could write any of these 3
x = x + 1;
x += 1;
x++; (You sometimes see this in for loops)

To decrement by 1 x--; works too.

You program looks pretty good to me. Black_death had a good idea with the error checking (making sure they paid enough, especially!) Run it through a good number of test cases, including ones where they get no change, and ones where the amount owed is 0.00 and the amount paid is 10.52(or boundary cases as they are sometimes called). Let us know of any further problems.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•