Introduction to C++

Jonah Warren
jonah AT parsons DOT edu
http://www.feedtank.com/2005/cpp

Everyone gets stuck... what to do about it.


  1. Look at your error messages.
  2. They can tell you a lot. Learn how to read them, and what messages you get when. They will tell you the line of code that is incorrect as well as what you did wrong.

  3. Look very carefully at your code.
  4. Look at the correct syntax. Look at your code. Look at the correct syntax. Is that a parentheses or is that a curly bracket? Missing a semicolon? Remember those games you played when you were a kid, where you had to find whats wrong with this picture? Yeah.

  5. Write clean code - keep a consistent style.
  6. Make the error finding game easier on your eyes by being consistant with spacing and formatting your code. If you like writing your if statements like this:
    	if (score == 100) {
    		gameState = 2;
    	}
    	
    Don't write your next if statement like this:
    	if (score < 100)
    	{
    		gameState = 1;}
  7. Print lines to the screen at various points in your program.
  8. If your program is not functioning properly, use cout to print values and messages to the screen.

    Lets say you made a game, where after you score 100 points, the game should print "You win!" to the screen. If the game mysteriously never prints "You win!," you may want to print out the value of the score before your if statement... like so.
    	cout << score << endl;
    
    	if (score == 100) {
    		gameState = 2;
    	}
    	
  9. Comment out code to track down bugs.
  10. If your program doesn't compile, comment out code that you think might be causing the problem and try compiling again.

  11. Play around. Try something else.
  12. Even if its not what you want to accomplish, play around. Its better than just staring at it, and you might discover something. You won't break it.

  13. Make small changes at a time.
  14. Compile as you go. Make a small change to your program, and then compile and run your program to make sure it works and to see what happens. Don't try to write a whole program and then pray it compiles. Baby steps...

  15. Go do something else and come back later.
  16. Brains can just get stuck Sometimes it takes walking away from the problem and coming back to it later.

  17. Be patient.