# loopin crazy

This is a discussion on loopin crazy within the C Programming forums, part of the General Programming Boards category; I am tryin to get this code to decrement. I keep gettin an infinte loop. The program is supposed to ...

1. ## loopin crazy

I am tryin to get this code to decrement. I keep gettin an infinte loop. The program is supposed to show the value of your money each year and how it depreciats. Here is the code

float sld (float a)
{
float e,f,g,h,i,j;

scanf("%f",&e);
printf("Please enter the percentage of depreciation:%\n");
scanf("%f",&f);
printf("Please enter the Number of Years:\n");
scanf("%f",&g);

for(g>0,++g)
{
while(g=1)
{
h=e/f;
scanf("%3.2f",&h);
i=f-h;
printf("YEAR DEPRECIATION CURRENT VALUE\n");
printf("%4.2f %4.2f %4.2f\n",g,h,i);
}
g+=1
j=i-h;
printf("%4.2f %4.2f %4.2f\n",g,h,j);
j=i;
}
}

2. > I am tryin to get this code to decrement. I keep gettin an infinte
> loop.

Ok, so why are you incrementing?

> for(g>0,++g)

Also, here is your infinite loop:

> while(g=1)
> {
> h=e/f;
> scanf("%3.2f",&h);
> i=f-h;
> printf("YEAR DEPRECIATION CURRENT VALUE\n");
> printf("%4.2f %4.2f %4.2f\n",g,h,i);
> }

The value of 'g' never changes in this loop. Fix it.

Quzah.

3. Code:
```for(g>0,++g)
{
while(g=1) //this will always be true g will accept 1 that is true
while( 1 == g ) //if you leave out the second = sign the complier will complain about 1 not being an l-value
{
h=e/f;
scanf("%3.2f",&h);
i=f-h;
printf("YEAR DEPRECIATION CURRENT VALUE\n");
printf("%4.2f %4.2f %4.2f\n",g,h,i);
}```

> while(g=1)

You are assigning a value to g. G is given the value of 1 which is "true", and other than that, the loop itself never changes 'g' to anything else.

Quzah.

5. Ok, first, I'm going to rename all your variables because single letter names are HORRIBLE style. *Only* use one letter names for index or loop variables (usually, use i & j).

Also, what does your parameter to the function (a) do? It's completely unused.

This is, I believe, a corrected version of your code; the following code does what I think you're trying to do.

Code:
```

float sld (float a){
float balance,depPercent,deprecation;
int yearCount, currentYear; // yearcount must be an integer, so make it one.
scanf("%f",&balance);
printf("Please enter the percentage of depreciation:%\n");
scanf("%f",&depPercent);
printf("Please enter the Number of Years:\n");
scanf("%d",&yearCount);
deprecation = 0;
for (currentYear = 0; currentYear <= yearCount; currentYear ++){
//do the printing FIRST, because in year 0, we want to see 0 deprecation.
printf("YEAR DEPRECIATION CURRENT VALUE\n");
printf("%d %4.2f %4.2f\n",currentYear,deprecation,balance);
deprecation = balance * depPercent; // you should multiply, not divide?
//scanf("%3.2f",&h); (what in the WORLD are you trying to do here??
balance = balance - deprecation;
}
return 0; // what value do you want to return??
}```
I made a few assumptions about how you want to handle the number of years. I assumed that you wanted to input the number of years, and that you wanted how much it had deprecated in that period of time. Year "0" is the initial value before any deprecation.

I ran the above code, and it looked like this:
Code:
```Please enter the account balance:
1000
Please enter the percentage of depreciation:%
0.10
Please enter the Number of Years:
5
YEAR DEPRECIATION CURRENT VALUE
0 0.00 1000.00
YEAR DEPRECIATION CURRENT VALUE
1 100.00 900.00
YEAR DEPRECIATION CURRENT VALUE
2 90.00 810.00
YEAR DEPRECIATION CURRENT VALUE
3 81.00 729.00
YEAR DEPRECIATION CURRENT VALUE
4 72.90 656.10
YEAR DEPRECIATION CURRENT VALUE
5 65.61 590.49```
I do believe this is the correct output -- here, you have 10% deprecation, with an initial value of \$1000.

