Some more things:
Try to avoid global variables - they will become a nuisance one day. This is part of keeping the scope of variables as small as possible. Remove the
static
as this has the effect of making them global anyway.
Learn to write your code in terms of functions: at the moment you could have 2. One to print the initial text, another to print out the results of the calculations. Even though these functions are only called once each, doing so aids in understanding and is a form abstraction. There is a kind of unwritten rule that functions should never be more 40 Lines Of Code (LOC) - some go for even less, one can always split a function into parts by having more functions. Sometimes it's worth it to have a function even if it is only a few lines, it's not only to avoid writing code more than once. Remember to put function
declarations before main, with the function
definitions after main.
Also very helpful are meaningful names for variables and functions. If done well, the code by itself should tell a story of what is happening - without having too many comments. Use comments to say
why code is doing something, as well as pre and post-conditions and expected valid ranges of values, or anything else that might actually be useful. Here is a comical version of what I mean by good names and story telling:
http://www.cplusplus.com/forum/lounge/176684/#msg872881
Another thing is to think about the types for your variables. For example
num_years
should never be negative, so make it
unsigned
. However be careful mixing signed and
unsigned
calculations, as there are different ways of representing signed ints - this can cause problems. So the thing to do there is make
count
unsigned as well, so the
for
loop uses the same type throughout.
As everyone has mentioned, it's best in the end to just put
std::
before each std thing. I used to do
using std::cout;
but that gets tiresome as well - if one is using various STL containers and algorithms, it becomes a pain to put 20 of them at the beginning of a file every time.
Finally, while we are on namespaces: It's a good idea to put all of your own code into it's own namespaces ; yet another way to restrict scope. It's possible to have more than one namespace and they can be nested. Perhaps it's a bit trivial here (but still not unreasonable) - it is more important when you get to writing your own classes. For example you could have this in a header file - I like to use *.hpp for header file extensions - it means header file with cpp code inside:
CountryClub.hpp
1 2 3 4
|
namespace CountryClub {
double increase = 0.0;
constexpr unsigned int num_years = 6;
} //end of CountryClub namespace
|
before main:
#include "CountryClub.hpp"
And then in main:
namespace CC = CountryClub; // an alias to make things easier
Then refer to the variables like this:
for(unsigned int count = 0; count < CC::num_years; ++count) {
As I say this is way over the top for now, just a good idea to mention things early on :+)
Good Luck !!