# Struct Help!

Page 1 of 1

## 2 Replies - 1068 Views - Last Post: 01 August 2009 - 04:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=117993&amp;s=9de179c8520d0463651605b266f7fd35&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 crunyan

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

# Struct Help!

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..

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
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
};

// 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.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

cout << "Trade in before displaying it " << inputData.tradeIn << endl;
displayLoanForm(inputData.price, inputData.tradeIn, inputData.downPayment, loanAmt, annualIntPercent);	// Display Loan Form
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
{
do
{
cout << "Enter Trade-In value of vehicle:  ";
{
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;
}

}

//***********************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;
}
```

Is This A Good Question/Topic? 0

## Replies To: Struct Help!

### #2 horace

• D.I.C Lover

Reputation: 535
• Posts: 2,825
• Joined: 25-October 06

## Re: Struct Help!

Posted 01 August 2009 - 11:26 AM

crunyan, 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..

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
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
};

// 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.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

cout << "Trade in before displaying it " << inputData.tradeIn << endl;
displayLoanForm(inputData.price, inputData.tradeIn, inputData.downPayment, loanAmt, annualIntPercent);	// Display Loan Form
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
{
do
{
cout << "Enter Trade-In value of vehicle:  ";
{
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;
}

}

//***********************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

### #3 crunyan

Reputation: 0
• 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
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)
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!

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }