Off by a penny problem

This is a discussion on Off by a penny problem within the C++ Programming forums, part of the General Programming Boards category; Getting the off by a penny problem... Enter an amount in double, for example 11.56: 12.35 Your amount 12.35 consists ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    39

    Off by a penny problem

    Getting the off by a penny problem...

    Enter an amount in double, for example 11.56: 12.35
    Your amount 12.35 consists of
    12 dollars
    1 quarters
    0 dimes
    1 nickels
    4 pennies

    Press ENTER to continue...

    Where do I properly add my + .00001

    I tried a few places but it did not work...any insight...

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      // Receive the amount
      cout << "Enter an amount in double, for example 11.56: ";
      double amount;
      cin >> amount;
    
      int remainingAmount = static_cast<int>(amount * 100);
    
      // Find the number of one dollars
      int numberOfOneDollars = remainingAmount / 100;
      remainingAmount = remainingAmount % 100;
    
      // Find the number of quarters in the remaining amount
      int numberOfQuarters = remainingAmount / 25;
      remainingAmount = remainingAmount % 25;
    
      // Find the number of dimes in the remaining amount
      int numberOfDimes = remainingAmount / 10;
      remainingAmount = remainingAmount % 10;
    
      // Find the number of nickels in the remaining amount
      int numberOfNickels = remainingAmount / 5;
      remainingAmount = remainingAmount % 5;
    
      // Find the number of pennies in the remaining amount
      int numberOfPennies = remainingAmount;
    
      // Display results
      cout << "Your amount " << amount << " consists of \n" <<
        "\t" << numberOfOneDollars << " dollars\n" <<
        "\t" << numberOfQuarters << " quarters\n" <<
        "\t" << numberOfDimes << " dimes\n" <<
        "\t" << numberOfNickels << " nickels\n" <<
        "\t" << numberOfPennies << " pennies";
        
    /* Scaffolding code for testing purposes */
    cin.ignore(256, '\n');
    cout << "Press ENTER to continue..." << endl;
    cin.get();
    /* End Scaffolding */
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Before division I think. But the real solution is to keep track of money using an integer to represent pennies or whatever the smallest unit is.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    hmm... it works for me
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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