Struct Help!

Need help passing struct variable into/outof function

Page 1 of 1

2 Replies - 785 Views - Last Post: 01 August 2009 - 04:59 PM Rate Topic: -----

#1 crunyan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-June 09

Struct Help!

Post icon  Posted 01 August 2009 - 11:15 AM

Ok, I'm a first timer here so please bear with me. Yes, this is homework. The code does work ..except..when I input the trade in value of 2000 and the down payment of 4000, my end result report has these values swapped.. I thought it would be something simple/stupid that I did ...but I cannot find it..
Anyone...please help!

Thanks
***************************************************************************************

// Honest Dave's Used Cars Monthly Car Loan Payment Calculator.
// Calculates 4 monthly car payment options using functional decomposition;
// independent modules; small "driver" main module with a series of calls;
// no global variables; struct data structure; for loops; do/while loops;
// return by value functions; and one-dimensional arrays.
//	

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

//Functions
float getPrice();					// Get Price of Vehicle 	
float getInterestRate();			// Get the Annual Interest Rate as a Fraction
float getDownPayment(float, float);	// Get the Down Payment in Dollars and Cents
float getTradeIn(float);			// Get the Trade-In in Dollars and Cents
void calcMonPayment(float&, float&, float&, int&, float&, double[], float&, double&);	// Calculate Monthly Payment
void displayLoanForm(float&, float&, float&, float&, float&);	// Display Loan Form
void displayLoanSchedule(int&, double[]);	// Display the Loan Data and Calculation Results

// Main Function****
int main()
{
	struct loanVar
{
	// declare member list
	float price, tradeIn, downPayment, annualIntRate;			
};

	// declare variables
	double monPayment[4], monIntRate; // Monthly interest rate & Monthly payment as a one-dimensional array
	float annualIntPercent; // Annual interest percentage
	float loanAmt; // Loan Amount
	int noMonths; // Where no Months - 24, 36, 48, 60
	loanVar inputData;

	inputData.price = getPrice();	// Get price of vehicle (declare input variable for function - Page 529)
	inputData.tradeIn = getTradeIn(inputData.price);	// Get trade-in in dollars and cents (declare input variable for function)
	inputData.downPayment = getDownPayment(inputData.price, inputData.tradeIn);	// Get down payment in dollars and cents (declare input variable for function)
	inputData.annualIntRate = getInterestRate();	// Get the annual interest reate as a fraction (declare input variable for function)

	float rateConvert = 100.00;	// For the annualIntPercent calc due to double to float warning
	annualIntPercent = inputData.annualIntRate * rateConvert;	// Annual Interest Rate

	calcMonPayment(inputData.price,inputData.downPayment,inputData.tradeIn,noMonths,loanAmt,monPayment,inputData.annualIntRate,monIntRate);	// Calculate payments
	cout << "Trade in before displaying it " << inputData.tradeIn << endl;
	displayLoanForm(inputData.price, inputData.tradeIn, inputData.downPayment, loanAmt, annualIntPercent);	// Display Loan Form
	cout << "Trade in  after displaying it" << inputData.tradeIn << endl;
	displayLoanSchedule(noMonths, monPayment);	// Display Loan Schedule
	   
	return 0;

} // End of Main Function****

//***************************Get price of vehicle with do/while loop and return by value********************************
// Greater than $50.00 and less than $50,000.00
float getPrice()
{
	float price;
	do
	{
		cout << "Please input the price of the vehicle purchasing. Do NOT use COMMAS or DOLLAR SIGNS please.:  ";
		cin >> price;
		if (price > 50000.00 || price < 50.0)
			cout << "The price must be between $50.00 and $50,000.00. Please re-input price.: " << endl;
	}while (price > 50000.00 || price < 50.0);

	cout << price << endl;
	return price;
}

//**************************Get the trade in value with do/while loop and return by value********************************
// Greater than or equal to zero and less than the price
float getTradeIn(float price)
{
	float tradeIn;
	do
	{
		cout << "Enter Trade-In value of vehicle:  ";
		cin >> tradeIn;
		if (tradeIn < 0 || tradeIn >= price)
		{
			cout << "Trade-in needs to be greater than or equal to ZERO and less than the purchase price of the new vehicle. Re-input trade-in value: " << endl;
		}
	}while (tradeIn <0 || tradeIn >= price);
	
	cout << tradeIn << endl;
	return tradeIn;
	cout << tradeIn << endl;
}

