> Also, if somebody would be so kind to offer up an example of my code with the proper indentation
> and bracket spacing (for easier reading/debugging in the future), that would be fantastic.
Just pick on a brace and indentation style that appeals to you; one that makes you feel that your code is most easily readable and maintainable if written in this style. Just be consistent with whatever style you choose.
Once you start reading real-life code, once you are past the amateur stage, you would have to read, write and maintain code written using many different styles - K&R, Allman, and so on.
http://en.wikipedia.org/wiki/Indent_style
Very soon, you would realize that code written in a consistent style is both readable and understandable; it doesn't really matter what the style is as long as it is consistent.
If you modify or extend a program that you didn't write, preserve the style that is already present in the code - the program's overall consistency of style is more important than your own predilections. And don't waste time over sterile arguments int* p vs int *p, which brace and indentation style to use etc.
If I were to write this program, it may end up look something like this; but then that is just me:
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
|
#include <iostream>
int main()
{
std::cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n\n" ;
char again ;
do
{
std::cout << "What should n be in the final term (2 - 10)? " ;
int nterms = 0 ;
std::cin >> nterms ;
if( nterms > 1 && nterms < 11 )
{
double sum = 0.0 ;
double term = 1.0 ;
const double multiplier = 1.0 / 2.0 ;
for( int n = 1 ; n <= nterms ; ++n )
{
term *= multiplier ;
sum += term ;
std::cout << "1/2^" << n << ' ' ;
if( n < nterms ) std::cout << "+ " ;
else std::cout << "== " ;
}
std::cout << sum << '\n' ;
}
else std::cout << "n must be in the range (2-10)\n" ;
std::cout << "Would you like to run this program again? (y/n): " ;
std::cin >> again ;
} while( again == 'Y' || again == 'y' ) ;
}
|