MonthlyPayment not changing in Array of Structures

Hello I am about done with this challenge but the cost is not changing when I'm trying to display all of the array. Here is my challenge to sum up the problem.

Write a program that uses a structure to store the following data about a car dealership:
Make
Model
Year
Cost
The program should use an array of at least 5 structures. It should let the user enter data into the array,
calculate the monthly payment with an interest rate of 7% for 36 months and display all the data stored in
the array.

Use the following formula
x = (1+Rate/12)
monthlyPayment = (Cost * (Rate / 12)) / (1 - pow(x,-36))


The problem is I can't put the monthly payments part of the structure but it needs to change for every car. What is another way of doing this and why is the cost not changing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Declare Dealership structure
struct Dealership
{
	string make;
	string model;
	string year;
	double cost;
};


int main()
{
	// Array size
	const int ARRAY_SIZE = 5;

	Dealership userInfo[ARRAY_SIZE];

	// Variables
	const int MONTHS = 36;
	const double INTEREST = .07;
	double x;
	double monthlyPayment;

	// Prompt user to enter info
	for (int count = 0; count < ARRAY_SIZE; count++)
	{
		cout << "This is for car number " << (count + 1) << endl;
		cout << "---------------------------------- \n";
		cout << "Please enter the make of the car." << "\n";
		getline(cin, userInfo[count].make);
		cout << endl;
		cout << "Please enter the model of the car." << "\n";
		getline(cin, userInfo[count].model);
		cout << endl;
		cout << "Please enter the year of the car." << "\n";
		getline(cin, userInfo[count].year);
		cout << endl;
		cout << "Please enter the cost of the car." << "\n";
		cin >> userInfo[count].cost;
		cin.ignore();

		// Calculates monthly cost
		x = (1 + INTEREST / 12);
		monthlyPayment = (userInfo[count].cost * (INTEREST / 12)) / (1 - pow(x, -36));

		cout << endl << endl;
	}

	// Displays the output (Everything in the array)
	cout << "Here are your cars. \n";
	cout << fixed << showpoint << setprecision(2);
	for (int count = 0; count < ARRAY_SIZE; count++)
	{   
		cout << "Car " << (count + 1) << endl;
		cout << "---------------------------------- \n";
		cout << "Make: " << " " << userInfo[count].make << "\n";
		cout << "Model: " << " " << userInfo[count].model << "\n";
		cout << "Year: " << " " << userInfo[count].year << "\n";
		cout << "Total Cost: $" << userInfo[count].cost << "\n";
		cout << "Monthly Payments: $" << monthlyPayment << "\n";
		cout << endl;
	}

	return 0;
}
The problem is I can't put the monthly payments part of the structure

I'm sorry, I'm having trouble understanding that sentence. Are you saying you can't change the structure declaration?

Your payment amount is not changing because when you exit the loop at line 52, monthlyPayment contains the last one calculated at line 49.

If you can't include monthlyPayment as part of the struct, then there is no point in calculating it at line 49 because it just gets overlaid by the next iteration of the loop. You should move the calculation at lines 48-49 to after line 64.
Last edited on
Ok I figured it out thanks! :) I was saying I wasn't allowed in the assignment to make monthlyPayments part of the structure.
Topic archived. No new replies allowed.