# Huh? Can anyone explain?

• 03-30-2002
Tarls
Huh? Can anyone explain?
I keep getting this when I try to compile my program:

name lookup of `month' changed for new ISO `for' scoping
temp.cpp:16: using obsolete binding at `month'

Does anyone know what this means? And even better, does anyone know how to fix it?

Thanks,
Tarls.:confused:
• 03-30-2002
blight2c
no, but if you post the code i'll try to help :)
• 03-30-2002
Tarls
That'd be great, only I've screwed the code up so much it doesn't really make a whole lot of sense anymore! :rolleyes:

This is what I'm trying (with little success and a whole lot of frustration) to make it do:

-allow the user to input an amount of money, annual percent interest, and a time (in years) of investment. No worries, I did that.

- call a function (oh great. fuctions. kill me now...) to calculate and display a table of monthly balances for the account that compounds interest every three months.

So it should go something like this:

Enter the principal amount to invest: 5000
Enter the annual percent interest earned: 3.4
Enter the investment time in years: 0.5

Interest compounded quarterly:

Length Investment Interest Total
1 5000.00 0.00 5000.00
2 5000.00 0.00 5000.00
3 5000.00 42.50 5042.50
4 5042.50 0.00 5042.50
5 5042.50 0.00 5042.50
6 5042.50 42.86 5085.36

I figured out that to display 0.5 years as six seperate months in the table I had to put:

for (int month = 1; month <= (years * 12); month ++)

But now I can't figure out how to get the interest to calculate (let alone display) on every third month (I'll worry about the investment and total later) - I thought something like

if (month % 3 == 0)
interest = (annualPercentInterest / 100) * total / 4;
else
interest = 0;

But I can't get it to work, and I have no idea how to make it display in the table. I only started learning C++ four weeks ago and it still isn't making a whole lot of sense.

Argh. If someone could help me out here I'd be so very very grateful. (I know I'm probably asking alot here, I'm just getting incredibly, terrifyingly desperate to get this done!) Any help anyone could give me would be very much appreciated.

Thanks,
Tarls.
• 03-30-2002
blight2c
would something like this help:

Code:

```cout<<tab<<"month"<<tab<<tab<<"balance"<<endl;         do         {                 i++;                 if (i%3==0)                 {                         balance=f_calc(balance, intrest);                 }                                 cout<<tab<<i<<tab<<tab<<balance<<endl;         }         while (i<payout);```
• 03-30-2002
Tarls
Hey, thanks, I'll give it a try!
• 03-30-2002
taylorguitarman
It would appear that the compiler has a problem with the variable month: a scoping issue related to the for loop.
Post the code you have (doesn't matter what it looks like) and I'm guessing the problem lies within the first 16 lines (if you haven't changed it yet). Also what compiler are you using?
• 03-31-2002
Tarls
Okay, well this is what I have... as for what compiler I'm using, and this is probably going to sound really stupid, I don't know!! I'm using Tera Term to connect to the turing system at my university, I type the code in the Xemacs window and compile using the 'g++ name.cpp -o name' command; if any of that tells you what compiler I'm using, great. If not, sorry, that's all I know. :-|

#include <iostream>
using namespace std;

int main ()
{
double amount, percentInterest, length, interest;
cout <<"Enter the amount to be invested: ";
cin >> amount;

cout <<"\nEnter the annual percent interest earned: ";
cin >> percentInterest;

cout <<"\nEnter the length in years of the investment: ";
cin >> length;

for (int month = 1; month <= (length*12); month++)

if (month % 3 == 0)
double interest = percentInterest / 100 * amount / 4;
else
interest = 0;

cout << month << "\t" << interest << endl;
• 03-31-2002
salvelinus
Quote:

Originally posted by Tarls

for (int month = 1; month <= (length*12); month++)

if (month % 3 == 0)
double interest = percentInterest / 100 * amount / 4;
else
interest = 0;

cout << month << "\t" << interest << endl;

taylorguitarman is right, you've got a scope problem with month. You declare month in the above for loop, then try to use it ouside the loop. The only statement in the for loop you wrote is
if (month % 3 == 0)
You need to enclose the for block in brackets, and if your cout statement is not part of the for loop, declare month outside the loop. Also, you've declared interest above, no need to declare it again, unless this is a different variable than the one declared at the start.
Code:

```int month; for (month = 1; month <= (length*12); month++) {   if (month % 3 == 0)       interest = percentInterest / 100 * amount / 4;   else       interest = 0; } cout << month << "\t" << interest << endl;```
or
Code:

```for (int month = 1; month <= (length*12); month++) {   if (month % 3 == 0)       interest = percentInterest / 100 * amount / 4;   else       interest = 0;  cout << month << "\t" << interest << endl; }```
Either way is valid, the results depend on what you want to do. I'd guess the first, but check it out for yourself.
As an aside, you could cut out the else statement by setting interest to a default of 0, only changing it in your if statement.
• 03-31-2002
Lesson: If there are no {} following a control loop statement (or an if, else, if/else statement ), then only the next line is "within" the loop. If the body of the loop is several lines/statements long, then you need to use the {}'s. A variable declared inside a loop using static memory goes out of scope after the closing } of the loop.

Use variable declaration and {}'s judiciously, as discussed by the other responders.
• 03-31-2002
salvelinus
Quote:

Originally posted by Tarls

double interest = percentInterest / 100 * amount / 4;

You might have another problem here (besides redeclaring interest). Multiplication and division have equal precedence. What happens here is percentInterest is divided by 100. That result is multiplied by amount. That result is divided by 4 (at least on some compilers - others might do it in unpredictable order).
What you may want is
Code:

`  interest = (percentInterest/100) * (amount/4);`
Maybe you do want it your way, but parantheses will help it compile that way on all systems.
• 03-31-2002
taylorguitarman
That's what I thought you were doing and salvelinus pointed it out. Good going salvelinus.
Scoping is an interesting issue in programming and worth looking into. C++ lets you declare a variable anywhere within the program but the catch is that it is only "alive" in the block of code in which it is declared, meaning the {} (if declared outside of the main and other functions then it has "global" scope and is "alive" any in the whole program). You've pointed out that you've only been programming for 4 weeks so I wouldn't expect you to be an expert yet. Try to get your hands on a copy of "Code Complete." It's a pretty good book that will help with topics related to building programs and maybe it'll prevent some bad habits that programmers develop.

And in the future:
-Always post code (in code tags or as an attachment if it's big)
-Tell what compiler you're using (each has their idiosyncrasies)

That makes it much easier to help.

Oh and your using GCC as your compiler (a very respectable one) and programming in some flavor of UNIX so good for you. :)

Good luck and visit often.
• 03-31-2002
Tarls
Hey, thanks guys, you explained it better than my lecturer ever did! ;) You should all become teachers.

I just compiled it and didn't get any scary error messages, so that's gotta be a good sign. Haven't tested it yet, but that can wait, seeing as it's almost 2am and sleep is a high priority at the moment. :)

Just wanted to say thanks.
Tarls.