Passing by reference without global

I am not sure what I am doing here, I was given a formula to use in the passing by reference and my lecture was not helpful at all. I am also supposed to put this in a loop so that someone can keep entering until they terminate the program..




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
#include<iostream>
#include<cmath>
using namespace std;

void functionA(int&z)

{
	z=A/S
	x = r / 1200;
	y = pow(1 + x, 12 * t) - 1;
	S = R * y / x;
	TotalPayment=R*(12*t)
		TotalInterest=(TotalPayment-A)

	
	

}
int main()
{
	functionA(z);
	double R, r, t,TotalPayment;
	double S, x, y,TotalInterest;
		cout << "Please Enter the Loan Amount:\n";
		cin >> R;
		cout << "Please Enter the Interest Rate:\n";
		cin >> r;
		cout << "Please Enter the Term in years:\n";
		cin >> t;

		

		cout << "Your Monthly Payment is" << "$" << S << endl;
		cout << "Your total payment will be" << "$" << TotalPayment << endl;
		cout << "Total interest paid will be" << $ << TotalInterest << endl;
		return 0;
	}
	}
Last edited on
Am I off to the right start, I know that I need to adjust the cout answers at the end, but I have to be honest. I have no idea what I am doing....
Well it would be nice to format your post for readability.
https://www.cplusplus.com/articles/jEywvCM9/
Just press edit and add the code tags.

> I was given a formula to use in the passing by reference and my lecture was not helpful at all.
> I am also supposed to put this in a loop so that someone can keep entering until they terminate the program..
Before you make it a function, before you make it a loop - just try to do it once in main().

Make something small that goes some way to solving the problem.
Then you have something to build on.

Rome wasn't built in a day, etc etc.
Sorry about that, first time I posted on here... I am trying to pass this class and I am struggling with this, my teacher isn't very helpful.. this is what he is looking for

Without using global variables, write a C++ program that contains a function (other than main() function) that calculates the monthly payment of a mortgage. The function will receive (will have inputs) loan amount, annual interest rate, and the term (the number of years to pay off the loan), and returns the monthly payment, total payment, and the total interests paid. Do not include any cin nor cout statements inside of the function, in other words, the function don't display any calculation results, it returns calculation results, such as monthly payment, total payment, and the total interests paid to the caller. The caller function will display the calculation results. (Make sure no calculation made in main() function, the main() receives the calculation results from the function calls and displays those results on the screen).

More specifically, in the main() function, using a a looping statement to allow a user to repeatedly use the function for the calculation.

Last edited on
Understanding compiler error messages is a skill that will go a long way. Read them and try to guess what they mean. If you still aren't sure, post the compiler error message and just tell us your guess as to what it means, then we can tell you how far off you are.

Line 21: you are attempting to call functionA, but the variable 'z' does not exist in the main function. Non-global variable names in different functions have separate scope.

Furthermore, you attempt to use like half a dozen variables that don't exist in functionA. What is A? What is s? etc.
If x in functionA is supposed to be the same variable as the x in main, you must pass that by reference, too

1
2
3
4
5
6
7
void functionA(int& a, int& x)
{ ... }

// main
int a:
int x; // etc.
functionA(a, x);


Without any restrictions, write a C++ program to compute the monthly payment of one mortgage.
This is the essence of your computer program, the most important bit of functionality. It is also the hardest part of your task, one that you should solve before adding superfluous restrictions.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
  // enter loan principal, interest rate, and term; then
  // compute monthly payment; then
  // print monthly payment
  
  // your code here

}


Once you have this, you can focus on making sure that all of the assignment's requirements are satisfied, one at a time.
Last edited on
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
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	int TotalInterestPaid;
	int TotalPayment;
	double A, r, t;
	double R, x, y;
	cout << "Please Enter The Loan Amount:\n";
	cin >> A;
	cout << "Please Enter the Interest Rate:\n";
	cin >> r;
	cout << "Please Enter the Number of Years:\n";
	cin >> t;

	x = r / 1200;
	y = 1-(pow(1 + x,-12 * t));
	R = A / (y / x);
	TotalPayment = R * (12 * t);
	TotalInterestPaid = TotalPayment - A;

	cout << "Your Monthly Payment is" << "$" << R << endl;
	cout << " Your Total Payment is" << "$" << TotalPayment << endl;
	cout << "Total Interest paid is" << "$" << TotalInterestPaid << endl;
	return 0;
}


