Home arrow C++ arrow C++ Programmer Alerts
C++

C++ Programmer Alerts


In this article I show you how you can avoid common programming errors in C++. There will be examples (including code) that you can use to practice on your own. Are you interested? Then click on the link and start reading.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
April 02, 2008
TABLE OF CONTENTS:
  1. · C++ Programmer Alerts
  2. · Function Basics and Libraries
  3. · List
  4. · Pointers and Dynamic Memory

print this article
SEARCH DEVARTICLES

C++ Programmer Alerts
(Page 1 of 4 )

FUNDAMENTALS

Briefly, here are some fundamental points that show good style:

  • Start the program with a short statement of what the program does.

  • List the author(s) of the programs.

  • Give the date the program was written.

  • Use comments and blank lines to delineate sections of the programs.

CONTROL CONSTRUCTS

Assignment and Equality Operators

A common programming error is the confusion between the == and = operators, especially by “newbies.”

Instead of writing, 


j==5


for example, a programmer may write


j=5


The first expression is an equality expression and is true whenever j is 5. The second expression is an assignment expression where j is assigned to 5.

Rounding Errors

Be careful when using the equality operators ( == , != ) with floating point types. The finite precision of floating-point types allows round-off errors to be introduced with repeated operations. Suppose we have the following definition:


float Total = .1+.1+.1+.1+.1+.1+.1+.1+.1+.1;


The following expression, which is mathematically true, will not likely be true within the program.


Total == 1.0


Instead of directly testing for equality or inequality, we should check if the two values (from the above expressions) are sufficiently close to each other. This checking is done by specifying a maximum error tolerance and verifying that the absolute value of the difference is smaller than the tolerance. For example, suppose the maximum error tolerance is specified by the following constant:


Const float Delta = 0.0001;


There is a math library function, fabs(), that returns the absolute value of its floating point parameter. You can use this function to detect if the two values are sufficiently close as follows:


if (fabs(Total – 1.0) <= Delta) {…}


A value of True shows that the two values are sufficiently close.


Faulty While Assumption

Consider the following code:


int Position;

while (cin >> Position)

{

cout << “Extract another value” endl;

}

cout << “Last input: ” << Position;


“Newbies” often make the mistake of assuming that the body of the while loop is always executed at least once. It is important to realize that if the while loop initially comes back false, then its while body is never executed. For the above code, if there is no more data in the standard input stream before the code segment begins, then the insertion statement would display an object that has never been explicitly set.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials