Introduction to C++
Jonah Warrenjonah AT parsons DOT edu
http://www.feedtank.com/2005/cpp
Everyone gets stuck... what to do about it.
- Look at your error messages.
- Look very carefully at your code.
- Write clean code - keep a consistent style.
- Print lines to the screen at various points in your program.
- Comment out code to track down bugs.
- Play around. Try something else.
- Make small changes at a time.
- Go do something else and come back later.
- Be patient.
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.
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.
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;}
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.
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;
}
If your program doesn't compile, comment out code that you
think might be causing the problem and try compiling again.
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.
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...
Brains can just get stuck Sometimes it takes walking away from the
problem and coming back to it later.