8 Replies - 1034 Views - Last Post: 12 October 2012 - 09:43 PM Rate Topic: -----

#1 Dus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 12

How to to write two functions to compute investment value

Posted 12 October 2012 - 09:09 AM

Hi

I couldn't understand how to do a program using two functions!! However, I must use at least two functions in this project

[Write a function that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the following formula:

Accumulative Value =
investmentAmount * (1 + MonthlyInterestRate) NumberofYears*12]

 
#include <iostream>
#include<math.h>
#include<iomanip>
using namespace std;

int InvesAmount, years, i, futureValue;
float annualRate,monthlyRate;

float futureV()
{
    for (i=1;i<=years;i++)
        futureValue=InvesAmount*pow((1+monthlyRate),(i*12));
    cout<<"Years"<<setw(16)<<"Future Value\n";
    cout<<i      <<setw(16)<<futureValue;
}

int main()
{
    cout<<"Enter your investment amount";
    cin>>InvesAmount;
    cout<< "How much is the interest rate?";
    cin>>annualRate;
    monthlyRate=(annualRate/100)/12;
    cout<<"For how long you want to invest?";
    cin>>years;
    cout<<"Your future inestment values are  ";
    futureV;
}


Errors:
Control reaches end of non-function
Expression result unused
No Debug Session

[To repeat, your program should at least consist of two functions, the main function and a function that calculates the balance]

Please I want to understand how I do two functions, I didn't find good tutorials in this subject

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to to write two functions to compute investment value

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 09:15 AM

This should stop the immediate error:
    cout<<"Your future inestment values are  " <<  futureV();


but you're not passing anything to the function, so it's still not going to do what you want.

Read up on functions.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 09:24 AM

There are also my function tutorials, linked in my signature.
Was This Post Helpful? 1
  • +
  • -

#4 Dus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 12

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 11:46 AM

View PostCTphpnwb, on 12 October 2012 - 09:15 AM, said:

This should stop the immediate error:
    cout<<"Your future inestment values are  " <<  futureV();


but you're not passing anything to the function, so it's still not going to do what you want.

Read up on functions.

Thank u for ur help and I've read the article, but couldn't get how it works in sophisticated functions.
Could you please explain what do you mean that I am not passing anything?
when I write the function name( futureV() ), is it suppose to call the function?

View PostJackOfAllTrades, on 12 October 2012 - 09:24 AM, said:

There are also my function tutorials, linked in my signature.

Thank you I'll check them
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 11:59 AM

Basically a function is written like this.

return_type functionName(parameters if any)
{
//code
//return_value if return_type is not 'void'
}



So according to this,your futureV() function is supposed to return a floating point value so it would do you a lot good if you could use a 'return' statement instead of a cout so that your function works correctly.

regards,
Raghav

This post has been edited by raghav.naganathan: 12 October 2012 - 12:00 PM

Was This Post Helpful? 1
  • +
  • -

#6 Dus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 12

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 12:06 PM

View Postraghav.naganathan, on 12 October 2012 - 11:59 AM, said:

Basically a function is written like this.

return_type functionName(parameters if any)
{
//code
//return_value if return_type is not 'void'
}



So according to this,your futureV() function is supposed to return a floating point value so it would do you a lot good if you could use a 'return' statement instead of a cout so that your function works correctly.

regards,
Raghav


Could you please explain how i use return instead of cout?

Thank u

the code
#include <iostream>
#include<math.h>
#include<iomanip>
using namespace std;

int InvesAmount, years, i, futureValue;
float annualRate,monthlyRate;

float futureV()
{
    for (i=1;i<=years;i++)
    { futureValue=InvesAmount*pow((1+monthlyRate),(i*12));
        cout<<i      <<setw(16)<<futureValue;}
}

int main()
{
    cout<<"How much is your investment amount?";
    cin>>InvesAmount;
    cout<< "How much is the annual interest rate?";
    cin>>annualRate;
    monthlyRate=(annualRate/100)/12;
    cout<<"How many years you want to invest?";
    cin>>years;
    cout<<"Your future inestment values are\n"<<"Years"<<setw(40)<<"    Future Value\n"<<futureV();
    
} 

Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 12:17 PM

View PostDus, on 13 October 2012 - 12:36 AM, said:

View Postraghav.naganathan, on 12 October 2012 - 11:59 AM, said:

Basically a function is written like this.

return_type functionName(parameters if any)
{
//code
//return_value if return_type is not 'void'
}



So according to this,your futureV() function is supposed to return a floating point value so it would do you a lot good if you could use a 'return' statement instead of a cout so that your function works correctly.

regards,
Raghav


Could you please explain how i use return instead of cout?

Thank u


Let me illustrate that to you with this example

float sum()
{
float num1=1.0,num2=2.0,sum_float;
sum_float=num1+num2;
return sum_float;
}



Try along the same lines. You can use cout as well but make sure you provide a return statement after you do the cout.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 12:24 PM

@raghav.naganathan
Please find an indentation style you like and use it consistently. Especially when providing answers using your own code. In my opinion proper indentation is a mandatory item for proper program structure.
float sum()
{
   float num1 = 1.0, num2 = 2.0, sum_float;
   sum_float = num1 + num2;
   return sum_float;
}



Jim

This post has been edited by jimblumberg: 12 October 2012 - 12:26 PM

Was This Post Helpful? 2
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 09:43 PM

View Postjimblumberg, on 13 October 2012 - 12:54 AM, said:

@raghav.naganathan
Please find an indentation style you like and use it consistently. Especially when providing answers using your own code. In my opinion proper indentation is a mandatory item for proper program structure.
float sum()
{
   float num1 = 1.0, num2 = 2.0, sum_float;
   sum_float = num1 + num2;
   return sum_float;
}



Jim


Sure Jim, thank you for that suggestion. Will keep that in mind when I am posting next time :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1