Inflation / Depreciation calculator

In an effort to become more proficient with C++, I'm developing small snippets that are of some value to me. What I'm looking for is, can this code be improved, especially in regard to optimization.

Input
#1: Amount = any real number

#2: Ratio in %. negative values invoke depreciation otherwise inflation.
Amounts > 1 will be reduced to decimal equivalent. ie ( 12.3 > .1230 )

#3: Years < 50 will have 2000 added to them and < 100 will have 1900.
Otherwise use 4 digits.

My next version will have a procedure inserted @ line 73 that will display a table, much like a spreadsheet.

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

using namespace std;

int main ( void ) {
	tm *Time_Info;
	time_t CurTime;
	
	short	CurYear, Year;
	double	Ratio, Amount, Total;
	bool	Deprec = false;
	
	time ( &CurTime );
	Time_Info = localtime ( &CurTime );
	CurYear = Time_Info->tm_year + 1900;
	
	cout << "\n\n\tDepreciation / Inflation Calculator";
	cout << "  \n\t--------------  " << CurYear << "  -------------";
	cout << fixed;
	cout << setprecision (2);
	
	do	{
		// Loop until user enters zero.
		cout << "\n\n  ---->\tAmount: ";
		cin >> Amount;
		if ( !Amount ) break;
		
		// Get percentage and convert to decimal value if required.
		cout << "\t Ratio: ";
		cin >> Ratio;
		if ( !Ratio ) break;

		if ( Ratio < 0 ) {
			Ratio *= -1;
			Deprec = true;
			}
		else
			Deprec = false;
			
		if ( Ratio >= 1 )
			Ratio /= 100;		// Ratio value must always be decimal.
		
		// Get begin or end year. Analysis can be overidden by enter all 4 digits.
		cout << "\t  Year: ";
		cin >> Year;
		if ( !Year ) continue;
		if ( Year < 50 )
			Year += 2000;		// Values < than 50 will be in this century
		else
			if ( Year < 100 )	// Values < than 100 will be in last century
				Year += 1900;
		
		int Count = CurYear - Year;
		if ( Count < 0 ) Count *= -1;
		
		cout << "\n\n\t";
		if ( Deprec )
			cout << "Depreciating "; else cout << "Inflating ";

		cout << Amount << " @ " << Ratio * 100 << "% over " << Count << " years \n";

		for ( int Cnt = 0; Cnt < Count; Cnt++ ) {
			double Dep = Amount * Ratio;
			if ( Deprec )
				Amount -= Dep;				
			else
				Amount += Dep;
			}
		
		cout << "\t\t\t= " << Amount;	
		cout << "\n *** \n";	
		} while (1);
		
	// Epilog, indicate application is done and return to command prompt.
	cout << "\n\n\t\t[ DONE ]\n" << endl;
	return 0;
	}
TightCoderEx wrote:
What I'm looking for is, can this code be improved, especially in regard to optimization.
In 90% of cases you should not worry about optimization - get some good, well-designed code first, and only optimize it if you find that it is a bottleneck. Never pointlessly optimize code.

As for structural and design improvements, here are some points:
- if possible, you should avoid "using namespace std;". There are plenty of articles you can google about why - it's such a common topic that I get tired of explaining it.
- C++11 introduced new date and time classes so you don't have to deal with the old C ones - I'd recommedn learning them and trying to use them. Check out http://en.cppreference.com/w/cpp/chrono
- You will definitely want to find a better brace & indentation style - I'm having a very hard time reading your code because of it. I notice that you are trying to line things up so they look nice - this is fine, but you should only do it if the things you are lining up are relevant. Currently you are lining up irrelevant things and it is confusing as to why they are lined up.

I can't say much more because I'm having a hard time readin your code with the indentation style you're using - the point of indentation styles is to emphasize the flow of code, and yours is preventing me from seeing how your code flows. I would have to spend a lot of time analyzing it or else reformat it myself, which is not good.
Yes, coding style does seem to be a contentious issue. There seems to be as many options as there are programmers. Not having control over indentation level doesn't help either as I will admit 8 makes it unwieldy looking as compared to 2. Is this somewhat you had in mind.

1
2
3
4
5
6
7
for (int Cnt = 0; Cnt < Count; Cnt++)
{
	double Dep = Amount * Ratio;
	
	if (Deprec)
		Amount -= Dep; else Amount += Dep;
}


Good point to know about namespace. Not sure if my project will ever become that elaborate, but as one person noted, it was sure a pain in the ass going back afterwards and making the change. I would assume this is why in Stephen Prata's 6th edition "C++ Primer Plus" using namespace is in the function block now.

1
2
3
4
5
6
7
8
int main (void)
{
    using namespace std;

   .. code --

    return 0;
}
That indentation is better yes.

Also, "C++ Primer Plus" is widely considered bad, unlike the highly respected "C++ Primer". It's stupid that you have to read reviews in order to figure out if a C++ book is good, but that's the truth - there are a lot of really really bad C++ books out there. For a list of good books, check this out:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Bjarne's book looks particularly interesting, but as I'm partial to electronic media so I can have up on my dual screen desktop, or tablet when I'm travelling, I may have to settle. I'm not adversly opposed to cost, so if you know of a source in PDF, it would be appreciated.
Topic archived. No new replies allowed.