![]() |
|
#1
|
|||
|
|||
Problems with outputI am creating a program to show a person monthly payments they need to make on their loan. I am having problems with getting a value besides 0 to stick to i and balance, because of this when the loop runs the amount of balance, interest and prin stay the same. Hoping someone can point me in the right direction. I am using the GCC compiler and C language.
N is the number of payments person is going to make principal is the amount borrowed Balance is the amount left to be paid a_i_r is the annual interest rate prin is the monthly payment minus the interest interest is the interest obviously Counter is my loop counter acting as the number of payments made so far CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
Re: Problems with outputWould it be too much to ask that you go back and really read the guidelines? I see that you've now graduated to using [c] ... [/c] tags for your code, but you apparently didn't read the part about formatting your code. Here's a piece of advice that should last you forever. Don't use tabs. Use spaces instead of tabs. Figure out how to make whatever editor you're using insert spaces instead of tabs when you type the tab key.
It is painful to have to try to read poorly formatted code when trying to help someone. Here is your code reformatted. See the difference? CPP / C++ / C Code:
MxB |
|
#3
|
|||
|
|||
Re: Problems with outputQuote:
So look at the statement where you calculate i: CPP / C++ / C Code:
Now, it might look OK to us mere humans, but calculations in C go something like this: The (1/12) term is evaluated with integer division, since 1 and 12 are both integers. The value is zero. Now when this is multiplied by the floating point value of the other expression, the integer zero is "promoted" to a double so that the arithmetic can be performed, but its value still zero So: get into the habit of making sure that constants used in floating point expressions are forced to be floating point: CPP / C++ / C Code:
Run the program with this change. (Use 5% as user input for annual interest rate.) Does the value of i still seem to be zero. Well it can't be, can it? I mean, our expression in C is absolutely, positively, unequivocally correct, right? I mean, you printed out the value that the program got from the user (that's a really, really Good Thing to do), so you know that a_i_r is correct. Here's the deal: Since the input to the expression is OK, then either the expression is wrong or something about the print statement is not giving us what we expect to see. So: Here's a tip for debugging: Calculate (manually or with a calculator) what you think the value of the expression should be. Then look at the computer output again. So: what do you expect the value to be? For example suppose the user enters 5 (for 5%). Then, according to my calculator, the variable i will have a value of something like 0.004167, and your format tells the program to show 2 digits after the decimal point, so it would still look like 0.00 (TSo, change the format specifier to "%f" instead or "%.2f" and see what happens) Try it and see if the interest is still stuck at zero. Now, if things still don't look like you expect, then: Run a simple, short problem (1000 at 5% for 3 months, for example). What do you expect the values to be at each step. (Use a calculator to perform the calculations.) Look at the program output that you print at each step. If you don't understand why it's not right, then do the following: 1. Show the input that you gave the program. 2. Show the manually calculated expected output values for the first month. 3. Show the program's output for the first month. 4. Tell us what you don't understand about the difference. Regards, Dave |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 65 | 19-Aug-2009 08:50 |
| Bit stuffing | agnostos | C Programming Language | 17 | 12-Sep-2006 21:10 |
| Format string in output functions | Extro | C Programming Language | 2 | 26-Jul-2005 10:45 |
| editbox output problem | FireFox8173 | MS Visual C++ / MFC Forum | 1 | 01-Apr-2005 15:15 |
| urgent: problem + output filters | jack | Apache Web Server Forum | 0 | 04-Feb-2004 23:32 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The