Search⌘ K

Outputs and Strings

Learn how to use std::cout in C++ to print a multi-line static invoice header using newline and tab escape characters.

The project

You’ve probably gone shopping more times than you can count.

You walk into the store, choose what you need, bring everything to the counter, and watch as the cashier scans each item. The prices appear on the screen, and then the receipt is printed. That receipt isn’t random; it’s generated by a program that instructs the system on exactly what to display and in what order.

This type of program is known as a point-of-sale (POS) system.

In this lesson, we’ll create our own small version of that system. No calculations have been made yet, and no data is being stored; instead, the receipt text is printed exactly as it should appear on the screen.

This is what the receipt looks like:

You’re the developer responsible for generating this output.

The programming concept

The concept for today is output using cout, C++’s way of displaying messages, numbers, or text on the screen. C++ already knows this function, so you don’t need to define it; you just use it.

Here’s your first example:

C++
cout << "Hello, World!" << endl;

When you run it, the text appears exactly as written. cout sends text to the console, and << endl; moves the cursor to a new line.

Congratulations. You are officially a programmer. This is an age-old tradition in the programming community: start learning a new programming language by printing “Hello, World” on the screen!

The significance of the concept in the project

Your invoice is made entirely of text.

Without cout, there would be nothing to display. Every line from the store title to the total is printed using a sequence of cout statements. If you look closely at our receipt below, it consists only of lines of text and numbers. Without cout(), nothing would appear on the screen.

For this first version of our point-of-sale app, each line of the receipt will be produced by a separate cout() statement.

Concept at a glance:

  • cout is short for console output.

  • Each line ends with << endl; or \n for a new line.

  • Escape characters like \" let you print quotes, and \t adds spacing between columns.

  • You can print numbers and text in the same line by chaining them with <<.

Learn to code for the concept

Let’s print the first two lines of your invoice:

C++
cout << "=== MART RECEIPT ===" << endl;
cout << "Cashier Name: Alex" << endl;

Run this code, and you’ll see:

=== MART RECEIPT ===
Cashier Name: Alex

To match the full receipt, we just keep adding more cout statements for each line.

Learn to code with AI

If you’re working with an AI assistant, you can simply tell it:

Write a C++ program that prints this exact receipt using only cout statements:
=== MART RECEIPT ===
Cashier Name: Alex
Items Purchased:
Milk (1): 4
Bread (1): 2
Eggs (12): 0.5 x 12 = 6
Total bill: 4 + 2 + 6 = 12

The AI will give you working code immediately, ready to run.

Generate code with AI

Here is the Copilot, where you can write your prompt and get it to generate code for you.

Powered by AI
5 Prompts Remaining
Prompt AI WidgetOur tool is designed to help you to understand concepts and ask any follow up questions. Ask a question to get started.
AI Copilot

Test code with AI

Once the AI generates your code, copy and paste it into the coding widget, run it, and then ask the AI Mentor for feedback. If the output doesn’t appear correctly, adjust your prompt or fix any minor formatting issues (such as a missing endl), and test it again.

When you look at the full version of the code generated by your AI Copilot, you’ll notice that all your cout statements are wrapped inside a specific structure like this:

#include <iostream>

using namespace std;

int main() {

// Your cout statements come here

return 0;

}

That’s how real C++ programs are written. C++ is a structured programming language, and every runnable program begins with a main() function—it’s like the starting point of a story, where the computer begins reading your code.

For now, our focus will stay on understanding how cout works to display text on the screen. We’ll let AI handle the “boilerplate” structure (the #include, using namespace std;, and main() parts) while we concentrate on writing expressive and well-formatted output lines.

That’s our philosophy for learning C++ in the age of AI—focus on logic and clarity first, and let the AI assist you with setup details.

C++
#include <iostream>
using namespace std;
int main() {
// Write your code here
return 0;
}

Food for thought:

  • What happens if you try to print some text without quotes, e.g., cout(Hello)?

  • What happens if you try to print some text without parentheses, e.g., cout Hello?

  • If it’s text, you must put it in quotes: “Hello!”

Practice makes perfect

Wonderful. You’ve managed to execute a successful C++ program that meets your client’s requirements. But the code was generated by AI; let’s see if you’ve learned a simple enough concept. You have to use the concept you’ve learnt to solve a similar problem.

Problem

Print the following receipt exactly as shown, using only cout statements:

=== TECH STORE RECEIPT ===
Cashier Name: Jamie
Items Purchased:
Keyboard (1): 2500
Mouse (1): 800
Headset (1): 3200
Total bill: 2500 + 800 + 3200 = 6500

Code its solution

The final output should match the sample output shown above:

C++
#include <iostream>
using namespace std;
int main() {
// Write your code here
return 0;
}

Perfect! Your POS app has now successfully launched and is ready to ship to customers.