Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,331 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,111 people online right now. Registration is fast and FREE... Join Now!




help with functions

 
Reply to this topicStart new topic

help with functions

dwd3773
post 11 Mar, 2008 - 03:11 PM
Post #1


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 101



Thanked 1 times
My Contributions


I have to crate a main that invokes 5 function. 4 of the functions gets input from the ketboard, the 5th function takes the input fro mthe other 4 functions and creates a cars monthly payment. I have everything working but I cant get the input from the other function in the 5th function.
heres my code so far :
CODE

#include <fstream>
#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void getPrice();
void getDownPayment();
void getTradeIn();
void getInterestRate();
void calcMonPayment();

int main()
{
    //Function Calls
    getPrice();
    getDownPayment();
    getTradeIn();
    getInterestRate();
    calcMonPayment();

    cin.get();
}

void getPrice()
{
    float price;

    cout << "Please enter the price of the vehicle: " << endl;
    cin >> price;
    cout << fixed << setprecision(2) <<price << endl;

    cin.get();
}

void getDownPayment()
{
    float downPayment;

    cout << "Please enter the down payment: " << endl;
    cin >> downPayment;
    cout << fixed << setprecision(2) << downPayment << endl;

    cin.get();
}

void getTradeIn()
{
    float tradeIn;

    cout << "Please enter the trade in value of the vehicle: " << endl;
    cin >> tradeIn;
    cout << fixed << setprecision(2) << tradeIn << endl;

    cin.get();
}

void getInterestRate()
{
    float interestRate;

    cout << "Please enter the interest rate: " << endl;
    cin >> interestRate;
    cout << fixed << setprecision(2) << interestRate << endl;

    cin.get();
}

void calcMonPayment()
{
    float price;
    float downPayment;
    float tradeIn;
    float interestRate;
    float loanAmt;
    float annualIntRate;
    float annualIntPercent;
    float monIntRate;
    int noMonths;
    float monPayment;
    
    annualIntRate = interestRate;
    monIntRate = (annualIntRate/12.0);
    annualIntPercent = (annualIntRate*100.0);
    loanAmt = (price - downPayment-tradeIn);
    monPayment = (loanAmt*monIntRate)/(1.0-(1+monIntRate)-noMonths)

    cout << "The monthly payment of the vehicle is: " << monPayment << endl;
    
}


Also I dont know how to get the -noMonths in as an exponent
CODE
(1.0-(1+monIntRate)-noMonths)
.
User is offlineProfile CardPM

Go to the top of the page

KYA
post 11 Mar, 2008 - 03:56 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


Have the final function have 4 parameters that are the returns from the previous four functions.

Or in main() where you call each function have a variable = it like:

CODE


price = getprice(); //this will require you to have your functions return something and cannot be void
turnIn = tradeIn();

//.....

finalFunction(price, turnIn, etc...)



This post has been edited by KYA: 11 Mar, 2008 - 03:57 PM
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 11 Mar, 2008 - 04:14 PM
Post #3


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


You are not sending the variables to your function. I fixed getPrice and calcMonPayment. You need to do the same for the others.
Hope this helps.

CODE

float getPrice();
float calcMonPayment(float price, float downPayment, float tradeIn, float interestRate);

int main()
{
    // main's variables
    float price; // this is the main price which is not the same one in getPrice
    float downPayment;
    float tradeIn;
    float interestRate;
    float monPayment;
    
    //Function Calls
    float price = getPrice();
    getDownPayment();
    getTradeIn();
    getInterestRate();
    monPayment = calcMonPayment( price, downPayment, tradeIn, interestRate);
    cout << "The monthly payment of the vehicle is: " << monPayment << endl;

    cin.get();
}

float getPrice()
{
    float price;

    cout << "Please enter the price of the vehicle: " << endl;
    cin >> price;
    cout << fixed << setprecision(2) <<price << endl;

    cin.get();
    return(price); // you have to return the price
}

