I need help identifying what's wrong with my code?

I have no idea what is wrong with my code?
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 #include <iostream>
using namespace std;

//Function Declarations
float getLoanPayment();
float getInsurance();
float getGas();
float getOil();
float getTires();
float getMaintenance();
float totalMonthlyCosts();
float totalAnnualCosts();
void showResult(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost);
float calculateTotalMonthlyCosts(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost);



int main() // start of the main function

{
	//Declare and Initialze Variables
	float loanPaymentCost = 0;
	float insuranceCost = 0;
	float gasCost = 0;
	float oilCost = 0;
	float tiresCost = 0;
	float maintenanceCost = 0;


	// Loan Payment Cost
	loanPaymentCost = getLoanPayment();

	//Get Insurance Cost
	insuranceCost = getInsurance();

	//Get Gas
	gasCost = getGas();

	// Get Oil Cost 
	oilCost = getOil();

	// Get Tires Cost
	tiresCost = getTires();

	// Get Maintenance Cost
	maintenanceCost = getMaintenance();

	// Display results
	showResult(loanPaymentCost, insuranceCost, gasCost, oilCost, tiresCost, maintenanceCost);

	// Calculate toal Monthly costs 
	totalMonthlyCosts(loanPaymentCost, insuranceCost, gasCost, oilCost, tiresCost, maintenanceCost);

	char quitKey;
	//Delay
	cout << "Press any key and Enter to end program: ";
	cin >> quitKey;

	//End of Program
	return 0;
}
// Function to get Loan Payment Cost
float getLoanPayment()
{
	float loanPaymentCost;
	cout << "Enter the Loan Payment Cost and press ENTER" << endl;
	cin >> loanPaymentCost;
	//return result
	return loanPaymentCost;
}

// Function to get Insurance Cost
float getInsurance()
{
	float insuranceCost;
	cout << "Enter the Insurance Cost and Press Enter" << endl;
	cin >> insuranceCost;
	//return result
	return insuranceCost;
}

// Function to get Gas Cost
float getGas()
{
	float gasCost;
	cout << "Enter the gas cost and Press Enter" << endl;
	cin >> gasCost;
	// return result
	return gasCost;
}

// Function to get Oil cost
float getOil()
{
	float oilCost;
	cout << "Enter the oil cost and press Enter" << endl;
	cin >> oilCost;
	// return result
	return oilCost;
}

// Function to get Tires Cost
float getTires()
{
	float tiresCost;
	cout << " Enter the tire cost and press Enter" << endl;
	cin >> tiresCost;
	// return result
	return tiresCost;
}
// Function to get Maintenance cost

float getMaintenance()
{
	float maintenanceCost;
	cout << " Enter the maintenance cost and press Enter" << endl;
	cin >> maintenanceCost;
	// return result
	return maintenanceCost;
}


void showResult(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost)
{
	//Display all the purchase amount, state tax, county tax, total tax, and total of sale
	cout << "Loan Payment Cost= " << loanPaymentCost << endl;
	cout << "Insurance Cost = " << insuranceCost << endl;
	cout << "Gas Cost = " << gasCost << endl;
	cout << "Oil Cost = " << oilCost << endl;
	cout << "Tires Cost = " << tiresCost << endl;
	cout << "Maintenance Cost= " << maintenanceCost << endl;
}

float calculateTotalMonthlyCosts(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost)
{
	float totalMonthlyCost;
	
	totalMonthlyCost = (loanPaymentCost + insuranceCost + gasCost + oilCost + tiresCost + maintenanceCost);

		return totalMonthlyCost;
}



Hi

totalMonthlyCosts on line 52 doesn't exist, the compiler should have had an error about that.

Did you mean to call calculateTotalMonthlyCosts ? Make sure you make use of the value returned in main()

If you have compiler warnings and errors, then it is usually a good idea to post them here.

Some other things:

A matter of style - if you have lots of arguments or anything that will make your line of code long, you can split it across lines like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
float calculateTotalMonthlyCosts(const float loanPaymentCost,
                                               const float insuranceCost, 
                                               const float gasCost, 
                                               const float oilCost, 
                                               const float tiresCost, 
                                               const float maintenanceCost)
{
	float totalMonthlyCost;
	
	totalMonthlyCost = (loanPaymentCost + insuranceCost + gasCost + oilCost + tiresCost + maintenanceCost);

		return totalMonthlyCost;
}


This makes it much easier to read, and one can put comments if necessary.

Notice how I made the parameters const Try to use const as much as you can.

Prefer the double type, it is the default in c and c++. The precision of a float can easily be exceeded.

I like to put digits on either side of the decimal point to reinforce the idea that it is a FP number. As in 0.0 or 0.1 or 5.0, not , .1 or 5.

Hope all goes well
Hey,

It looks like you mixed your totalMonthlyCosts function with the calculateTotalMonthlyCosts function.

totalMonthlyCosts is only a function prototype and you never actually declared it! Also the function prototype is wrong since there are no parameters on your forward declaration for totalMonthlyCosts(), which means the compiler is expecting no parameters right now (and you are providing 7).

