Like Tree1Likes
  • 1 Post By grumpy

Missing something, works fine until the end

This is a discussion on Missing something, works fine until the end within the C++ Programming forums, part of the General Programming Boards category; Code: #include Code: <iostream> #include <string> #include <iomanip> using namespace std; int main() { //declare variables int dollars = 0; ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    Missing something, works fine until the end

    Code:
    #include
    Code:
    <iostream>
    #include<string>
    #include<iomanip>
    usingnamespace std;
    int main()
    {
    //declare variables
    int dollars = 0;
    int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    int pennies = 0;
    int change = 0;
    double amountDue = 0.0;
    double amountPaid = 0.0;
    double valChange = 0.0;
    //enter input items
    cout << "Enter the amount due: $";
    cin >> amountDue;
    cout << "Enter the amount the customer paid: $";
    cin >> amountPaid;
    //calculate
    valChange = amountPaid - amountDue;
    change = valChange * 100;
    dollars = change / 100;
    change = change - (dollars *100);
    quarters = change / 25;
    change = change - (quarters * 25);
    dimes = change / 10;
    change = change - (dimes * 10);
    nickels = change / 5;
    change = change - (nickels * 5);
    pennies = change;
    //display output
    cout << "The change amount is: $" << valChange << endl;
    cout << dollars << " dollars " << quarters << " quarters " << dimes << " dimes " << nickels << " nickels " << pennies << " pennies." << endl;
    return 0;
    }
    

    When I run this, it works fine until nickels and pennies. If there are no nickels, pennies calculate correctly. If there are nickels, it shows 0 pennies always.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    What inputs are you supplying, what output is being produced, and how is it different from what you expect?

    The only issues I can see are;

    1) your inputs are floating point, so you would potentially being hit with precision concerns, as floating point types cannot precisely represent fractional powers of ten (0.1, 0.01, etc). This is an issue you may see, or you may not - depending on how lucky you get with errors cancelling out or reinforcing when subtracting. The calculation of nickels and pennies are most likely (of the ones you are doing) to be affected by such concerns.

    2) Multiplying a floating point value by 100 may yield a value that overflows an int. If that happens, all bets are off with your code. The values where this can happen are compiler dependent (range of values that a float can represent or that an int can represent are implementation defined).

    3) Another issue may arise if amountPaid is less than the amountDue. The results of expressions in your code for such inputs are compiler dependent, as it is implementation defined whether division of negative operands rounds up or down.

    Those quibbles aside, I can't see how your code would produce incorrect numbers of nickels or pennies. Which leads me to guess that your expectations of "correct" behaviour differs from mine.
    Last edited by grumpy; 01-18-2012 at 10:30 PM.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,552
    Quote Originally Posted by grumpy View Post
    1) your inputs are floating point, so you would potentially being hit with precision concerns, as floating point types cannot precisely represent fractional powers of ten (0.1, 0.01, etc). This is an issue you may see, or you may not - depending on how lucky you get with errors cancelling out or reinforcing when subtracting. The calculation of nickels and pennies are most likely (of the ones you are doing) to be affected by such concerns.
    I ran the code and saw the above issue using MinGW GCC (Likely TDM build). Change of $1.43 resulted in 142 cents of change being calculated.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    This is just a most basic program, the way it is described, the amountPaid will always be higher than the amountDue. Since I can't seem to get an int variable to use decimal places even with:
    fixed << setprecision(2);
    I figured going from double to int would cause precision errors, I just couldn't figure out how to do it.
    If you want to see where the errors are, run it and answer:
    amount due 76.34
    amount paid 80.00

    run it again and use:
    amount due 76.14
    amount paid 80.00

    you will see what I mean.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,552
    Since you are trying to figure it out and failed; I will give you the fix to one of the issues.

    Your code
    Code:
    change = valChange * 100;
    IIRC, what I tried at school to fix this rounding bug.
    Add 1/2 of a cent to the dollar amount this results in rounding the value up.

    Code:
    change = (valChange) + 0.005 * 100;
    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Instead of reading in a floating point value, try reading two integers separated by a full stop (where the second integer has, at most, two digits).

    Even better, read input as a string (look up the function fgets() for a way to do that) and interpret the string as two integers separated by a full stop.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by grumpy View Post
    ...look up the function fgets() for a way to do that...
    You mean std::getline, of course.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This works fine with IP 127.0.0.1 but fails with my REAL IP.
    By azjherben in forum Networking/Device Communication
    Replies: 15
    Last Post: 05-19-2009, 10:28 PM
  2. Compile issue in g++, works fine in VS
    By bean66 in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2009, 09:48 AM
  3. It works just fine but
    By jcmhex in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2005, 06:53 PM
  4. Wont run in X, works fine in alt+F1 terminal
    By zmerlinz in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2004, 11:58 AM
  5. My computer works just fine
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-02-2002, 08:51 AM

Tags for this Thread


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