Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,402 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,114 people online right now. Registration is fast and FREE... Join Now!




Function Definition

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

Function Definition

mitchelltab
16 Sep, 2006 - 10:53 AM
Post #1

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
I want to know if I'm using funcations correctlry to run this program. I also wanted to know if I should use an arry to pass the information from one funcation to the nect

CODE

#include <stdio.h>
#include <math.h>

double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
  double Calculate;
  double Amortization;

main  ()
{
  
  
   int   TermLoan,  // length of the loan
         PaymentNumber,  //number of payments made
          Numberofmonths, //Number of months in the loan
          count,
          n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
         AmountInterest,  //amount paid to interest
         LoanBlance, //amount owed on loan\ count,
         CurrentBlance,
          LoanAmount, //amount of the loan  
         InterestRate, //interest rate on loan
           a,i,
           PaymentAmount;  //amount paid to loan



/****************************************************************      
//Start of the function to collect data
/****************************************************************
double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
  {

   printf ("Please enter in loan amount, Term of Loan in months and Interest Rate.\n");
  

        while (scanf("%lf%d%lf", &LoanAmount, &TermLoan, &InterestRate) != 3
                     || (LoanAmount < 0 || TermLoan < 0 || InterestRate < 0 ))
          {
             while ((c = getchar()) != '\n' && c != EOF);
             printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
         }        
                CurrentBlance = LoanAmount;
               Numberofmonths = TermLoan;
               i = InterestRate/ 100;    
               n = Numberofmonths;
               a = CurrentBlance;
                PaymentNumber = 0;
                count = 0;  
   }  //end of collect data


/********************************************************************
Start of Calculate function
/*************************************************************
int Calculate ( int n, double a, double i);                
   {
          
    PaymentAmount = ((i/12*a)/(1-pow((1+(i/12)),-n)));            
  
            while(PaymentNumber < Numberofmonths)
           {
                    
           PaymentNumber++; //Start number of months for amortization schedule
           count++;
           AmountInterest = a  * (i/12);  
           AmountPrinciple = PaymentAmount - AmountInterest;
           LoanBlance = a - AmountPrinciple;
          
          a = LoanBlance;  
                }//end of while loop
   } //end of Calculate Function


/**********************************************************************
State of print function
/*************************************************************
int Amortization;
{
          printf ("\amortization Schedule\n");
            printf ("_____________________\n");
           printf ("CurrentBlance %.2f\n", a);
           printf ("InterestRate %.2f\n", InterestRate);
          printf ("PaymentNumber %d\n",  PaymentNumber);
          printf ("LoanBlance  %.2f\n", LoanBlance);
          printf ("PaymentAmount %.2f\n", PaymentAmount);
          printf ("AmountPrinciple %.2f\n", AmountPrinciple);
          printf  ("AmountInterest %.2f\n", AmountInterest);
          printf ("_______________________\n\n");

} //end of Amortization print function


printf ("hit enter to end");
getchar ();
getchar ();
return 0;        
}


User is offlineProfile CardPM
+Quote Post

frog
RE: Function Definition
16 Sep, 2006 - 11:13 AM
Post #2

unleashed
Group Icon

Joined: 26 Mar, 2006
Posts: 682


Dream Kudos: 500
My Contributions
the function definations and prototypes here are incorrect. first of all you are defining the functions in another function main () which is not allowed. next the return data types in function prototypes and definations of calculate() and amortization() dont match .in the prototype of calculate() the argument list is incorrect . for the definations of amortization() you are not specifying the arguments whaich are to be passed to it i.e is void in this case. next all three functions dont have a return data type and all the function headers in the definations have a semi colon at there end . that shud be removed
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Function Definition
17 Sep, 2006 - 01:34 AM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
You've tried to attempt the K&R Style of Function prototypes.

Remove the semicolons at places where frog has mentioned them.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Function Definition
18 Sep, 2006 - 09:13 AM
Post #4

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
i need help. I have the program written with no code. But I'm having an issue getting to the functions . Need some help please
CODE


#include <stdio.h>
#include <math.h>

double CollectInput (double LoanAmount,  int TermLoan, double InterestRate, int c);
double Calculate (int n, double a, double i)  ;
double Amortization(double a, double InterestRate, double LoanBlance, double PaymentNumber,
        double AmountPrinciple, double AmountInterest, int InterestRate);
main  ()
{
  
  
   int   TermLoan,  // length of the loan
         PaymentNumber,  //number of payments made
          Numberofmonths, //Number of months in the loan
          count,
          n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
         AmountInterest,  //amount paid to interest
         LoanBlance, //amount owed on loan\ count,
         CurrentBlance,
          LoanAmount, //amount of the loan  
         InterestRate, //interest rate on loan
           a,i,
           PaymentAmount;  //amount paid to loan

printf ("hit enter to end");
getchar ();
getchar ();
return 0;        
}

//****************************************************************      
//Start of the function to collect data
//****************************************************************
double CollectInput (double LoanAmount,  int TermLoan, double InterestRate, int c, double i, double n,
     double CurrentBlance,double Numberofmonths, double a, double PaymentNumber, int count)
     {
                
        while (scanf("%lf%d%lf", &LoanAmount, &TermLoan, &InterestRate) != 3
                     || (LoanAmount < 0 || TermLoan < 0 || InterestRate < 0 ))
          {
             while ((c = getchar()) != '\n' && c != EOF);
             printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
             }


                CurrentBlance = LoanAmount;
               Numberofmonths = TermLoan;
               i = InterestRate/ 100;    
               n = Numberofmonths;
               a = CurrentBlance;
                PaymentNumber = 0;
                count = 0;  
   }  //end of collect data


//********************************************************************
//Start of Calculate function
//*************************************************************
double Calculate (int n, double a, double i, double PaymentAmout, double PaymentNumber, int Numberofmonths,
         double PaymentAmount, int count, double AmountInterest, double AmountPrinciple, double LoanBlance )              
   {
          
         PaymentAmount = ((i/12*a)/(1-pow((1+(i/12)),-n)));            
  
            while(PaymentNumber < Numberofmonths)
             {
                    
             PaymentNumber++; //Start number of months for amortization schedule
             count++;
             AmountInterest = a  * (i/12);  
             AmountPrinciple = PaymentAmount - AmountInterest;
             LoanBlance = a - AmountPrinciple;
             a = LoanBlance;  
                }//end of while loop
   } //end of Calculate Function


//*********************************************************************
//State of print function
//************************************************************
double Amortization(double a, double LoanBlance, double PaymentNumber, double PaymentAmount,
       double AmountPrinciple, double AmountInterest, int InterestRate)
{
          printf ("\amortization Schedule\n");
            printf ("_____________________\n");
           printf ("CurrentBlance %.2f\n", a);
           printf ("InterestRate %.2f\n", InterestRate);
          printf ("PaymentNumber %d\n",  PaymentNumber);
          printf ("LoanBlance  %.2f\n", LoanBlance);
          printf ("PaymentAmount %.2f\n", PaymentAmount);
          printf ("AmountPrinciple %.2f\n", AmountPrinciple);
          printf  ("AmountInterest %.2f\n", AmountInterest);
          printf ("_______________________\n\n");

} //end of Amortization print function





User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Definition
18 Sep, 2006 - 09:18 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Please elaborate on the problem you are encountering.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Function Definition
18 Sep, 2006 - 09:34 AM
Post #6

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(Amadeus @ 18 Sep, 2006 - 10:18 AM) *

Please elaborate on the problem you are encountering.

All that i get is please hit enter. it dose not take me to my funcations. It dose not allow me to eneter in the data and then pass it to the next funcation.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Definition
18 Sep, 2006 - 09:47 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You're not calling any of your functions. You need to invoke them to have them do anything...the general form is:
CODE

myvar = myfunc(myarg1,myarg2);

if the function returns a value, or
CODE

myfunc(myarg1,myarg2);

if the function does not return a value...you can also pass by value (shown) or be reference.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Function Definition
18 Sep, 2006 - 10:09 AM
Post #8

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(Amadeus @ 18 Sep, 2006 - 10:47 AM) *

You're not calling any of your functions. You need to invoke them to have them do anything...the general form is:
CODE

myvar = myfunc(myarg1,myarg2);

if the function returns a value, or
CODE

myfunc(myarg1,myarg2);

if the function does not return a value...you can also pass by value (shown) or be reference.
So I have my functions written worng
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Definition
18 Sep, 2006 - 10:13 AM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Not specifically (although there are a couple of small issues - each of your functions has a return type, but you are not returning any values, for example)...what I'm saying is that you have written several functions, but you are not calling them from your main function. Think of it like a taxi...you know it exists, but you are currently waiting for it to show up and transport you, despite the fact you have not informed the taxi that you are waiting.

You actually need to call the functions themselves.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Function Definition
18 Sep, 2006 - 10:14 AM
Post #10

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(mitchelltab @ 18 Sep, 2006 - 11:09 AM) *

QUOTE(Amadeus @ 18 Sep, 2006 - 10:47 AM) *

You're not calling any of your functions. You need to invoke them to have them do anything...the general form is:
CODE

myvar = myfunc(myarg1,myarg2);

if the function returns a value, or
CODE

myfunc(myarg1,myarg2);

if the function does not return a value...you can also pass by value (shown) or be reference.
So I have my functions written worng

so I should have it as
CODE
myfunc(CollectInput, Calculate, Amortization);
Biut how would I define the myfunc. I know that it is not a double.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Function Definition
18 Sep, 2006 - 10:15 AM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Do you want the function to return a value? If so, what data type do you want it to return?
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Function Definition
18 Sep, 2006 - 10:24 AM
Post #12

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(Amadeus @ 18 Sep, 2006 - 11:15 AM) *

Do you want the function to return a value? If so, what data type do you want it to return?

ok like the frist funcation. I need it to pass to the second funcation to do the calcauation
i = InterestRate/ 100;
n = Numberofmonths;
a = CurrentBlance;
PaymentNumber = 0;
count = 0;

The second funcation i need to pass to the thier to print.
printf ("CurrentBlance %.2f\n", a);
printf ("InterestRate %.2f\n", InterestRate);
printf ("PaymentNumber %d\n", PaymentNumber);
printf ("LoanBlance %.2f\n", LoanBlance);
printf ("PaymentAmount %.2f\n", PaymentAmount);
printf ("AmountPrinciple %.2f\n", AmountPrinciple);
printf ("AmountInterest %.2f\n", AmountInterest);


I think i need a loop or an arry to print out all of the data. Let me know if I'm on the correct path.
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 12/5/08 03:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month