![]() |
|
#1
|
|||
|
|||
Loan Repayment programI've not been dealing with C++ to long but I've been given a task and need some help to say the least. I'm trying to design a program dealing with student loan repayment and mainly how long its going to take me to pay £12000 back. I've tried to write in the code for the 3.1% apr, the time when i have to start paying back (salery threshold-£15000 a year) and paying the money back at £20 per week.
What do you think so far? CPP / C++ / C Code:
I'm hazy on the last function to handle the input. Am I on the right tracks at least. Thanks John Last edited by JdS : 23-Jan-2004 at 15:38.
Reason: used bbcode for c code
|
|
#2
|
||||
|
||||
|
Hi John. Welcome to GIDforums. I am pretty new here myself, but I have found this to be a great forum. One of the coolest things is that if you can post your code and it will highlight it. If you start your code section with a C inside [] and end it with a /C inside [], it makes it much easier to read. You may want to edit your original post and do that.
On to a few items in your program that I saw immediately: Forgive me if I am presumptious, but I am pretty sure that this doesn't compile. C is all about functions. Everything that you do in C is inside of a function. The program execution starts by calling the function called main and then you can call the other functions from there. Your main function simply says CPP / C++ / C Code:
Your whole program defines a variable called value as a double and initilizes it to 0. Your main function should be more like this (note that capitalization or lack there of is important CPP / C++ / C Code:
This breaks your tasks down a little bit. I do like your use of the #define declaritive. It makes your program more readable. As for inputs, if you are using straight C you probably want to use a scanf for your inputs. It could be something like: CPP / C++ / C Code:
Work with that and then post what you come up with. Remember the syntax codes :-) |
|
#3
|
||||
|
||||
|
John, I was just reading your post again. Did you want to read your input from the console or from a file? The way I gave you was to read it from the console.
|
|
#4
|
|||
|
|||
|
Hi,
Reading the input from the console, I had some help earlier with the input so I’m not 100% sure how its meant to be written! /*Loan Repayment*/ #define apr .0031 #include <stdio.h> Int Main () { static double value = 0.0; int fill salary(file*fp,double*months, int maxmonths) P(principal) = P* (1 + rate/100); double calcinterest(double P, double ratepercent); double calcrepayment(double P, double salery); while(loan>0); salery=2500; loan-=calcrepayment(80); loan+=calcinterest(.0031); nmonths++; return 0 } How would I fit in the printf/scanf functions? Should they be inputted to the while loop? Excuse my ignorance with this John |
|
#5
|
||||
|
||||
|
Hey John, this is looking more like a C program now
First off though, I would like to direct you to this link that shows how to post code in this forum. It really helps with readability and this post explains a lot better than I can. Now on with the program. This line here CPP / C++ / C Code:
My advice is to tackle this thing one element at a time. So I would comment out everything that you have in your main loop for now and just concentrate on getting the input in. There are numerous inconsistencies here anyway, but we can work on these later. For now these should be the only lines that you don't comment out. These are the only lines that should be active (again pay attention to the capitalization: CPP / C++ / C Code:
Now the first thing that you want to do is figure out what your input is going to be and declare the variables for them. I would suggest that you let the interest rate to be a variable as well. This will make the program more robust at run-time. For each variable the you need, put in a declaration in the form of: CPP / C++ / C Code:
Now for each variable entry you have put in code like: CPP / C++ / C Code:
After you get all of those done, print out a summary of the input CPP / C++ / C Code:
Once you do this, I would really recommend that you compile the program and run it. It really helps to isolate errors, if you can try to take it one step at a time. If this works, when you run the program, it should ask you for each of your parameters that you can enter and then print a summary of your input. Try this and then post your code, letting me know if it works or not. |
|
#6
|
||||
|
||||
Please post the final programwhen you are done with everything. I'd be very interest to use it as a reference for any further loan case study.
Thanks, Kimber |
|
#7
|
|||
|
|||
re:loan repaymentWell i've done away with the code that i can't see to be working, started from the bottom and put in this for the inputs. My compiler isn't happy with the repeated use of double rate = 0.0; so i couldn't get an executable file and its left me wondering where I go from here.
Should I continue using the "%8.2f\n" for the salery threshold code and the repayment code? Also, how do I define 'monthly' repayments to the program? CPP / C++ / C Code:
Thanks John |
|
#8
|
||||
|
||||
|
Okay, that looks good (and you posted with the c code script). Thank you!
Anyway, the problem is that you need to define a different variable for each input you want. The compiler is complaining because you keep redefining rate. I quickly modified your code and posted it below. CPP / C++ / C Code:
Notice at the top of the function is where I declare all of the variables. You can declare them anywhere, as long as they are defined prior to using them, but for readability purposes, I like to declare all of my variables at the beginning of the function. Also, I made it ask for all of the inputs first and then echoed everything to the screen at once. See if this works or not. One thing on the scanf function that I really struggled with when I was learning C, was the necesity to pass it an address of a variable and not just the variable. Parameters to functions in C are local to that function. In other words if I pass a variable to a function, a new copy is made for that function only. That means any changes made to that variable do not get passed onto the calling function. But when you pass the address of the variable, the changes can be done to the variable that the address points to. Does that make any sense? Maybe someone with better language skills could post a clarification. |
|
#9
|
||||
|
||||
You can do it.Don't give up my friend.
Best of luck, Kimber |
|
#10
|
|||
|
|||
|
CPP / C++ / C Code:
Using this code, what would I need to add for the program to tell me how many months of repayment I'll need. This would need to be floating point |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A question about the Amazon Affiliate Program | JdS | Advertising & Affiliates Forum | 5 | 13-Jan-2004 18:00 |
| Help with calendar program | mike3340 | C++ Forum | 0 | 18-Nov-2003 21:25 |
| error during program | rjd72285 | C++ Forum | 0 | 11-Nov-2003 19:49 |
| How to write simple program like ........? | laputa9000 | C++ Forum | 3 | 29-Oct-2003 13:09 |
| one program access another? | dgoulston | C++ Forum | 1 | 07-Oct-2003 12:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The