A List of Traps and Tips

◀ How Do You Create a Char Array with Other Variables?▶ Resources
Amazon In Chapter 16 you’ve seen general traps and tips. They are related to the C++ language and need more explanations. Here I’ve come up with a list of smaller tips and traps. It doesn’t mean they are any less important than Chapter 16. It just means tips here are shorter and easier to describe in a paragraph.

You should familiarize yourselves with all these tips and traps!

  • It is advisable to put the prototypes of all functions and classes right after the header. If you do not want to do it, you need to make sure when you scan your program from top to bottom, you do not see any reference to a function or a class not defined prior to the current point. For example, if function A uses function B, then function A must be placed after function B.
  • If you declare an array of strings like “string s[100]”, they all are initialized to “”.
  • The keyword “break” works only within a loop or switch statement. In the context of a loop, a “break” will exit the nearest loop no matter where the current control. For example, if you have 10 nested if statements within a for loop and a “break” is placed in the innermost if statement, when control reaches that “break”, it will exit the for loop.
  • In the condition of a flow control statement, only 0, or NULL, evaluates the condition to false. Other values, including negative integers, all evaluate the condition to true.
  • A container variable (vector, queue, stack, etc) and an object of a class are passed by value implicitly, “&” can be added for it to be passed by reference.
  • Omission of a semicolon can be a deadly bug, sometimes even difficult to track down. If compiler spits out a weird error, look at the line above the one it says where the error originates and see if you miss a semicolon. Incidentally, a class declaration and a structure both end with a semicolon.
  • Implicit conversion takes place between an int and a char. For example, if you do int a = ‘a’ then cout << a, you get 97. If you do char b = 38 then cout << b, you get &. In fact, a char represents a byte, or 8 bits.
  • When you declare a pointer to char, always make sure it is NULL terminated.
  • If you want to test whether score is greater than 80 and lower than 100, you do if(score > 80 && score < 100). If you do if(80 < score < 100), the compiler will not catch this error because it still is valid C++ syntax. However, the previous expression means if( (80 < score) < 100). In this case, (80<score) is either true, or 1, or false, or 0. In any case, it is less than 100. So the condition is always true.
  • C++ and C both have a goto statement. However, using it usually is a bad idea, and you should use structured controls such as if else, continue, break, to control program flow.
  • There are many ways to test for the end of input, and while(cin) is the simplest one. It coves more types of failures than other functions such as while(!cin.eof()) and while(!cin.fail()).
  • The <cctype> library provides many character-related functions such as determining whether a character is letter or a digit or a control character. Take advantage of that library.
  • Use close() to close a file after you finish writing or reading it; if you don’t, your program may not work correctly. Some implementation requires clear() before you use close().

Next let’s look at what resources you have at the tip of your fingers!
◀ How Do You Create a Char Array with Other Variables?▶ Resources

Questions? Let me know!