/* This program .... Author: Last Modified: Known bug: */ #include ... using namespace std; /* Function declarations (comments and function heads) go here.... */ /* swap two int variables' value @param v1, v2: the two int variables post condition: v1 and v2's values are swapped */ void swap (int & v1, int & v2); int main() { .... } /* Function definition (function comments, headers and bodies) go here ... */ // swap v1 and v2's value void swap (int & v1, int & v2) { int tmp; // local variable used in the swapping tmp = v1; v1 = v2; v2 = tmp; }
const double PRICE_MEDIUM_PIZZA=12.39;
The following are the indentation rule.
In the following program, each statement's indentation level is marked in the comment:
#include <iostream> using namespace std; int main() //indentation level 0 { // take a line, and align with the function header and matching } int a, b; //level 1 // enter value of a and b cout <<"Enter a and b:"; //level 1 cin >> a >> b; //level 1 // calculate the GCD (greatest common division of a and b while( 1 ) //level 1 { //opening brace take a line, align with while and the matching } a = a % b; //indentation level 2 if ( a == 0 ) //level 2 return b; //level 3, yes_statement of if, indented by one more level b = b % a; //level 2 if ( b == 0 ) //level 2 return a; //level 3 } // align with matching } } // align with matching {
You are strongly encouraged to pay attention to indentation level in the first place, as doing so will help you see the logic structure of your code, and minimze chances of making mistakes. The editor emacs can help you apply indentation rule. While in emacs, if you press tab key, emacs automatically adjusts the indentation level of the current line (where cursor locates). To enable this, and to set the indentation level to 3, please type the following command at the storm terminal, which copies my emacs configuration file to your home directory.
cp ~zhang/.emacs ~
If you haven't paid enough attention to style when you first type your program, but want to go back to fix indentation (in order to understand some logic or syntax errors). There is the following quick fix. The following command can be used to fix the indentation of a program. Note, run this program from a command line (terminal).
$indent lab4.cppThis command will read lab4.cpp and adjust indentation for every lines in the program.