GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jan-2004, 08:48
Tripod Tripod is offline
New Member
 
Join Date: Jan 2004
Posts: 8
Tripod is on a distinguished road

Loan Repayment program


I'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:
/*Loan Repayment*/
#define apr .0031
#include <stdio.h>
Main ()
{
static double value = 0.0;
}

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++;
}
{
int fill salary(file*fp,double*months, int maxmonths)
}
return 0
-----------

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 14:38. Reason: used bbcode for c code
  #2  
Old 23-Jan-2004, 09:22
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
Main ()
{
static double value = 0.0;
}

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:
int main()
{
    //Get the input...
    //Process the input...
    //Show the results
    return 0;
}

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:
    double principal;

    printf("Enter Principal Value: ");
    scanf("%f",&principal);

Work with that and then post what you come up with. Remember the syntax codes :-)
  #3  
Old 23-Jan-2004, 09:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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  
Old 24-Jan-2004, 09:24
Tripod Tripod is offline
New Member
 
Join Date: Jan 2004
Posts: 8
Tripod is on a distinguished road
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  
Old 24-Jan-2004, 11:01
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
int fill salary(file*fp,double*months, int maxmonths)
is a function header and appears that at some point you may have intended to get the input from a file. Let's work from the console stand point first though.

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:
#include <stdio.h>

int main()
{
     return 0;
}

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:
double rate = 0.0;
You will probably need one for salary, principal, etc.

Now for each variable entry you have put in code like:
CPP / C++ / C Code:
printf("\nEnter the interest rate: ");
scanf("%f",&rate);

After you get all of those done, print out a summary of the input
CPP / C++ / C Code:
printf("The interest rate to be used is: %6.4f\n",rate);
If you are not familiar with the %f format flag, the 6.4 tells printf to what precision to print the double variable. In this example it would be 00.0000. For your other entries you will want to change this to something more appropriate linke 8.2.

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  
Old 24-Jan-2004, 11:23
mak90thug's Avatar
mak90thug mak90thug is offline
New Member
 
Join Date: Jan 2004
Posts: 17
mak90thug is on a distinguished road

Please post the final program


when 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  
Old 26-Jan-2004, 10:36
Tripod Tripod is offline
New Member
 
Join Date: Jan 2004
Posts: 8
Tripod is on a distinguished road

re:loan repayment


Well 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:
  /*Loan Repayment*/
#include <stdio.h>
int main()

{

double rate = 0.0;
printf("\nEnter the interest rate: ");
 scanf("%f",&rate);
printf("The interest rate to be used is: %8.2f\n",rate);
double rate = 0.0;
printf("Enter the salary threshold: ");
scanf("%f",&rate);
printf("The salary threshold to be used is: %8.2f\n",rate);
double rate = 0.0;
printf("Enter the amount payable per month: ");
scanf("%f",&rate);
printf("The amount to be payed back each monthis: %8.2f\n",rate);

return 0;
}

Thanks
John
  #8  
Old 26-Jan-2004, 11:04
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
/*Loan Repayment*/
#include <stdio.h>
int main()

{

double rate = 0.0;
double salary = 0.0;
double payment = 0.0;

printf("\nEnter the interest rate: ");
scanf("%f",&rate);

printf("Enter the salary threshold: ");
scanf("%f",&salary);

printf("Enter the amount payable per month: ");
scanf("%f",&payment);

printf("The interest rate to be used is: %6.4f\n",rate);
printf("The salary threshold to be used is: %8.2f\n",salary);
printf("The amount to be payed back each monthis: %8.2f\n",payment);

return 0;
}

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  
Old 26-Jan-2004, 17:10
mak90thug's Avatar
mak90thug mak90thug is offline
New Member
 
Join Date: Jan 2004
Posts: 17
mak90thug is on a distinguished road

You can do it.


Don't give up my friend.

Best of luck,

Kimber
  #10  
Old 01-Mar-2004, 04:19
Tripod Tripod is offline
New Member
 
Join Date: Jan 2004
Posts: 8
Tripod is on a distinguished road
CPP / C++ / C Code:
/*Loan Repayment*/
#include <stdio.h>
int main()

{



double debt = 0.0
double rate = 0.0;
double salary = 0.0;
double payment = 0.0;

printf("Enter total debt: ");
scanf("%f",&debt);

printf("Enter the interest rate: ");
scanf("%f",&rate);

printf("Enter the salary threshold: ");
scanf("%f",&salary);

printf("Enter the amount payable per month: ");
scanf("%f",&payment);

printf("Your Debt is: %8.2f\n",debt);
printf("The interest rate to be used is: %6.4f\n",rate);
printf("The salary threshold to be used is: %8.2f\n",salary);
printf("The amount to be payed back each monthis: %8.2f\n",payment);

if (salary >= 12000)
{
   */ Calc number of months/*
}


return 0;
}

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 GIDBlogCompress Your Web Site by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about the Amazon Affiliate Program JdS Advertising & Affiliates Forum 5 13-Jan-2004 17:00
Help with calendar program mike3340 C++ Forum 0 18-Nov-2003 20:25
error during program rjd72285 C++ Forum 0 11-Nov-2003 18:49
How to write simple program like ........? laputa9000 C++ Forum 3 29-Oct-2003 12:09
one program access another? dgoulston C++ Forum 1 07-Oct-2003 11:26

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:34.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.