Change Maker (problems with %)

Show 80 post(s) from this thread on one page
Page 3 of 3 First 123
• 03-10-2011
Elysia
If something costs \$50, and I pay \$50.5, do you get .5 subdollars or 5 subdollars, or 5x subdollars, where x is 10 raised to a number of choice?
modf returns the decimals, so 50.5 becomes 50 and .5.
• 03-10-2011
Mr.777
I don't know, why you are not trying to think.... Just play with the functions with different values, not just hit and miss......
Code:

```double paid=50.57; double bill=50.0; double integer_Number, double_Number; double_Number=modf((paid-bill),&integer_Number); cout<<"Your total bill was:"<<bill<<" and you've paid"<<paid<<". So the remaining amount is"<<double_Number<<endl;```
What's so difficult?????
What are the problems???
If there are, you should better try to think logically...... Just don't learn something new from anywhere and if you couldn't do, again go back... First learn good, read about it.... Then implement it with different examples (I mean play with that) and then if you still got problems, share them.....
• 03-10-2011
muffinman8641
Quote:

Originally Posted by Mr.777
I don't know, why you are not trying to think.

I actually am. I've been thinking about this for days. I don't know as much abut functions and the options I have as the mods and users here do, so that's why I started the thread.

It works! I have to stick a statement in there to see if it adds up and if not add a penny, because once in a while it's off. Here's the code, sans the ASCII bunny I put in the final version. :p My teacher has a sense of humor, so it's not like I'll get in trouble for it.

Code:

``` #include <iostream> #include <iomanip> #include <time.h> #include <stdio.h> #include <math.h> using namespace std; int main() {         double bill, paid;     int list;         cout << "CHANGE MAKER" << endl;     cout << "To begin, press ENTER. " << endl;     getchar();         restart:         cout << "\nEnter how much the bill is. " << endl;     cout << "Do not use a \$ sign. Just enter the decimal number. " << endl;     cin >> bill;     cout << "Now enter how much cash you paid. " << endl;     cin >> paid;     if (paid < bill) {             cout << "Uh-oh! You need to pay more cash. You " << endl;             cout << "haven't paid the whole bill yet. " << endl;             cout << "You still owe " << setprecision(2) << fixed << (bill - paid) << ". " << endl;             goto restart;             }         if (bill <= paid) {             cout << "The most efficient way to get your change would be to get" << endl;             int dollars = (paid - bill), quarters = (((paid - bill) - dollars) / 0.25);             int dimes = ((((paid - bill) - dollars) - (quarters * 0.25)) / 0.1);             int nickels = ((((paid - bill) - dollars) - (quarters * 0.25) - (dimes * 0.1)) / 0.05);             int pennies = ((((paid - bill) - dollars) - (quarters * 0.25) - (dimes * 0.1) - (nickels * 0.05)) / 0.01);             cout << dollars << " dollar(s), " << endl;             cout << quarters << " quarter(s), " << endl;             cout << dimes << " dime(s), " << endl;             cout << nickels << " nickel(s), and " << endl;             cout << pennies << " pennies(s). " << endl;             cout << "----------------------------------------------" << endl;             cout << "TOTAL CHANGE: " << (paid - bill) << endl;             cout << "You may have noticed that the number of pennies is incorrect. " << endl;             cout << "This sometimes happens. Just add one. " << endl; } list_reset: cout << "\n\nI would like to... " << endl; cout << "[1] Restart the program " << endl; cout << "[2] Quit the program " << endl; cout << "[3] Receive a compliment, then quit the program " << endl; cout << "[4] See an ASCII picture of a deranged bunny " << endl; cin >> list; if ((list < 1) || (list > 3)) {             cout << "Please enter a number ranging from one to three. " << endl;             goto list_reset;             }             if (list == 1) { goto restart; } if (list == 2) {   cout << "Bye. " << endl; } if (list == 3) {     cout << "\nI like your shirt. " << endl; }     end_program: system ("pause"); return (0); }```
Mr. 777, I'd appreciate it if you'd either help or stop talking, because I don't appreciate being talked down to because I don't understand this concept.
• 03-10-2011
Elysia
T'would be easier if you just took calculated the amount of dollars, then take the rest and calculate the nickels or whatever, and so on.
And get rid of the goto already.
• 03-10-2011
Mr.777
Quote:

Mr. 777, I'd appreciate it if you'd either help or stop talking, because I don't appreciate being talked down to because I don't understand this concept.
First thing, all my advices were just to make you think logically.
Scond thing, do i care???
• 03-10-2011
whiteflags
Quote:

It works! I have to stick a statement in there to see if it adds up and if not add a penny, because once in a while it's off.
Yeah, some numbers cannot be represented exactly in floating point. When accuracy is needed, as is the case with finance, you can always express amounts in terms of pennies.

long money = int(100 * amount + 0.5);

That .5 actually adds the penny for goofy amounts.

What you said you do instead is OK of course.
• 03-11-2011
Mr.777
And what if paid is greater than bill?
I think you should handle that too....
• 03-11-2011
muffinman8641
Quote:

Originally Posted by Mr.777
And what if paid is greater than bill?
I think you should handle that too....

Paid being greater than the bill is the whole program. If you mean paid being LESS than the bill, there's a chunk in there for such events. Take a look and you'll find it.
Show 80 post(s) from this thread on one page
Page 3 of 3 First 123