How do I Loop 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
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
double maturityCalc (double invest, float interestR, float years, int compound)
{	
	double interestT = 0;
	double epon = 0;

	interestT = (1+((interestR/100) / compound)); // =(1+P/M)
	epon = (years * compound); //^(Y*M)

	double result = invest * pow (interestT, epon); //Finally, multiply the investment into the total interest

	return result;
}
int maturityDriver()
{
	double invest = 0;	
	float interestR = 0; 
	float years = 0;	
	int compound = 0;	
	double maturity = 0; 

	cout << "This program will calcuate a maturity.  Please enter the following: \n";
		
	//ask user for input
	cout << "\nInital investment: ";
	cin >> invest;
	cout << "\n";
	
	cout << "Interest rate, in percentage: ";
	cin >> interestR;
	cout << "\n";
	
	cout << "Total years of maturity: ";
	cin >> years;
	cout << "\n";
	
	cout << "Compound, number of times interest is applied: ";
	cin >> compound;
	cout << "\n";

	maturity = maturityCalc(invest, interestR, years, compound);
	
	cout << "Your maturity is: " << maturity << "\n";

	system ("pause");
	return 0;
}

double loanPay (int loan, float rate, int payment)
{
		float baseLoan = 0;		
		double numer = 0;		
		double denom = 0;		
		double resultB = 0;		
		double resultF = 0;		
		double resultT = 0;		

		baseLoan = (1 + (rate/1200));						
		resultB = pow ((double) baseLoan, (double) payment); 
		numer = ((double) rate/1200) * resultB;			
		denom = resultB - 1;							 
		resultF = numer / denom;						 
		resultT = resultF * loan;						 
	
	return resultT;
}

int loanDriver()
{
	cout << "This program will calculate your monthly loan payments,\n";
	cout << "please enter the following: \n";

		int loan, payment = 0;  //L and N, Loan amount and number of payments
		float rate = 0;			//R, interest rate
		double getLoan = 0; //calls getloan
	
		cout << "\nLoan amount: ";
		cin >> loan;
		cout << "\n";
	
		cout << "Interest Rate, in percentage: ";
		cin >> rate;
		cout << "\n";

		cout << "Number of payments: ";
		cin >> payment;
		cout << "\n";

		getLoan = loanPay(loan,rate,payment);

		cout << "Your monthly loan payment is: " << getLoan << "\n"; //display results

		system ("pause");
		return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "This program is designed to calculate a Maturity value of an investment or \n";
	cout << "the monthly payments on a loan.  Please enter the 1 or 2. \n";
	cout << "1) Maturity Calculator \n";
	cout << "2) Monthly Loan Payments \n";

	char response = ' ';
	cin >> response;
	
	if (response == '1')
	{
		maturityDriver(); //calls maturity function
	}
	else if (response == '2')
	{
		loanDriver(); //calls loan function
	}
	else
	{
		cout << "You entered in an invalid option. \n";
		system("pause");
	}
	return 0;
}


Part of my assignment is to loop this, can anyone give me an idea on how to even start? I've read a bunch of articles and it's just too confusing for me.

• Our Maturity/Payment program should be able to run indefinitely allowing the user to run many loan
or maturity calculations.
• This looping continues until the user decides that he/she has calculated enough and presses a certain
key (let’s say ‘x’) to quit the program.
• Write the program once using the while loop with the if-else construct. (assign6a)
• Write the program a second time using the for loop with the switch construct. (assign6b)
Several different ways you can accomplish this. for example, use a while loop and check if the user has entered 'x' as your first thing to check and set a boolean value to quit which the while loop will look for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	char response = ' ';
	bool bQuit = false;
	do {
		cout << "This program is designed to calculate a Maturity value of an investment or \n";
		cout << "the monthly payments on a loan.  Please enter the 1 or 2. \n";
		cout << "1) Maturity Calculator \n";
		cout << "2) Monthly Loan Payments \n";

		cin >> response;	

		if (response == 'x') bQuit = true;
		else if (response == '1') {
			maturityDriver(); //calls maturity function
		}
		else if (response == '2') {
			loanDriver(); //calls loan function
		}
		else {
			cout << "You entered in an invalid option. \n";
			system("pause");
		}
        } while (bQuit == false);
	return 0;
how would i go about using the the "for loop" and "switch construct" ?

edit: thank you very much for the help
Last edited on
Topic archived. No new replies allowed.