Thread: How do you ??????

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    How do you ??????

    What I want to do is to insert a blank line every 6 lines in the print out, here is what I have:

    // This program produces a loan amortization

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    using namespace std;

    int main()
    {

    ofstream outfile("A:Mort.txt");
    char name[31];
    float loan, rate, years, balance, term, payment;

    cout << "Enter the name of the mort. recipant. ";
    cin.getline(name, 31);
    cout << "Loan amount: $";
    cin >> loan;
    cout << "Annual Interest Rate: ";
    cin >> rate;
    cout << "Years of loan: ";
    cin >> years;

    term = pow((1 + rate / 12.0), 12.0 * years);
    payment = (loan * rate / 12.0 * term) / (term - 1.0);

    outfile.precision(2);
    outfile.setf(ios::fixed | ios::showpoint | ios::left);
    outfile << "The loan belongs to : " << name << endl;
    outfile << "Monthly payment: $" << payment << endl;

    outfile << endl;
    outfile << setw(10) << "Pay. No.";
    outfile << setw(10) << "Interest";
    outfile << setw(10) << "Principal";
    outfile << setw(10) << "Balance" << endl;
    outfile << "-------------------------------------\n";

    balance = loan;
    for (int month = 0; month < (12 * years); month++)
    {
    float minterest, principal;


    minterest = rate / 12 * balance;
    principal = payment - minterest;

    outfile << setw(10) << (month + 1);
    outfile << setw(10) << minterest;
    outfile << setw(10) << principal;
    outfile << setw(10) << balance << endl;

    balance -= principal;
    }


    outfile.close();
    return 0;

    }



    So I was thinking I would add :
    if ((month%6)==0)
    outfile << "Question how do you display a blank line?" ;
    For some reason I can't remember how to display a blank line


    TIA
    Bill

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    I don't know much about fstreams, but wouldn't

    outfile<<endl<<endl

    do the trick?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    This is where I put the if statement

    balance -= principal;
    }
    if ((month % 6) == 0)
    {
    outfile << endl<<endl;
    }
    That didn't seam to work but thanks anyway... Maybe I put it in the wrong place????

    Anybody else?????

    TIA
    Bill

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Maybe I put it in the wrong place????
    I think you did

    balance -= principal;
    } <--- this is the closing brace of your for loop

    // which is odd, because isn't month now supposed to be out of scope?
    // in any event, it only happens once, when the for loop has finished.
    if ((month % 6) == 0)
    {
    outfile << endl<<endl;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    Thanks Salem I got that fixed, however when it printed to my out file it skipped the 1: here let me show you...

    1 44.67 72.81 6700.00

    2 44.18 73.29 6627.19
    3 43.69 73.78 6553.90
    4 43.20 74.27 6480.12
    5 42.71 74.77 6405.85
    6 42.21 75.27 6331.08
    7 41.71 75.77 6255.82

    8 41.20 76.27 6180.05
    9 40.69 76.78 6103.78
    10 40.18 77.29 6027.00
    11 39.66 77.81 5949.70
    12 39.15 78.33 5871.90
    13 38.62 78.85 5793.57 \

    See it didn't do right, it supposed to leave a blank line after the no. 6...How can I fix this???? Any suggestions????


    TIA
    Bill

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Month starts at 0 (in the for loop), so month % 6 will also be 0

    The test you need is
    if ( (month+1) % 6 ) == 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    Thanks Salem that did the trick....

    Thanks
    Bill

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    One last question.... How do I get the totals of principal and interest.... I want to display them every 36th line....

    TIA
    Bill

Popular pages Recent additions subscribe to a feed