//***********************Get the down payment with do/while loop and return by value**************************************
// Greater than or equal to zero and less than price minus the trade-in
float getDownPayment(/* in */ float price, /* in */ float tradeIn)
{
	float downPayment;
	do
	{
		cout << "Enter Down Payment on vehicle:  ";
		cin >> downPayment;
		if (downPayment < 0 || downPayment > (price - tradeIn))
		{
			cout << "The Down Payment needs to be ZERO or higher but not more than the purchase price after trade-in. Re-input Down Payment: " << endl;
		}
	}while (downPayment < 0 || downPayment > (price - tradeIn));

	cout << downPayment << endl;
	cout << "Trade in  at getting down payment" << tradeIn << endl;
	return downPayment;
}

//***********************Get the interest rate with do/while loop and return by value************************************
// Greater than zero and less than .50
float getInterestRate()
{
	float annualIntRate;
	do
	{
		cout << "Enter the Annual Interest Rate as a decimal fraction, IE: Use .06 instead of 6%: ";
		cin >> annualIntRate;
		if (annualIntRate < 0 || annualIntRate >= 0.50)
			cout << "The Annual Interest Rate must be greater than or equal to ZERO and less than .50. Re-input Annual Interest Rate.: ";
	} while (annualIntRate < 0 || annualIntRate >= 0.50);

	return annualIntRate;
		
}

//*******************Calculate 4 Monthly Payments and store in a one-dimensional array.***********************************
// Monthly payments @ 24, 36, 48, & 60 months
void calcMonPayment(float& price, float& downPayment, float& tradeIn, int& noMonths, float& loanAmt, double monPayment[], float& annualIntRate, double& monIntRate)
{
	noMonths = 24;
	int count;
	float rateMon = 12.0;	// Because it compiles with warnings if I do not.
	for (count = 0; count <= 3; count++)
	{
		monIntRate = annualIntRate / rateMon;
		loanAmt = price - downPayment - tradeIn;
		monPayment[count] = (loanAmt * monIntRate) / (1.0 - pow((monIntRate + 1), -noMonths));
		noMonths = noMonths + 12;
	}
	
	return;
}

//**********************Loan Form. Void Function*************************************************************************
// Use a for loop to display the four monthly payments.  Must be a void function
void displayLoanForm(float& price, float& downPayment, float& tradeIn, float& loanAmt, float& annualIntPercent)
{
cout << "\n\n" << endl;
cout << setw(50) << "Honest Dave's Used Cars. \n\n";

	cout << "Vehicle Price" << fixed << setprecision(2) << setw(20) << price << endl;
	cout << "Trade In Value" << fixed << setprecision(2) << setw(19) << tradeIn << endl;
	cout << "Down Payment" << fixed << setprecision(2) << setw(21) << downPayment << endl;
	cout << setw (20) << " _________________________" << endl;
	cout << "Loan Amount" << fixed << setprecision(2) << setw (22) << loanAmt << endl << endl;

	cout<< "Annual Interest Rate" << fixed << setprecision(2) << setw(20) << annualIntPercent << "%" << endl << endl;
	cout << setw (20) << "Monthly Payment Options \n\n";
	return;
}

//**********************Loan Schedule. Pass variables using a struct and 4 payments as an array. Void Function*************
// Use a for loop to display the four monthly payments.  Must be a void function
void displayLoanSchedule(int& noMonths, double monPayment[])
{
	noMonths = 24;
	int count;
	for (count = 0; count <= 3; count++)
	{
		cout << setw (30) << noMonths << " Months" << fixed << setprecision(2) << setw(20) << monPayment[count] << endl;
		noMonths = noMonths + 12;
	}
	cin.get();
	cin.get();
	return;
}


Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Struct Help!

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Struct Help!

Posted 01 August 2009 - 11:26 AM

View Postcrunyan, on 1 Aug, 2009 - 05:15 PM, said:

Ok, I'm a first timer here so please bear with me. Yes, this is homework. The code does work ..except..when I input the trade in value of 2000 and the down payment of 4000, my end result report has these values swapped.. I thought it would be something simple/stupid that I did ...but I cannot find it..
Anyone...please help!

Thanks
***************************************************************************************
 
// Honest Dave's Used Cars Monthly Car Loan Payment Calculator.
// Calculates 4 monthly car payment options using functional decomposition;
// independent modules; small "driver" main module with a series of calls;
// no global variables; struct data structure; for loops; do/while loops;
// return by value functions; and one-dimensional arrays.
//	

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

