Trying to not use all global variables...getting an error

Pages: 123
The % operator will divide by the right side and give the remainder.
i.e.
7 % 7 is 0.

7 goes in to 7 with no remainder.

So when you add the ! operator, the 0( which is false ) will become true.

So when i is 0, or a multiple of 7, the remainder will always be 0( false ). Add the ! and the if statement will be true and run the statements within.

Hope I explained that in a good way, lol.

I'm not sure if this is correct, but from the formulae, I came up with this:
1
2
3
4
double months = ( yearB - yearA ) * 12; // get the range and work out the months
double i = ( rateB - rateA ) / 12; // get the range and work out the monthly rate

factor = ( pow( i + 1, months ) * i ) / ( ( pow( i + 1, months ) - 1 ) * mortgage );



EDIT:
i was incremented each time that rateA was < rateB. I did it this way, so that when 7 columns( rates ) were printed, it would make a newline and continue with the other rates.
Last edited on
The modulus operator yields the remainder of the division of i and 7.

Wazzak
Okey dokey. Just called my teacher to have him clarify and we don't need to input a mortgage, and the table's factors are not the monthly payments after all....the table is just of the 'factors.' So all I need to put in there is the decimal number that I get when I do the division up there minus the * mortgage amount. I think I may be able to do this now....I can't believe I've been doing it wrong all this time! Grrrr. Thanks for all the help. I'm really sorry my misunderstanding of the directions wasted your time. :(
No problem! And it wasn't a waste of time if you learnt something! (:
Finally finished! Run it! Run it Here's what it was supposed to do, for anyone who cares. ;)-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//*************************************************************
//This program generates a table of factors used to
//compute monthly payment amounts for money borrowed.
//Again, this program does NOT compute monthly payment,
//only a table of factor values.
//*************************************************************

#include <iostream>//Header files
#include <cmath>
#include <fstream>
#include <iomanip>

using namespace std;

ofstream outfile;//File descriptor

//Prototypes
int openOutfile(); //Function opens the answer outfile
void getValues(double &rateA,  double &rateB, double &inc, int &yearA, int &yearB);//Function gets interest rate range, increment, and year range
void createTable(double& rateA, double& rateB, double &inc, int &yearA, int &yearB);//Function creates a table of factors using user-provided values to compute

//Begin main
int main()
{
    double rateA, rateB, inc;//Declare variables
    int yearA; int yearB;

    openOutfile(); //Call function 1
    getValues(rateA, rateB, inc, yearA, yearB); //Call function 2
    cout << "               Monthly Payment Factors Used in Computing Monthly Payments" << endl;//Display table
    cout << "                                     Interest Rates                      " << endl;//title
    cout << endl;
    outfile << "               Monthly Payment Factors Used in Computing Monthly Payments" << endl;//Echo
    outfile << "                                     Interest Rates                      " << endl;
    outfile << endl;
    createTable(rateA, rateB, inc, yearA, yearB); //Call function 3
    outfile.close(); //Close outfile
    return 0; //Quit
}

int openOutfile()//Function definition 1
{
    outfile.open("answers.out");//Open an outfile for answers
    if( !outfile) //Check to see if outfile opens correctly
    {
        cout << "Error opening output file." << endl; //If it doesn't open correctly, display message
        return 0;                                     //and quit.
    }
}

void getValues(double &rateA,  double &rateB, double &inc, int &yearA, int &yearB)//Function definition 2
{
    cout << "Enter interest rate range (two values)," << endl //Prompt for interest
         << "and increment amount separated by spaces: " << endl;//rate values and increment amount
         cin >> rateA >> rateB >> inc; //Read in interest rate values and increment amount
    outfile  << "Enter interest rate range (two values)," << endl //Echo
         << "and increment amount separated by spaces: " << rateA << " " << rateB << " " << inc << endl;
    cout << "Enter the year range amount separated by a space: " << endl //Prompt for year range
         << "ex. 1 15" << endl;
         cin >> yearA >> yearB; // Read in year range
    outfile  << "Enter the year range amount separated by a space: " <<  yearA << " " << yearB << endl << endl;//Echo
    return;
}

void createTable(double &rateA,  double &rateB, double &inc, int &yearA, int &yearB)//Function definition 3
{
    double tempRate = rateA;//declare variable for beginning rate value
    while ( tempRate <= rateB) //while it is less than or equal to the max rate value
    {
        double factor = 0;//  declare and initialze variable for computing the factors
        cout << "  "; //Insert a space for formatting
        outfile << "  ";//Echo
        for (int count = 0; count < 6 && tempRate <= rateB; count++)//For as long as count is less than or
        {                                                           //equal to 6 and your beginning value is less than or equal to max value
            cout << fixed << setw(13) << setprecision(2) << tempRate;//Display the rate in increments
            outfile << fixed << setw(13) << setprecision(2) << tempRate;//Echo
            tempRate = tempRate + inc;//Increment your rate value
        }
        cout << endl;//New line for formatting
        outfile << endl;//Echo
        cout << "________________________________________________________________________________" << endl;//Display a line for formatting
        outfile << "________________________________________________________________________________" << endl;//Echo

        for (int count = yearA; count <= yearB; count++)//For as long as count is less than or equal to your max year
        {
            tempRate = rateA;//Declare and initialize variable to beginning rate value
            cout << setw(2) << count;//Display the rate value
            outfile << setw(2) << count;//Echo
            for (int i = 0; i < 6 && tempRate <= rateB; i++)//For as long as i is less than or equal to 6 and
            {                                               //tempRate value is less than max value
                double eyeplusone = (tempRate /1200) + 1;//Declare variable for part the numerator of the equation
                double exp = pow(eyeplusone, count * 12);//Declare variable for the entire numerator
                double factor =(exp * tempRate/1200) / (exp -1);//Declare variable for entire equation
                cout << fixed << setw(13)<< setprecision(5) << factor;//Display factor
                outfile << fixed << setw(13)<< setprecision(5) << factor;//Echo
                tempRate = tempRate + inc;//Increment factor
            }
            cout << endl;//Display line for formatting
            outfile << endl;//Echo
        }
    cout << endl;//Display line for formatting
    outfile << endl;//Echo

    rateA = tempRate;//Initialize variable to last value for next set of columns
    }
}

If it produces the results you need, then consider it a success at this point.
Roberts he has, he isn't asking for more help if you had read what he said ;)
Nicely done! Hope we helpped in some way (:
Topic archived. No new replies allowed.
Pages: 123