Jump to content

Help with C program

SpaceAgentX

Hello, I have a loan interest rate amortization table program that I have written up but I have an issue where at the end the Total balance due is less then the monthly payment. I put an if statement in the code to try and account for that but it doesn't work. Can anyone tell me how to get it so I don't keep getting a negative number for the balance? It should be zero. I'll post a link to my code.

 

http://pastebin.com/Afg6D5X1

Link to comment
Share on other sites

Link to post
Share on other sites

replace the if statement with 
 

if(monthlyPayment > totalBalance){     monthlyPayment = totalBalance;}

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

 

replace the if statement with 

 

if(monthlyPayment > totalBalance){     monthlyPayment = totalBalance;}

You just rewrote the if statement using braces? Nothing has actually changed.

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

Unless I'm mistaken, you'd need to break out of the while loop when the conditional is met, then print out the final results (after running the necessary calculations to force the newBalance to be 0) once out of the loop. 

 

The other way to do it is to look at the sequence of equations. I think you'd need to calculate the newBalance AFTER checking the condition. You can move the totalBalance, old = new, and new = blah below the conditional without breaking it. Just a hunch though. 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

So are you saying I could put the if statement outside of the loop to get it to stop at zero? 

Link to comment
Share on other sites

Link to post
Share on other sites

So are you saying I could put the if statement outside of the loop to get it to stop at zero? 

No, the if conditional stays within the loop, but includes the "break;" statement to exit the loop when the condition is met. Right now, the only thing stopping the loop from executing a 6th time is the newBalance > 0 condition, but you want an alternate condition to force the final balance (newBalance as it appears in the final print out of the results) to be 0. 

if(monthlyPayment > totalBalance){monthlyPayment = totalBalance;newBalance = oldBalance - monthlyPayment;}

May work, without the break statement. It replaces the newBalance calculated earlier (which is negative) to a balance which should be 0. Haven't tested it out though. 

 

You need to force it to re-calculate the newBalance before printing it out. 

 

e: The above doesn't work, but it should give you something to look at. 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

From what I have seen you were actually pretty close to the correct answer (with two minor changes).

 

The first is line 34

if(monthlyPayment > totalBalance) monthlyPayment = totalBalance;

The main issue is this if statement doesn't correspond to the actual amount you need to pay, as well as that you put the if statement way to late (after the monthly payment was paid). (So rewriting the if statement and moving it should fix your problem)

The problem being that totalBalance is the amount you are paying down your loan.  You want to compare the current account balance with the amount being paid down.  So the if statement gets changed

if(totalBalance > oldBalance)

Now when totalBalance > oldBalance you have to adjust 2 variables (the amount you are paying this month...and since totalBalance is based off of the amount you are paying you need to change that as well).  So the if statement is now

if(totalBalance > oldBalance)  {    totalBalance = oldBalance; //Ensures that newBalance will be 0    monthlyPayment = totalBalance + interest; //Just simple algebra from your code (since we know what totalBalance should be and interest)}

This if statement should also be placed before the newBalance = .... line

 

Now this will brings me to a few points.  You were right in the way of thinking, but the flow of your program was off.  Remember if you change a variable to match a number then it should be placed before the variable is used...and if you need to print out the numbers that formed, in this case totalBalance, you need to change those variables too.

 

The other point I would like to say, is please think about variables names more carefully in the future.  I found myself a bit confused at why totalBalance was used instead as what I would call "netPayment"....newBalance is a pretty good name (although personally I would name it currentBalance but that is just me)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Hey thanks for your help Wandering. That solution worked for me. I really appreciate the advice as well. 

Link to comment
Share on other sites

Link to post
Share on other sites

Hey I have another question How can I make it so if the user enters in values where the loan can never be paid off it displays an error message? Like would I need another if statement? 

Link to comment
Share on other sites

Link to post
Share on other sites

You can impose a minimum interest rate and monthly payment using conditionals, yes. There are different ways to accomplish it.

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm trying to figure out how to write it if the monthly payment isn't going to be enough to pay back the loan with the interest. It is possible for the user to enter values such that the loan will never be paid. For example, a million dollar loan with a $50 payment never ends. My program should stop as soon as such a case is detected and print an appropriate message

Link to comment
Share on other sites

Link to post
Share on other sites

Well totalBalance is the difference between the amount paying off vs the interest

 

If you are unable to pay off your load it would imply interest is greater than your monthly payment....so if totalBalance <= 0 (after calculation) then you will never be able to pay it back (so do whatever print an error message and then make sure to call break; to exit out of the while loop)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Okay what you said works but I'm confused about the math behind it. How does the total balance become less then or equal to 0 if the loan can never be paid off?

Link to comment
Share on other sites

Link to post
Share on other sites

Okay what you said works but I'm confused about the math behind it. How does the total balance become less then or equal to 0 if the loan can never be paid off?

It is just about realizing the variables

 

totalBalance = monthlyPayment - interest

newBalance = oldBalance - totalBalance

 

For newBalance to decrease totalBalance has to be positive

Also totalBalance is the form of monthlyPayment minus interest

If the monthly payment is less than the interest then you will never pay it off

 

so it is the classical

a = b - c

When c > b

a is negative

when b > c

a is positive

when b and c are both positive

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×