Skip to main content

Exercises 2.12 Exercises

At the end of each chapter, we include exercises you can do with the things you’ve learned. You can’t learn to program only by reading about it; you have to practice.

1.

Take a look at the following program. How many lines of output will be produced?
int main() {
  cout << "The current time is: " << endl;
  cout << 7;
  cout << ":" << 50;
  cout << endl;
  cout << "I'm going to be late for my 8am!";
}
  • There are 5 cout statements, but that doesn’t mean there are 5 lines of output!
  • Remember that endl is what makes a new line!
  • Even though there are 5 cout, there are only 3 lines of output in the terminal.
  • There are 2 endl statements. But what happens when you have more output after the endl?
  • 0 (There is an error!)
  • No errors here.

2.

Construct a block of code that prints: β€œLions &” one the first line, β€œTigers & Bears!” on the second line, and β€œOh my!” on the FOURTH line.

3.

4.

Complete the code so that it prints Hello! on one line and then I love programming! on the next.
You have attempted of activities on this page.