Change Maker (problems with %)

This is a discussion on Change Maker (problems with %) within the C++ Programming forums, part of the General Programming Boards category; If something costs $50, and I pay $50.5, do you get .5 subdollars or 5 subdollars, or 5x subdollars, where ...

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    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.....

  3. #33
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Quote Originally Posted by Mr.777 View Post
    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. 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.
    Last edited by muffinman8641; 03-10-2011 at 09:42 AM.

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    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???

  6. #36
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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.

  7. #37
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    And what if paid is greater than bill?
    I think you should handle that too....

  8. #38
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Quote Originally Posted by Mr.777 View Post
    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.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change maker program remainder troubles.
    By muffinman8641 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2011, 02:41 PM
  2. Making a change counting program
    By Scrogdor in forum C Programming
    Replies: 15
    Last Post: 02-23-2011, 01:00 PM
  3. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  4. Replies: 2
    Last Post: 09-04-2001, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21