Modularizing code and Arrays.

Hey all! I've been working on a financial calculator for the past month and I've got a couple questions. In the book I'm reading on c++, it talks about modularizing the code and I was wondering how I could modularize the code for my calculator. I don't quite get what it means or how to do/write it. Last question I have is if it is possible to use arrays in my calculator program. I tried using arrays to get the information of the user before choosing what option they want to do but it never worked.

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
  #include <iostream>
#include <ctime>
#include <string>
#include <iomanip>

using namespace std;

void showMenu();

int main()
{

	double interestRate, monthsPaid, remainingLength, loanLength, loanAmount, monthlyPay, totalDue, totalInterest;
	int choice;
	const int monthlyPayChoice = 1,
		totalDueChoice = 2,
		remainingLengthChoice = 3,
		totalInterestChoice = 4,
		quit = 5;

	do
	{

		showMenu();
		cin >> choice;

		while (choice < monthlyPayChoice || choice > quit)
		{
			cout << "Please enter a valid menu choice: ";
			cin >> choice;
		}

		if (choice != quit)
		{

			cin >> choice;

			switch (choice)
			{
			case monthlyPayChoice:

				cout << "\n\t\Welcome to the Monthly Payment Calculator!\n\n";
				cout << "Please enter the total amount of your loan: ";
				cin >> loanAmount;
				cout << "please enter the total interest amount of your loan: ";
				cin >> totalInterest;
				cout << "Please enter the length of your loan in months: ";
				cin >> loanLength;

				monthlyPay = (loanAmount + totalInterest) / loanLength;

				cout << "Your monthly payment is: " << monthlyPay << endl;
				break;

			case totalDueChoice:

				cout << "\n\t\Welcome to the Total Due Calculator!\n\n";
				cout << "Please enter the length of the loan in months: ";
				cin >> loanLength;
				cout << "Please enter the total amount of your loan: ";
				cin >> loanAmount;
				cout << "Please enter the interest rate of your loan: ";
				cin >> interestRate;

				totalDue = (loanLength * loanAmount) + (loanLength * loanAmount * (interestRate / 100));

				cout << "The total amount that is due is: " << totalDue << endl;
				break;

			case remainingLengthChoice:

				cout << "\n\t\Welcome to the Remaining Month Calculator!\n\n";
				cout << "Please enter the length of the loan in months: ";
				cin >> loanLength;
				cout << "Please enter the amount of months you've already paid: ";
				cin >> monthsPaid;

				remainingLength = loanLength - monthsPaid;

				cout << "The amount of months you have left to pay is: " << remainingLength << endl;
				break;

			case totalInterestChoice:

				cout << "\n\t\Welcome to the Total Interest Calculator!\n\n";
				cout << "Please enter the length of the loan in months: ";
				cin >> loanLength;
				cout << "Please enter the total amount of your loan: ";
				cin >> loanAmount;
				cout << "Please enter the total amount of your loan: ";

				totalInterest = totalDue - (loanLength * loanAmount);

				cout << "Your total interest due is: " << totalInterest << endl;
				break;
			}
		}

	}
	while (choice != quit);
	return 0;
}
	void showMenu()
	{
		cout << "\n\t\Welcome to the Loan Calculater program!\n\n"
			<< "1. Calculate Monthly Payment\n"
			<< "2. Calculate Total Due\n"
			<< "3. Calculate Remaining Length of the Loan\n"
			<< "4. Calculate Total Interest\n"
			<< "5. Quit Program\n\n"
			<< "Enter your selection: ";
	}
For a start, you can separate the user i/o from the calculations.

In your case, it's not quite so obvious becuase the calculations are one liners.
Hello Snarkky,

Welcome to the forum.

To make your code more modular tends to mean putting things in functions.

See: http://www.cplusplus.com/doc/tutorial/functions/

For line 15 you could use an "enum" in place of what you did.

http://www.cplusplus.com/doc/tutorial/other_data_types/#enumerated_types

For the "showMenu" function I like to print the menu, get the input and verify that the choice entered is a valid choice before returning the choice back to where it was called from. In this you have a function that does one thing and returns the result for later use. Lines 25 - 32 would be better used in the"showMenu" function.

Line 36 is redundant and pointless as you already would have a value for "choice" and you have no prompt to let the user know what it is waiting for.

In the switch/case for case 1 lines 42 - 48 could be put in a function and the math on line 50 would be done in the function returning the result back to the case 1. Something like: monthlyPay = PayChoice();. For classes, structs and functions I like to start the names with a capital letter to distinguish them form a normal variable. I would say it is mostly a personal choice, but I have seen this in other programs.

This way each case statement should have three lines to it.

Hope that helps,

Andy

P.S. You should initialize all your variables when they are defined. The simplest way is with an empty set of {}s. like; double interestRate{}, ...;.
Last edited on
You could condense the output but doing so, while more modular, would be significantly more confusing to follow.
Topic archived. No new replies allowed.