Skip to main content
Contents Index
Dark Mode Prev Up Next Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
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
?
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.
int main() {
---
cout << "Lions &" << endl;
---
cout << "Lions &"; #paired
---
cout << "Tigers &";
---
cout << "Tigers &" << endl; #paired
---
cout << " Bears!" << endl;
---
cout << "Bears!" << endl; #paired
---
cout << endl;
---
cout << "Oh my!";
---
}
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.