A small program about interest loan from bank.

Hi guys , I'm new to C++ programming language. I made a program but I need some suggestions to improve my coding skill, my questions are ,is my code too complicated? Is something wrong with my coding skill ? Thanks !

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

using namespace std;

int main() {
	int loan, year, month;
	double interest, loanInterest, amount;
	
	cout << "Enter amount of loan :";
	cin >> loan;
	if ( loan < 0){
		for ( loan; loan < 0 ;)
			cout << "\nerror ! Enter loan amount :";
			cin >> loan;
	};
	cout << "\nEnter loan interest :";
	cin >> interest;
	if ( interest < 0 || interest > 100){
		for ( interest; interest < 0 || interest > 100 ;)
			cout << "\nerror ! Enter loan interest :";
			cin >> interest;
	};
	cout << "\nEnter loan period :";
	cin >> year;
	if ( year < 0){
		for ( year; year < 0 ;)
			cout << "\nerror ! Enter loan period :";
			cin >> year;
	};
	
	month = year * 12;

	interest /= 100;

	loanInterest = loan * interest * year ;

	amount = loanInterest + loan;
	cout << "amount :" << amount;

	cout << "\nTotal of Interest :" << loanInterest;
	cout << "\nTotal for pay every month :" << amount / month << "\n";

	cout << "Month" << setw(10) << "Remain(RM)" << "\n";

	for(int i = 0 ; i < month+1; ++i)
		cout << i << setw(10) << (amount - ((amount / month) * i))
        << "\n" ;

	return 0;

}
is my code too complicated?

In my opinion, no. But if the question is could it be better, the probably yes.

One way of improving the code would be to use functions.

Another thing I recommend is that you always use braces with your control statements, even when not technically required.

Let's look at the following snippet:
1
2
3
4
5
	if ( year < 0){
		for ( year; year < 0 ;)
			cout << "\nerror ! Enter loan period :";
			cin >> year;
	};

I see a couple of issues with this snippet. First looking at the indentation it appears that you want the cout statement and the cin statement to be executed inside the for(;;) loop, but that is not what is happening, the cin statement is not part of the loop. Second the use of the if() statement and for(;;) loop in this snippet can be simplified by using a while() loop.

1
2
3
4
while(year < 0){
   cout << "\nError period must be positive! Please reenter the loan period: ";
   cin >> year;
}

Normally a for(;;) loop is used when accessing sequential data, not for validating input.


jlb,

I see, I'll use functions in my next practice and I'll do some research about for loop and while loop.

And for use braces with the control statements is my mistake.I'll correct it in the new way.

Thanks for teaching me !
Another way to input the numbers and do error checking:
1
2
3
4
5
6
do {
    cout << "\nEnter loan interest :";
    cin >> loan;
    if (loan >= 0) break;
    cout << loan << " is invalid. Loan must be greater than 0.";
} while (true);


This code is nice and tight even if it seems unintuitive because the expected flow is to break out of the loop at line 4. Note that the error message prints the value that was input. This is useful in case cin>>loan is failing.
dhayden,

I got what u mean. But the keyword break quite new to me. I'll keep it in mind .

Thanks buddy !
If you are going to validate input you need to consider the case when a user enters invalid input. For example what would happen if the user enters a K instead of a number? so if you have an integer variable int loan = 0; and you do cin >> loan; if the user enters k, input will fail so the value of loan will not change and cin will enter a fail state. The k will still be waiting for input so if we cin >> loan; again it will fail for two reasons cin is in an error state and because k is not an integer.

lets look at an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <limits>
using namespace std;

int main()
{
	int loan = 0;
	cout << "Enter amount of loan :";
	while (!(cin >> loan) || loan < 0)
	{
		cin.clear(); //removes any error flags
		cin.ignore(numeric_limits<streamsize>::max(), '\n');//removes bad input
		std::cout << "Invalid input please try again" << std::endl;
		cout << "Enter amount of loan :";
	}
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	std::cout << "loan " << loan << std::endl;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	return 0;
}


ok so line 9 in the above code while (!(cin >> loan) || loan < 0) needs to be explained a bit. take this portion !(cin >> loan) here we attempt to get an integer from cin. The >> operator returns its left hand operand. so after this statement executes we have something that looks like this
while (!(cin) || loan < 0) ok so what does !(cin) do ? it checks the stream to see if any error flags are set. Meaning if cin is good ie. its not in an error state it will return 1 and if its not good meaning it is in an error state than it will return 0. If 0 is returned that means there was a problem and we need to deal with that. By doing !(cin) the 0 returned by cin is changed to a 1 and causes the wile loop to execute where we handle the problem.

When cin is in an error state all input attempts fail, so we need to clear the error flags. On line 11 we do cin.clear(); //removes any error flags this removes any error flags and takes us out of the fail state. However, the input that caused the error state is still waiting to be input because we failed to input it the first time. So we need to remove this input because it will just cause us to fail again. That's where cin.ignore(numeric_limits<streamsize>::max(), '\n');//removes bad input comes in on line 12 this removes everything up to and including a newline. now that the bad input is cleared we can try to input again.
Last edited on
Yanson,

Oh yeah! I've forgotten if a user enter a character, a big mistake from me and a big lesson from you!

Another question is , "numeric_limits<streamsize>::max()" , can you explain this? What is it use for and how it works ?

Thanks!
Topic archived. No new replies allowed.