ok so I worked it so it works, thank you everyone. But now i don't understand the pass by reference part
OK, so your first edit of the working code is this.
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
#include<iostream>
#include<cmath>
using namespace std;

void functionA()
{
	x = r / 1200;
	y = 1-(pow(1 + x,-12 * t));
	R = A / (y / x);
	TotalPayment = R * (12 * t);
	TotalInterestPaid = TotalPayment - A;
}

int main()
{
	int TotalInterestPaid;
	int TotalPayment;
	double A, r, t;
	double R, x, y;
	cout << "Please Enter The Loan Amount:\n";
	cin >> A;
	cout << "Please Enter the Interest Rate:\n";
	cin >> r;
	cout << "Please Enter the Number of Years:\n";
	cin >> t;

	functionA(R, TotalPayment, TotalInterestPaid);

	cout << "Your Monthly Payment is" << "$" << R << endl;
	cout << " Your Total Payment is" << "$" << TotalPayment << endl;
	cout << "Total Interest paid is" << "$" << TotalInterestPaid << endl;
	return 0;
}

What 1-line change do you need to make in order to make it compile again?

Hint: it's your reference parameters.
move the functionA(R, TotalPayement etc?
or I believe its what goes in the void functionA()<----?
I also added the while statement in there to make it loop

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
#include<iostream>
#include<cmath>
using namespace std;

void functionA()
{
	x = r / 1200;
	y = 1 - (pow(1 + x, -12 * t));
	R = A / (y / x);
	TotalPayment = R * (12 * t);
	TotalInterestPaid = TotalPayment - A;
}

int main()
{
	int a = 1;
	int TotalInterestPaid;
	int TotalPayment;
	double A, r, t;
	double R, x, y;
	while (a == 1) {
	cout << "Please Enter The Loan Amount:\n";
	cin >> A;
	cout << "Please Enter the Interest Rate:\n";
	cin >> r;
	cout << "Please Enter the Number of Years:\n";
	cin >> t;

	functionA(R, TotalPayment, TotalInterestPaid);

	cout << "Your Monthly Payment is" << "$" << R << endl;
	cout << " Your Total Payment is" << "$" << TotalPayment << endl;
	cout << "Total Interest paid is" << "$" << TotalInterestPaid << endl;
	cout << "===================================================================" << endl;
	cout << "Please enter 1 to continue, or any other number to end the program:" << endl;
	cin >> a;
}
	return 0;
}
Last edited on
> I also added the while statement in there to make it loop
You should have fixed the first problem of making it work once, using a function with reference parameters.

Not add more code to something that doesn't work.

You already know what a reference parameter looks like from your first post.

void functionA(int x, int, int r, int y, int x, int t, int R, int A, int TotalPayment, int TotalInterestPaid) ?? i imagine its int & something for pass by reference?
You only need the three I have shown you.

> i imagine its int & something for pass by reference?
Yes, you seem to know everything you need to know.
Assuming the calculations are correct:

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
#include <iostream>
#include <cmath>
using namespace std;

void calculate(double A, double r, double t, double& R, double& TotalPayment) {
	const auto x {r / 1200};
	const auto y {1 - (pow(1 + x, -12 * t))};

	R  = A / (y / x);
	TotalPayment = R * (12 * t);
}

int main() {
	double TotalInterestPaid {}, TotalPayment {}, R {}, A {}, r {}, t {};

	cout << "Please Enter The Loan Amount: ";
	cin >> A;

	cout << "Please Enter the Interest Rate: ";
	cin >> r;

	cout << "Please Enter the Number of Years: ";
	cin >> t;

	calculate(A, r, t, R, TotalPayment);

	cout << "Your Monthly Payment is " << "$" << R << '\n';
	cout << "Your Total Payment is " << "$" << TotalPayment << '\n';
	cout << "Total Interest paid is " << "$" << TotalPayment - A << '\n';
}

Topic archived. No new replies allowed.