float calcMonPayment(float price, float downPayment, float tradeIn, float interestRate)
{
    //float price; // these are passed in, not declared here
    //float downPayment;
    //float tradeIn;
    //float interestRate;
    float loanAmt;
    float annualIntRate;
    float annualIntPercent;
    float monIntRate;
    int noMonths;
    float monPayment;
    
    annualIntRate = interestRate;
    monIntRate = (annualIntRate/12.0);
    annualIntPercent = (annualIntRate*100.0);
    loanAmt = (price - downPayment-tradeIn);
    monPayment = (loanAmt*monIntRate)/(1.0-(1+monIntRate)-noMonths)

    // this line should be in main
    //cout << "The monthly payment of the vehicle is: " << monPayment << endl;
    
    return(monPayment);
    
}



User is offlineProfile CardPM

Go to the top of the page

dwd3773
post 11 Mar, 2008 - 05:33 PM
Post #4


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 101



Thanked 1 times
My Contributions


I have to use woid functions, I believe that void functions cant return a value right? So how can I complete this using void functions. I was thinking about doing like you stated above, but I have to use void functions. Also Does anyone know how to use exponents in c++? I have to put the noMonths as a negaitve exponent in the following code:
CODE

(1.0-(1+monIntRate)-noMonths)
User is offlineProfile CardPM

Go to the top of the page

bad_karma00
post 11 Mar, 2008 - 05:47 PM
Post #5


New D.I.C Head

*
Joined: 10 Mar, 2008
Posts: 33


My Contributions


QUOTE(dwd3773 @ 11 Mar, 2008 - 06:33 PM) *

I have to use woid functions, I believe that void functions cant return a value right? So how can I complete this using void functions. I was thinking about doing like you stated above, but I have to use void functions. Also Does anyone know how to use exponents in c++? I have to put the noMonths as a negaitve exponent in the following code:
CODE

(1.0-(1+monIntRate)-noMonths)





I'm a beginner myself but I believe that is the pow().

CODE

(1.0-(1+monIntRate)*pow(-, noMonths))




err, make that:

CODE

(1.0-(1+monInRate)*pow(noMonths, -))
User is offlineProfile CardPM

Go to the top of the page

bad_karma00
post 11 Mar, 2008 - 05:53 PM
Post #6


New D.I.C Head

*
Joined: 10 Mar, 2008
Posts: 33


My Contributions


also, you these preprocessor commands:
#include<cmath>
#include<stdio.h>
User is offlineProfile CardPM

Go to the top of the page

KYA
post 11 Mar, 2008 - 11:11 PM
Post #7


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


You "HAVE" to use void functions? Does your teacher know that once the method is finished, its data goes out of scope and is unusable once control returns to main()? If you have to use void functions, might as well just do all of the program in main() without any helper functions.
User is offlineProfile CardPM

Go to the top of the page

dwd3773
post 12 Mar, 2008 - 08:53 PM
Post #8


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 101



Thanked 1 times
My Contributions


Ok thanks fo everyones help but I'm still having 1 problem. I need to make the noMonths to start at 24 and increase by 12 until it reaches 60. heres the section of code I have so far

CODE

void calcMonPayment(float price, float tradeIn, float downPayment, float annualIntRate)
{
    float loanAmt;
    float annualIntPercent;
    float monIntRate;
    int noMonths;
    float monPayment;


    noMonths = 24;

    monIntRate = (annualIntRate/12.0);
    annualIntPercent = (annualIntRate*100.0);
    loanAmt = (price - downPayment - tradeIn);
    monPayment = (loanAmt*monIntRate)/(1-pow(1+monIntRate,-noMonths));

    cout << fixed << setprecision(2) << "The monthly payment is " << monPayment << endl;
    cin.get();
    
}


This post has been edited by dwd3773: 13 Mar, 2008 - 01:47 PM
User is offlineProfile CardPM

Go to the top of the page

dwd3773
post 14 Mar, 2008 - 01:11 PM
Post #9


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 101



Thanked 1 times
My Contributions


anyone?
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:53AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month