B4Coder
0%
LessonAbout 5 min

Chapter 1 · Lesson 2 of 5

02. Printing more lines

Chain several things to cout with <<, and end lines with endl or \n.

You can send several things to cout in one line, each after its own <<. A line only ends when you send endl (or \n), so two couts can print on the same line.

C++
#include <iostream>using namespace std;
int main() {    cout << "Ready, " << "steady, " << "go!" << endl;    cout << "One";    cout << " line" << endl;    cout << "Two\nlines\n";    return 0;}

It shows

Ready, steady, go!
One line
Two
lines

\t is a tab, a jump to the next column, handy for lining up a table.

C++
#include <iostream>using namespace std;
int main() {    cout << "Item\tPrice" << endl;    cout << "Idli\t30" << endl;    cout << "Dosa\t50" << endl;    return 0;}

It shows

Item	Price
Idli	30
Dosa	50

Your turn

1Steps

  1. 1Print this little timetable, with \t between the day and the subject, each on its own line:
  2. 2Monday tab Maths, then Tuesday tab Science, then Wednesday tab English.

Write it in main.cpp. If it reads input, put the answers in Program input, under the terminal.

Use: cout

Starting code

The editor starts empty. Copy this into it to begin, or write your own from scratch.

C++
#include <iostream>using namespace std;
int main() {    cout << "Monday Maths Tuesday Science Wednesday English";    return 0;}

2Expected output

It should show

Monday	Maths
Tuesday	Science
Wednesday	English

3Hints

Stuck? Open one hint at a time: each gives a bit more help.

Hint 1

Each day needs its own line, so each needs an endl (or \n) after it.

Hint 2

Between the day and its subject, use \t instead of a space.

Hint 3

The first line is cout << "Monday\tMaths" << endl; and the other two are made the same way.

Terminal

Click Run to see what your program shows, then Submit answer to check it and unlock Next.