#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11

    Can't format a number correctly


    Ok, I am not sure where the problem is with this. That's why the topic isn't as detailed as I'd like. I'm working on a script that takes an amount and increases it by X% over a X years. The user can input how much money, what percentage and how many years.

    I put 2000 for the deposit, 20 years for the time and 10 for the %. I keep coming up with this strange #...
    134549992827283680000000.00

    Here's my code.. I've cut out anything I don't think is important...as the rules say not to post the entire code but only the part your having a problem with. My problem is, I don't know where the problem is.
    PHP Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    using namespace std;

    int main () {

        
    float payment 00.00;
        
    float term 0;
        
    float rate 00.00;
        
    float total 00.00;
        
    char confirm [2];
        
        
    start:
        
    cout << "Please enter your deposit amount." << endl;
        
    cin >> payment;
        
    cout << "Please enter the amoutn of time, in years, that you would like to calculate." << endl;
        
    cin >> term;
        
    cout << "Please enter the interest rate you wish to use." << endl;
        
    cin >> rate;
            
        
    cout << "Is this information correct?" << endl << endl;
        
    cout << "Deposit Amount: " << payment << endl;
        
    cout << "Number of Years: " << term << endl;
        
    cout << "Interest Rate: " << rate << endl << endl;
            
        
    cout << "Press 'Y' if everything looks ok or 'N' to start over.";
        
    cout <<endl;
        
    cin >> confirm;

    if (
    stricmp(confirm"n") ==0)
          {
          goto 
    start;
          }
          else
          {
          
    total payment + (payment * (1+pow(rate+1term))/rate);
          
    cout << fixed;
          
    cout << setprecision(2) << total;
          
    /* cout << setprecision(1); */
          
    cout << endl << endl;
          
    cout << endl << "Good";
          }
          
    cout << endl;


    Thanks in advance for any help!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Stupid question:
    Shouldn't you divide by 100.0 the value of rate input by the user? C doesn't know about X%, but it does know about (X/100.0) .
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    You have a problem here too:
    Code:
    cout << "Press 'Y' if everything looks ok or 'N' to start over.";
        cout <<endl;
        cin >> confirm;
    
    if (stricmp(confirm, "n") ==0)
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    You are also using the dreaded GOTO statement. You should avoid that as it generally leads to messy code. You should put it in a do/while loop.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11
    Originally posted by 7stud
    You have a problem here too:
    Code:
    cout << "Press 'Y' if everything looks ok or 'N' to start over.";
        cout <<endl;
        cin >> confirm;
    
    if (stricmp(confirm, "n") ==0)
    Whats wrong with that?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11
    Originally posted by dwise1_aol
    Stupid question:
    Shouldn't you divide by 100.0 the value of rate input by the user? C doesn't know about X%, but it does know about (X/100.0) .
    so,
    Code:
    total = total / 100;
    ?
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by dcj1978
    so,
    Code:
    total = total / 100;
    ?
    I would rather try:
    Code:
    total /= 100.0f;
    in order to avoid any possible implicit type conversions. BTW, you wouldn't need the 'f' after the 100.0 constant if you used double.

IMN logo majestic logo threadwatch logo seochat tools logo