Help! (Monetary Calculations Using Only Integers)

This is a discussion on Help! (Monetary Calculations Using Only Integers) within the C++ Programming forums, part of the General Programming Boards category; I am new to C++ programming, but I am trying to modify the following code so that I can calculate ...

  1. #1
    Unregistered
    Guest

    Help! (Monetary Calculations Using Only Integers)

    I am new to C++ programming, but I am trying to modify the following code so that I can calculate compound interest using only integers. I only clues I have are to (Treat all monetary amounts as integral numbers of pennies. Then "break" the result into its dollar portion and cents portion by using the division and modulus operations. Insert a period.) If anyone could help me along in the right direction or help me find resources that explain this I would greatly appreciate it. Thanks.

    #include <iostream>

    using std::cout;
    using std::endl;
    using std::ios;

    #include <iomanip>

    using std::setw;
    using std::setiosflags;
    using std::setprecision;

    #include <cmath>

    int main()
    {
    double amount, // amount on deposit
    principal = 1000.0, // starting principal
    rate = .05; // interest rate

    cout << "Year" << setw( 21 )
    << "Amount on deposit" << endl;

    // set the floating-point number format
    cout << setiosflags( ios::fixed | ios::showpoint )
    << setprecision( 2 );

    for ( int year = 1; year <= 10; year++ ) {
    amount = principal * pow( 1.0 + rate, year );
    cout << setw( 4 ) << year << setw( 21 ) << amount << endl;
    }

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    3
    I am not sure that i have completely understood your question(becorse of my english knowledge).

    Just declear your variables as integer. To deal with floating numbers using integers you have to multiply with 10, 100, 1000 to move your decimal point to the left. Then when you are finished you do the opposite.

    Remember that integers are always rounded down. So you have to do this manually.

  3. #3
    Unregistered
    Guest
    I'll try this and see what happens!
    Thanxs

  4. #4
    Unregistered
    Guest
    Did not work! Someone please help!

    Thanxs

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Try: 75 mod 10 = 5. 75 / 10 = 7 (using only integers). 862 mod 100 = 62, 862 / 100 = 8. And so on.
    862 pennies, do the math above.
    Code:
    int x = 862 % 100;  // x = 62
    int y = 862 / 100;  // y = 8
    cout << y << '.' << x;  // prints 8.62
    That should give you a start.
    Last edited by salvelinus; 05-02-2002 at 05:38 PM.
    Truth is a malleable commodity - Dick Cheney

  6. #6
    Unregistered
    Guest
    I'll give it a shot!

    Thanxs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

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