//Functions
float getPrice();					// Get Price of Vehicle 	
float getInterestRate();			// Get the Annual Interest Rate as a Fraction
float getDownPayment(float, float);	// Get the Down Payment in Dollars and Cents
float getTradeIn(float);			// Get the Trade-In in Dollars and Cents
void calcMonPayment(float&, float&, float&, int&, float&, double[], float&, double&);	// Calculate Monthly Payment
void displayLoanForm(float&, float&, float&, float&, float&);	// Display Loan Form
void displayLoanSchedule(int&, double[]);	// Display the Loan Data and Calculation Results

// Main Function****
int main()
{
	struct loanVar
{
	// declare member list
	float price, tradeIn, downPayment, annualIntRate;			
};

	// declare variables
	double monPayment[4], monIntRate; // Monthly interest rate & Monthly payment as a one-dimensional array
	float annualIntPercent; // Annual interest percentage
	float loanAmt; // Loan Amount
	int noMonths; // Where no Months - 24, 36, 48, 60
	loanVar inputData;

	inputData.price = getPrice();	// Get price of vehicle (declare input variable for function - Page 529)
	inputData.tradeIn = getTradeIn(inputData.price);	// Get trade-in in dollars and cents (declare input variable for function)
	inputData.downPayment = getDownPayment(inputData.price, inputData.tradeIn);	// Get down payment in dollars and cents (declare input variable for function)
	inputData.annualIntRate = getInterestRate();	// Get the annual interest reate as a fraction (declare input variable for function)

	float rateConvert = 100.00;	// For the annualIntPercent calc due to double to float warning
	annualIntPercent = inputData.annualIntRate * rateConvert;	// Annual Interest Rate

	calcMonPayment(inputData.price,inputData.downPayment,inputData.tradeIn,noMonths,loanAmt,monPayment,inputData.annualIntRate,monIntRate);	// Calculate payments
	cout << "Trade in before displaying it " << inputData.tradeIn << endl;
	displayLoanForm(inputData.price, inputData.tradeIn, inputData.downPayment, loanAmt, annualIntPercent);	// Display Loan Form
	cout << "Trade in  after displaying it" << inputData.tradeIn << endl;
	displayLoanSchedule(noMonths, monPayment);	// Display Loan Schedule
	   
	return 0;

} // End of Main Function****

//***************************Get price of vehicle with do/while loop and return by value********************************
// Greater than $50.00 and less than $50,000.00
float getPrice()
{
	float price;
	do
	{
		cout << "Please input the price of the vehicle purchasing. Do NOT use COMMAS or DOLLAR SIGNS please.:  ";
		cin >> price;
		if (price > 50000.00 || price < 50.0)
			cout << "The price must be between $50.00 and $50,000.00. Please re-input price.: " << endl;
	}while (price > 50000.00 || price < 50.0);

	cout << price << endl;
	return price;
}

//**************************Get the trade in value with do/while loop and return by value********************************
// Greater than or equal to zero and less than the price
float getTradeIn(float price)
{
	float tradeIn;
	do
	{
		cout << "Enter Trade-In value of vehicle:  ";
		cin >> tradeIn;
		if (tradeIn < 0 || tradeIn >= price)
		{
			cout << "Trade-in needs to be greater than or equal to ZERO and less than the purchase price of the new vehicle. Re-input trade-in value: " << endl;
		}
	}while (tradeIn <0 || tradeIn >= price);
	
	cout << tradeIn << endl;
	return tradeIn;
	cout << tradeIn << endl;
}

//***********************Get the down payment with do/while loop and return by value**************************************
// Greater than or equal to zero and less than price minus the trade-in
float getDownPayment(/* in */ float price, /* in */ float tradeIn)
{
	float downPayment;
	do
	{
		cout << "Enter Down Payment on vehicle:  ";
		cin >> downPayment;
		if (downPayment < 0 || downPayment > (price - tradeIn))
		{
			cout << "The Down Payment needs to be ZERO or higher but not more than the purchase price after trade-in. Re-input Down Payment: " << endl;
		}
	}while (downPayment < 0 || downPayment > (price - tradeIn));

	cout << downPayment << endl;
	cout << "Trade in  at getting down payment" << tradeIn << endl;
	return downPayment;
}

