Lecture 3: C++ Examples

For this lecture we looked at three different solutions to the task below:

Task: You are working as a consultant for a cable company. For each installation that is performed by the company, there's a $25.00 service charge and an additional $2.00 charge per foot of cable used. They need a program to compute the total revenue generated by their installers per month. In other words, if they use 263 yards of cable at 27 different locations, they make $2253.00 in revenue.

Since writing a program to solve this task is your first assigment, you can not find sample solutions here. However, in the process of looking at some sample solutions in class we came up with the following style guidelines for C++ programming:

C++ Style Guidelines

Names of variables
Variables should (almost) never consist of single letters like x or k unless the variable is only useful for some very minor part of the program. Input or output variable, for example, should never consist of single letters.

Instead, you should choose names for variables that reflect their meaning. Often, that meaning comes directly from the description of the task, but sometimes you need to use additional variables. Those, too, should have self-explanatory names whenever possible.

Note that variable names can not contain spaces. They must start with letters, and can contain only letters, numbers, and certain special symbols such as an "underscore" _.

Location of constants
All constants should always be declared at or near the top of the program (or function). Other variables should be declared whenever necessary, but those, too, should be grouped together whenever that makes logical sense. But again, for constants the rule is clear: they belong at the beginning of the source code.
Use of Comments
You should comments wisely: using too many programs will make your program unreadable (just the oposite of what you want to do), while not enough comments will make your program difficult to understand.

As a rule of thumb, use comments whenever a different logical part of your program starts (such as "// defining constants" or "// defining all input variables"), and whenever something tricky is done in your program. Do not use comments for code that is self-explaining. If you use - as you should - appropriately named variables, many lines of code will be self-explanatory and would not need comments.

In short, you should use just the right amount of comments, plus the mandatory comments below.

Mandatory Comments
The following comments are mandatory for every program you turn it. They simply must be present:
   // your name, at the top of the source code
   // the address you finish the project right after your name
   // (an empty line)
   // a brief description of what your program does, one to three lines

   #include 

   int main(void)
   {
      // some stuff here
      return 0;
   }
Chaining output
Frequently, you want to output more than one variable or text. You could use several cout statements, as in:
  cout << "This is the answer: "; cout << Answer; 
However, output through the cout operator can also be chained, using multiple redirection symbols. The above two lines, for example, can - and should - be combined into one line:
   cout << "This is the answer: " << Answer; 
You can chain as many items as you like.
Control sequences
Control sequences, or escape sequences, are special characters that can be embedded in a string that does not print, but instead causes certain action. There are several of those, but the two most useful ones for now are:
\n will put the cursor at the beginning of the next line
\t will advance the cursor by one tab position

Using those sequences, you could modify the previous statement into this:

  cout << "\nThis is the answer:\t" << Answer << "\n"; 
This will place the cursor into a new line first of all, then display the text "This is an answer:", then advance the cursor by a tab, show the value of the variable Answer, and finally move the cursor to the beginning of the next line.
Chaining input (don't)
You can also chain input, in a similar way to chaining output. In other words, instead of saying:
   cin >> NumberOfInstallation;
   cin >> NumberOfYards;

you could also use

   cin >> NumberOfInstallation >> NumberOfYards;

While sometimes useful, I do not recommend doing that for simple programs.

(bgw)