BTW, did you want people to input, say, a 10% deprecation as 10, or as .10? Right now, a 10% deprecation should be entered as .1, but you can change this by adding this line after you read the deprecation value in:

depPercent = depPercent / 100;

6. Ok maybe if we add some spaceing it might be clear
Code:
```float sld (float a)
{
float e,f,g,h,i,j;

scanf("%f",&e);
printf("Please enter the percentage of depreciation:%\n");
scanf("%f",&f);
printf("Please enter the Number of Years:\n");
scanf("%f",&g);

for(g>0,++g)
{
while(g=1)
{
h=e/f;
scanf("%3.2f",&h);
i=f-h;
printf("YEAR DEPRECIATION CURRENT VALUE\n");
printf("%4.2f %4.2f %4.2f\n",g,h,i);
}
g+=1
j=i-h;
printf("%4.2f %4.2f %4.2f\n",g,h,j);
j=i;
}
}```
Ok I found a number of problems with this code.

First is your for loop. You are using a comma (,) instead of a semi-colon (.

Second is your while loop. There are two problems. First part is the g=1. This is setting the value of g to 1. Try g==1. Second part is the fact that you are never changing the value of g. So if you start the while loop with g equaling 1 then the loop will never stop.

Fix these problems and things should start working better.

7. ## that is not what I am after but close

float sld (float a){
float balance,depPercent,deprecation;
int yearCount, currentYear; // yearcount must be an integer, so make it one.
scanf("%f",&balance);
printf("Please enter the percentage of depreciation:%\n");
scanf("%f",&depPercent);
printf("Please enter the Number of Years:\n");
scanf("%d",&yearCount);
deprecation = 1;
for (currentYear = 0; currentYear < yearCount; currentYear ++){
//do the printing FIRST, because in year 0, we want to see 0 deprecation.
printf("YEAR DEPRECIATION CURRENT VALUE\n");
printf("%d %4.2f %4.2f\n",currentYear,deprecation,balance);
deprecation = balance / depPercent; // you should multiply, not divide?
// I need to find the amount to subtract so I am dividing by the account
//balance to get the amount that this depreciates over however many years.
//This thing below was supposed to be the amount. All I need is for one
//amount to subtract from the balance every year from year 1 to whatever
//year the user inputs. This is called a Straight lin depreciation.
//scanf("%3.2f",&h); (what in the WORLD are you trying to do here??
balance = balance - deprecation;
}
}

the output should be
after you enter \$1000, 10%,5yrs
1st year 1000-100=900
2nd year 900-100=800
3rd year 800-100=700
etc

8. Hmm, you still need to multiply, not divide, because if you have a percentage, which is really a fraction, the division is implied.

I.e. 50% of 10,000 is 10,000 * 0.5 not 10,000 / 0.5

This is the modified version of code, after taking your desired output and behavior into consideration:

Code:
```void sld (){
float balance,depPercent,deprecation;
int yearCount, currentYear; // yearcount must be an integer, so make it one.
scanf("%f",&balance);
printf("Please enter the percentage of depreciation:%\n");
scanf("%f",&depPercent);
depPercent = depPercent / 100; // use this to convert 10 into 0.10
printf("Please enter the Number of Years:\n");
scanf("%d",&yearCount);
deprecation = balance * depPercent;
printf("YEAR DEPRECIATION CURRENT VALUE\n");
// If you want to print year 0 (initial values) leave the next line in, else delete it.
printf("0 0.00 %4.2f\n",balance); // year 0 statistics
for (currentYear = 0; currentYear < yearCount; currentYear ++){
balance = balance - deprecation;
printf("%d %4.2f %4.2f\n",currentYear + 1,deprecation,balance);
}
}```
I just tried it, and got this output for the program:
Code:
```Please enter the account balance:
1000
Please enter the percentage of depreciation:%
10
Please enter the Number of Years:
5
YEAR DEPRECIATION CURRENT VALUE
0 0.00 1000.00
1 100.00 900.00
2 100.00 800.00
3 100.00 700.00
4 100.00 600.00
5 100.00 500.00```
Now, the input and output may not be formatted the way you want, but numerically, the output is correct as I understand it.

I just changed this to reflect your desire for 10% to be input as 10.