//***********************Get the interest rate with do/while loop and return by value************************************
// Greater than zero and less than .50
float getInterestRate()
{
	float annualIntRate;
	do
	{
		cout << "Enter the Annual Interest Rate as a decimal fraction, IE: Use .06 instead of 6%: ";
		cin >> annualIntRate;
		if (annualIntRate < 0 || annualIntRate >= 0.50)
			cout << "The Annual Interest Rate must be greater than or equal to ZERO and less than .50. Re-input Annual Interest Rate.: ";
	} while (annualIntRate < 0 || annualIntRate >= 0.50);

	return annualIntRate;
		
}

//*******************Calculate 4 Monthly Payments and store in a one-dimensional array.***********************************
// Monthly payments @ 24, 36, 48, & 60 months
void calcMonPayment(float& price, float& downPayment, float& tradeIn, int& noMonths, float& loanAmt, double monPayment[], float& annualIntRate, double& monIntRate)
{
	noMonths = 24;
	int count;
	float rateMon = 12.0;	// Because it compiles with warnings if I do not.
	for (count = 0; count <= 3; count++)
	{
		monIntRate = annualIntRate / rateMon;
		loanAmt = price - downPayment - tradeIn;
		monPayment[count] = (loanAmt * monIntRate) / (1.0 - pow((monIntRate + 1), -noMonths));
		noMonths = noMonths + 12;
	}
	
	return;
}

//**********************Loan Form. Void Function*************************************************************************
// Use a for loop to display the four monthly payments.  Must be a void function
void displayLoanForm(float& price, float& downPayment, float& tradeIn, float& loanAmt, float& annualIntPercent)
{
cout << "\n\n" << endl;
cout << setw(50) << "Honest Dave's Used Cars. \n\n";

	cout << "Vehicle Price" << fixed << setprecision(2) << setw(20) << price << endl;
	cout << "Trade In Value" << fixed << setprecision(2) << setw(19) << tradeIn << endl;
	cout << "Down Payment" << fixed << setprecision(2) << setw(21) << downPayment << endl;
	cout << setw (20) << " _________________________" << endl;
	cout << "Loan Amount" << fixed << setprecision(2) << setw (22) << loanAmt << endl << endl;

	cout<< "Annual Interest Rate" << fixed << setprecision(2) << setw(20) << annualIntPercent << "%" << endl << endl;
	cout << setw (20) << "Monthly Payment Options \n\n";
	return;
}

//**********************Loan Schedule. Pass variables using a struct and 4 payments as an array. Void Function*************
// Use a for loop to display the four monthly payments.  Must be a void function
void displayLoanSchedule(int& noMonths, double monPayment[])
{
	noMonths = 24;
	int count;
	for (count = 0; count <= 3; count++)
	{
		cout << setw (30) << noMonths << " Months" << fixed << setprecision(2) << setw(20) << monPayment[count] << endl;
		noMonths = noMonths + 12;
	}
	cin.get();
	cin.get();
	return;
}


think you may have tradeIn and downPayment in the wrong order in the line
displayLoanForm(inputData.price, inputData.tradeIn, inputData.downPayment, loanAmt, annualIntPercent); // Display Loan Form



use code tags
Was This Post Helpful? 0
  • +
  • -

#3 crunyan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-June 09

Re: Struct Help!

Posted 01 August 2009 - 04:59 PM

Thanks! I was confusing myself with the order as well as the = sign for the functions....

That helped!

This is where I shot myself:

getPrice(inputData.price);	// Get price of vehicle 
	getTradeIn(inputData.price, inputData.tradeIn);	// Get trade-in in dollars and cents (declare input variable for function)
	getDownPayment(inputData.price, inputData.tradeIn, inputData.downPayment);	// Get down payment in dollars and cents (declare input variable for function)
	getInterestRate(inputData.annualIntRate);	// Get the annual interest rate as a fraction (declare input variable for function)
	calcMonPayment(inputData.price,inputData.downPayment,inputData.tradeIn,noMonths,loanAmt,monPayment,inputData.annualIntRate,monIntRate);	// Calculate payments
	displayLoanSchedule(inputData.price, inputData.downPayment, inputData.tradeIn, loanAmt, inputData.annualIntRate, noMonths, monPayment);	// Display output


..as well as some funky weird stuff I was doing in the loan schedule function itself!

Whew! Thanks again!

Mod edit - Please :code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1