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; Originally Posted by Mr.777 You must declare dollars variable as double datatype and then you can format the decimal point ...

  1. #16
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Quote Originally Posted by Mr.777 View Post
    You must declare dollars variable as double datatype and then you can format the decimal point using flags or setprecision....
    Good idea! I made my code this:
    Code:
    double bill, paid, dollars = (paid - bill);
    and also this:
    Code:
    cout << "The most efficient way to get your change would be to get:" << endl;
                 cout << setprecision(0) << fixed <<  dollars << " dollars, " << endl;
    Problem is, when I'm testing the program, it says "The most efficient way to get your change would be to get:", then nothing. Then it says the program stopped working. Basically, it crashes.

  2. #17
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Because you've just declared the variables bill and paid but didn't assign any value to them... So, do;
    Code:
    double bill, paid;
    bill=some_Value;
    paid=some_Other_Value;
    double dollars=paid-bill;

  3. #18
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    The user defines the value of the bill and paid in the program.

  4. #19
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Then,
    Code:
    double paid,bill;
    cin>>paid>>bill;
    double dollars=paid-bill;
    Try to think pleaseeeeeeeeeeeee...........

  5. #20
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    I figured that part out, just one last question and this program will (hopefully) be complete;
    how do I display a math result (e.g. (bill - paid)), then save the answer for use in the next math computation? Return?

  6. #21
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Oh God.......
    Don't you know, how to show something over console (monitor)??? If you really don't, just read then.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Again, saving results typically means you remember something, and in the computer world, that means saving the result to a variable. How and when? That's for your logical institution to decide.
    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.

  8. #23
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Is there a way to display ONLY the decimals of a number? I'm just asking odd questions like this here and there trying different ideas; I googled it with no results.

    For example, if the number was 5.68, how could I get the program to display ONLY the 0.68? Don't say subtract 5 either, it will be different almost every time.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    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.

  10. #25
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    OK, here's my code:
    Code:
     #include <iostream>
    #include <iomanip>
    #include <time.h>
    
    using namespace std;
    
    int main() {
        
    
        double bill, paid;
        int list;
        
        cout << "Welcome to my change-maker program! " << 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;
                 cout << setprecision(0) << fixed << (paid - bill) << " dollars, " << endl;
                 cout << "----------------------------------------------" << endl;
                 cout << "TOTAL: " << (paid - bill) << endl;
    
    }
    list_reset:
    cout << "I 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;
    
    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);
    }
    As you can see, there's a part that says:
    Code:
    cout << "The most efficient way to get your change would be to get:" << endl;
    cout << setprecision(0) << fixed << (paid - bill) << " dollars, " << endl;
    Below that is where I'd like to stick the rest with the quarters and change.

    I tried modf, but I was confused and messed it up. I'll look more into it.

    Other than the imminent criticisms about goto, what can I do to it to make it do what I want?

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Calculate dollars it's possible to give back, then calculate whatever sub-dollars it's possible to give back, and so on.
    Just like turning seconds into hours, minutes and seconds. Piece of cake.
    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.

  12. #27
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I think modf will be the best option for you in this case.....
    I am giving you an example of using modf...
    Code:
    double integer_Number, decimal_Number;
    double decimal_Number=modf(5.68,integer_Number);
    // This will return 0.68 to the decimal_Number and 5 to the integer_Number.
    Try using this....

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    That should be
    Code:
    double integer_Number, decimal_Number;
    double decimal_Number=modf(5.68, &integer_Number);
    // This will return 0.68 to the decimal_Number and 5 to the integer_Number.
    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.

  14. #29
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    My bad :-)

  15. #30
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Code:
     cout << "The most efficient way to get your change would be to get:" << endl;
                 double integer_number;
                 double decimal_number = modf((bill - paid), &integer_number);
                 cout << setprecision(0) << fixed << (bill - paid) << " dollars, \n";
                 cout << setprecision(0) << fixed << (decimal_number / 4) << " quarters, \n";
                 cout << setprecision (0) << fixed << ((integer_number / 4) / 10) << " dimes, \n" ;
                 cout << setprecision (0) << fixed << (((integer_number / 4) / 10) /5) << " nickels, and\n";
                 cout << setprecision (0) << fixed << ((((integer_number / 4) / 10) /5) / 1) << " pennies.\n" ;
                 cout << "----------------------------------------------" << endl;
    It returns the dollars, but then 0 for all the coins. It also puts a hyphen (-) before each one, as if it's negative, but also the zeroes.
    [Edit: noticed that I did bill - paid. Should be paid - bill]

    Any corrections?
    Last edited by muffinman8641; 03-10-2011 at 08:57 AM.

Page 2 of 3 FirstFirst 123 LastLast
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