You can forward declare it like this:
float totalMonthlyCosts(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost);

And remember to define your function since it's missing right now, or simply change your function call to calculateTotalMonthlyCosts

Last edited on
why would I name those as constants??
@SideEffects i fixed what you told me to fix, but when it runs it stops after it displays all the costs and doesn't add them up nor does it show the sum...
@Frankie1210 The reason why it's not displaying the added value is because you are not assigning the return value of the totalMonthlyCosts to any variable so it's lost. Also, you are not calling the showResults function with a monthlyCosts parameter, so it won't print anything.

Here is the code with the changes I have made. They are in bold and have comments. I noticed that you have a totalAnnualCosts function that you never used, you can try to see what changes I made with the monthly costs and do the same to add a totalAnnualCosts function that just multiplies the monthlyCosts variable by 12.

Here is the code:

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
using namespace std;

//Function Declarations
float getLoanPayment();
float getInsurance();
float getGas();
float getOil();
float getTires();
float getMaintenance();
//Got rid of the totalMonthlyCosts function
float totalAnnualCosts();
void showResult(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost, float monthlyCost); //Added the monthlyCost parameter
float calculateTotalMonthlyCosts(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost);



int main() // start of the main function

{
	//Declare and Initialze Variables
	float loanPaymentCost = 0;
	float insuranceCost = 0;
	float gasCost = 0;
	float oilCost = 0;
	float tiresCost = 0;
	float maintenanceCost = 0;
	float totalMonthlyCosts = 0; //Initialized totalMonthlyCosts variable to assign the result of the function


	// Loan Payment Cost
	loanPaymentCost = getLoanPayment();

	//Get Insurance Cost
	insuranceCost = getInsurance();

	//Get Gas
	gasCost = getGas();

	// Get Oil Cost 
	oilCost = getOil();

	// Get Tires Cost
	tiresCost = getTires();

	// Get Maintenance Cost
	maintenanceCost = getMaintenance();

	// Calculate total Monthly costs 
        // Assigned the result of calculateTotalMonthlyCosts to a variable
	totalMonthlyCosts = calculateTotalMonthlyCosts(loanPaymentCost, insuranceCost, gasCost, oilCost, tiresCost, maintenanceCost);

	// Display results
        // I switched the function call and placed it at the end of main, it's the last function thats called. I also addeed a totalmonthlyCosts parameter.
	showResult(loanPaymentCost, insuranceCost, gasCost, oilCost, tiresCost, maintenanceCost, totalMonthlyCosts);

	char quitKey;
	//Delay
	cout << "Press any key and Enter to end program: ";
	cin >> quitKey;

	//End of Program
	return 0;
}
// Function to get Loan Payment Cost
float getLoanPayment()
{
	float loanPaymentCost;
	cout << "Enter the Loan Payment Cost and press ENTER" << endl;
	cin >> loanPaymentCost;
	//return result
	return loanPaymentCost;
}

// Function to get Insurance Cost
float getInsurance()
{
	float insuranceCost;
	cout << "Enter the Insurance Cost and Press Enter" << endl;
	cin >> insuranceCost;
	//return result
	return insuranceCost;
}

// Function to get Gas Cost
float getGas()
{
	float gasCost;
	cout << "Enter the gas cost and Press Enter" << endl;
	cin >> gasCost;
	// return result
	return gasCost;
}

// Function to get Oil cost
float getOil()
{
	float oilCost;
	cout << "Enter the oil cost and press Enter" << endl;
	cin >> oilCost;
	// return result
	return oilCost;
}

// Function to get Tires Cost
float getTires()
{
	float tiresCost;
	cout << " Enter the tire cost and press Enter" << endl;
	cin >> tiresCost;
	// return result
	return tiresCost;
}
// Function to get Maintenance cost

float getMaintenance()
{
	float maintenanceCost;
	cout << " Enter the maintenance cost and press Enter" << endl;
	cin >> maintenanceCost;
	// return result
	return maintenanceCost;
}


void showResult(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost, float monthlyCost)
{
	//Display all the purchase amount, state tax, county tax, total tax, and total of sale
	cout << "Loan Payment Cost= " << loanPaymentCost << endl;
	cout << "Insurance Cost = " << insuranceCost << endl;
	cout << "Gas Cost = " << gasCost << endl;
	cout << "Oil Cost = " << oilCost << endl;
	cout << "Tires Cost = " << tiresCost << endl;
	cout << "Maintenance Cost= " << maintenanceCost << endl;
        cout << "Total Monthly Cost = " << monthlyCost << endl; //Printed the monthly costs variable
}

float calculateTotalMonthlyCosts(float loanPaymentCost, float insuranceCost, float gasCost, float oilCost, float tiresCost, float maintenanceCost)
{
	float totalMonthlyCost;

	totalMonthlyCost = (loanPaymentCost + insuranceCost + gasCost + oilCost + tiresCost + maintenanceCost);

	return totalMonthlyCost;
}
Last edited on
Topic archived. No new replies allowed.