C++
  Home arrow C++ arrow C++ Programmer Alerts
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

C++ Programmer Alerts
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2008-04-02

    Table of Contents:
  • C++ Programmer Alerts
  • Function Basics and Libraries
  • List
  • Pointers and Dynamic Memory

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C++ Programmer Alerts


    (Page 1 of 4 )

    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.

    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.

    More C++ Articles
    More By Chrysanthus Forcha


       · If you have been having mysterious problems when coding in C++, then this article...
     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class
    - Iostream Library and Basic I/O in C++
    - Introduction to Streams







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT