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

Pages: 123
You can increament/decrement, by what ever means you like. The loop will work fine( not infinite ) so long as the middle statement of the for-loop will eventually be met.

EDIT:
as LB and roberts said, which I never noticed... lol. You need to actually change the result of the last statement in the for-loop.

You can do the following, which will have no result on the variables used:
1
2
3
4
int var1 = 5;
int var2 = 6;

std::cout << var1 + var2;


this will add both variables for the print out, but both variables will have NOT changed!

To actually assign to a variable, you'll need a prefix/postfix of ++/-- or variable += 1/variable -= 1 OR other...
Last edited on
Yes, you can.

Look at this:

 
	for( double x_span = bb[MIN].x; x_span < bb[MAX].x; x_span =+ DEFAULTRES )


This sets x_span to bb[MIN].x, evaluates it against bb[MAX].x and in each loop, it increments x_span by DEFAULTRES, but the =+ stores the updated value back in x_span. It's the same as if I had written:

 
	for( double x_span = bb[MIN].x; x_span < bb[MAX].x; x_span = x_span + DEFAULTRES )
May have missed my post at the end of the previous page:
http://www.cplusplus.com/forum/beginner/60616/#msg327777

EDIT: @Roberts: definitely.
Last edited on
It was probably a timing issue. When I posted, no one had responded.
No offence roberts, but looking at your code examples, I would have had a few problems in understanding...

You're using a class/struct? within your loop, yet the OP( original poster ) is asking for help with a for-loop... If I had learnt about classes before learning about loops, I'd know a lot less than I do today.
None taken Lynx, it was probably overkill. The OP needed to focus on the conditional, which is why that was the focus of the summary, not really use of STL containers.

It was simply easier to cut & paste from my code since I have it up ready to continue work.
simplicity, sometimes, is the best learning curve ;)
I think you're all great. So darn helpful. Unfortunately, (for me) my internet just went down for about the last 3 hours, and I was alone with my program. Scary, I know. So, speaking of overkill, here's my code, yet again, but different. I have gotten it to do (kind of) what I want, but I wonder if what I have is even going to work. Another student emailed me and said the following:
Start with solving the equation for one cell. Then loop for a row. Then loop for years. Add column and row headers for dressing. Fix spacing.

That was not what I had envisioned. I have figured a way to increment my rates which will be my table header row. But my row is too long, currently, and I'm looking for ways to break it up into multiple tables. This just seems very hard for a second programming class ever, but I'm sure trying.
I've been at this desk for the better part of 13 hours, and I'm going to do it all again tomorrow. For the same program. So, if anyone has any easily understood pointers for a girl who is VERY (haha, obviously) new to this, please shed some light. The end result should be the mortgage amounts at every rate from rateA to rateB incremented by inc, and with as many rows as is input years. Fun.

Oh! And a question: if I'm supposed to show 5 digits of accuracy, how do I do that? What I have is 5 decimals, I know, but I can't find another way...

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
//*************************************************************
//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 &mortgage, double &rateA,  double &rateB, double &inc, int &yearA, int &yearB);//Function gets interest rate range, increment, and year range
void createTable(double &mortgage, 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 mortgage, rateA, rateB, inc;//Declare variables
    int yearA; int yearB;

    openOutfile();
    getValues(mortgage, rateA, rateB, inc, yearA, yearB);
    cout << "       Monthly Payment Factors Used in Computing Monthly Payments" << endl;
    cout << "                             Interest Rates                      " << endl;
    cout << endl;
    cout << "Years" << endl;
    createTable(mortgage, rateA, rateB, inc, yearA, yearB);
    outfile.close();
    return 0;
}

int openOutfile()
{
    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 &mortgage, double &rateA,  double &rateB, double &inc, int &yearA, int &yearB)
{
    cout << "Enter amount to borrow." << endl;
         cin >> mortgage;
    cout << "Enter interest rate range (two values)," << endl
         << "and increment amount separated by spaces." << endl;
         cin >> rateA >> rateB >> inc;
    cout << "Enter the year range amount separated by a space." << endl
         << "ex. 1 15" << endl;
         cin >> yearA >> yearB;
    return;
}

void createTable(double &mortgage, double &rateA,  double &rateB, double &inc, int &yearA, int &yearB)
{
    while (rateA <= rateB)
    {
       cout << "        " << setw(5) << showpoint << rateA ;
       rateA = rateA + inc;
    }
}
Some tips:

1. I would not declare:

ofstream outfile;//File descriptor

as global. For simple programs like this, there really is no need to declare globally, so definitely learn to declare locally. Global variables and declarations should be as sparse as possible and only when you have no other choice. You can pass "outfile" as a parameter to any function that needs to read in from or out to the file.

2. You open the file in openOutfile, but close it in main, hence, why you don't want to declare globally. It's easy to lose track of where something is happening with critical functions like these spread all over. You can open and close the file in main and it's perfectly within scope.

3. Open files when you are finally ready to read/write and close them as soon as you're done. While most modern OSs will free allocated memory and close any open files upon program exit, it's simply a good practice toward organized flow.

Instead of using setw to format the field width, use setprecision( http://www.cplusplus.com/reference/iostream/manipulators/setprecision/ ).

What do you mean by "But my row is too long?" Is there a condition on the line length of a row?

I'm still confused about this "table." What is the structure and data you need to represent in this table?
Last edited on
Thanks for the tips. As for the table:
You are to write a program to generate a table of factors only used to compute monthly payments for money borrowed. The formula to calculate monthly payments is as follows, and the factor times the mortgage will give you monthly payment.
Formula:
(I+1)^N * I
(I+)^N -1 *Mortgage

I= 1/12 of the interest rate
N = Total months in term

The user is prompted for the interest rate range (2 values), the increment, and the range of years of length for the loan ( ex. 1 15).
The output should be a table of these factors, but since the table will be so large, it should be broken up into multiple tables. Only about 7 columns or so per table. The rows in the table will be however many years were input by the user.

Ex._______________________Interest Rates_____________________________
----------------------8.00-------8.25-------8.50-------8.75-------9.00-------9.25-------9.50
Years---1
----------2
----------3
----------4 and the middle here is full of what the payment would be for each rate, for each year.



I changed your createTable function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void createTable(double &mortgage, double &rateA,  double &rateB, double &inc, int &yearA, int &yearB)
{
	int i = 0; // for use with formatting rates
	int pYear = 1; // year to print

    while (rateA <= rateB)
    {
		if( ! ( i % 7 ) ) // go to a new line and print year i, every 7 columns
		{
			cout << "\nYear " << pYear++; // print the current year, then increment it
		}

       cout << '\t' << setw(5) << showpoint << rateA ;
       rateA = rateA + inc;

	   // increment i
	   ++i;
    }
}


EDIT:
Was this similar to what you wanted?

EDIT2:
Also, you only need to pass variables by reference if you want the function to change them.
In your creareTable function, rateB never changes, so pass it normally;
void createTable( double &rateA, double rateB, ... )
Last edited on
Kind of. All those values need to be the column headers for the table (or multiple tables, since there will be so many, depending on the numbers the user inputs. I've been working on that function trying to put in that equation to get the factors, but I'm getting the word nan over and over....what does that mean and how do I fix it?
This is what I've been trying...

1
2
3
4
5
6
7
8
9
10
11
12
13
void createTable(double &mortgage, double rateA,  double &rateB, double &inc, int &yearA, int &yearB)
{
    double factor;//Create variable for each table entry
    while (rateA <= rateB)
    {
       cout << "        " << fixed << setprecision(5) << rateA;//This isn't right, but I need to only have 7 rows and each table entry must have 5 digits of accuracy
       rateA = rateA + inc;//Increment my rateA by inc 
       factor =(pow(((1/12)*(rateA/100) + 1),yearA * 12) * (1/12 *(rateA/100))) / (pow((1/12 * (rateA/100) + 1), yearA*12) -1) * mortgage; //This is the referenced equation, I think I did it right...but I keep getting nannannan here....
       cout << factor << "        ";
       outfile << factor << "        ";//Put this here so I could see what the calculation showed.
    }

}
This is such a nightmare!!
I know, I'm trying to work it out now! ahaha.

Just trying to work out the formulae. The second part, of which to divide by;
(I+)^N -1 *Mortgage

What is added to I ? ( I+ )
1
Has it given you a tic yet? This awful program made my left eye start twitching late last night. Then it started right back up again this morning as soon as I started working on it again...I keep trying different things, but the output doesn't change at all! Grrrr.
Sorry, I've kind of gave up for a while, it's hurting my head! ahaha.

I'll go back to it later!
I understand. Well, I have to keep at it, but the teacher's hint was to write the code to print one table correctly then add code to print multiple tables, so I'm going to start the createTable function over again and try to do that.

Oh, can you tell me, in a sentence, exactly what this says:
if( ! ( i % 7 ) ) // go to a new line and print year i, every 7 columns

I understand what it does, but not why. I think I have to know this in order to move on...

Thanks for your help. And if you should figure it out before tonight, I'll be your best friend.
ErinCorona wrote:
Oh, can you tell me, in a sentence, exactly what this says: (sic)

If the result of the expression: (i % 7) is zero, or false, execute the following statements.

Wazzak
No, I get that. I don't understand what ( i % 7) means.
Pages: 123