Skip to content

Latest commit

 

History

History
113 lines (86 loc) · 4.65 KB

FirstDebug.md

File metadata and controls

113 lines (86 loc) · 4.65 KB

What's News

SpaceX, Blue Origin and other space launch companies continue to look for new ways to power their rockets. The less volatile the fuel, the longer a ready-to-launch rocket can sit on the pad safely. Methane could be the answer.

One of These Things is Not Like the Other

To begin understanding the C++ language, let's work backward from some broken source code, decipher its intent and then correct it so that it performs as intended. The programmer in our example is attempting to give us directions to some secret treasure that they bequeathed to us. They want us to find what they left but it's hard to say whether they left the proper instructions:

#include <iostream>
/*
 * Follow the instructions printed on the screen to find the treasure.
 */
int main() {
  std::cout << "1. Orient your compass properly and count to 10";
  std::cout << "2. Next,\n";
  std::cout << "sail East toward the land";
  std::cout << "fill methane tank." << std::endl
  std::cout << "3. Lift off in the rocket ship.\n";
  return 0;
}

The first thing to notice is a missing ; at the end of the statement instructing us to fill a methane tank. Statements, complete instructions that define a computation, must end in a semicolon. In C++, most statements must end in a semicolon.

The great thing about this error was that we could discover it with the help of the compiler. Any error that the compiler can detect is known as a compile-time error because it is an error that happens, well, at compile time. Of course, this statement begs the question -- Just what is compile time? Compile time is when the programmed is compiled.

Here is the corrected version of our code:

#include <iostream>
/*
 * Follow the instructions printed on the screen to find the treasure.
 */
int main() {
  std::cout << "1. Orient your compass properly and count to 10";
  std::cout << "2. Next,\n";
  std::cout << "sail East toward the land";
  std::cout << "fill methane tank." << std::endl;
  std::cout << "3. Lift off in the rocket ship.\n";
  return 0;
}

If there is a compile time, then there must be another time ... the time when the program is executed. We call that the run time. With the fix in (i.e., the addition of the missing ;), our code now compiles. That's half the battle. Now we need to determine what happens when the code runs.

If we run the compiled code shown above, the output (perhaps unexpectedly) looks like

1. Orient your compass properly and count to 102. Next,
sail East toward the landfill methane tank.
3. Lift off in the rocket ship.

Only the author can say for sure whether or not that constitutes the proper path to the treasure. However, it certainly does not look like what they intended, especially given the way that the source code is formatted.

Although the code compiles and runs, it does not seem entirely correct. The mistake seems to appear when the program runs. We call these types of errors run-time errors.

It is my guess that the author probably intended their message to read something more like:

1. Orient your compass properly and count to 10
2. Next,
sail East toward the land
fill methane tank.
3. Lift off in the rocket ship.

Let's see whether we can correct our bequeather's bumbles. We can add formatting information to the output so that the lines break in the proper spots. There are two ways to include information in a C++ program about where to create new lines in the output: the newline escape sequence (\n) and std::endl;

To use a newline escape sequence, just embed it in the string literal you are printing to the screen:

  std::cout << "1. Orient your compass properly and count to 10\n";

To use a std::endl, you will need to use another <<:

  std::cout << "sail East toward the land";

Notice how you can output something that is not a string literal, i.e. the std::endl, at the same time that you are outputting the string. We will rely on this functionality later when we output the contents of variables!

Here is our corrected code:

#include <iostream>

/*
 * Follow the instructions printed on the screen to find the treasure.
 */
int main() {
	std::cout << "1. Orient your compass properly and count to 10\n";
	std::cout << "2. Next,\n";
	std::cout << "sail East toward the land" << std::endl;
	std::cout << "fill methane tank." << std::endl;
	std::cout << "3. Lift off in the rocket ship.\n";
	return 0;
}

With the changes, let's confirm that we have fixed the author's runtime errors:

1. Orient your compass properly and count to 10
2. Next,
sail East toward the land
fill methane tank.
3. Lift off in the rocket ship.

Looks like the only thing left is to claim our reward!