Geek Slack

Learn C++
About Lesson


C++ Output (Print Text)


C++ Output (Print Text)

In C++, printing text to the console is done using the standard output stream, which is represented by std::cout. This chapter will guide you through the basics of outputting text in C++ and provide examples to illustrate different scenarios.

1. Basic Output

The simplest way to print text in C++ is to use the std::cout object along with the insertion operator <<. Here’s an example:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Explanation:

  • #include <iostream>: Includes the input-output stream library needed for std::cout.
  • std::cout << "Hello, World!" << std::endl;: Prints “Hello, World!” followed by a newline.

2. Printing Variables

You can also print the value of variables using std::cout. Here’s an example:

#include <iostream>

int main() {
    int age = 25;
    std::cout << "I am " << age << " years old." << std::endl;
    return 0;
}

Explanation:

  • int age = 25;: Declares an integer variable age and assigns it the value 25.
  • std::cout << "I am " << age << " years old." << std::endl;: Prints the string “I am “, the value of age, and the string ” years old.” followed by a newline.

3. Formatting Output

C++ allows you to format your output in various ways. Here are some examples:

3.1. Printing Multiple Lines

#include <iostream>

int main() {
    std::cout << "Line 1" << std::endl;
    std::cout << "Line 2" << std::endl;
    std::cout << "Line 3" << std::endl;
    return 0;
}

3.2. Using Escape Sequences

#include <iostream>

int main() {
    std::cout << "Hello, World!\n";  // Newline
    std::cout << "Hello,\tWorld!" << std::endl;  // Tab
    std::cout << "Hello, \"World!\"" << std::endl;  // Double quote
    return 0;
}

4. Combining Text and Variables

Combining text and variables in your output can be very useful. Here is an example:

#include <iostream>

int main() {
    std::string name = "John";
    int age = 30;
    std::cout << "Name: " << name << ", Age: " << age << std::endl;
    return 0;
}

Explanation:

  • std::string name = "John";: Declares a string variable name and assigns it the value “John”.
  • int age = 30;: Declares an integer variable age and assigns it the value 30.
  • std::cout << "Name: " << name << ", Age: " << age << std::endl;: Prints “Name: “, the value of name, “, Age: “, and the value of age followed by a newline.

5. Outputting Boolean Values

When printing boolean values, C++ outputs 1 for true and 0 for false by default:

#include <iostream>

int main() {
    bool flag = true;
    std::cout << "Flag is " << flag << std::endl;
    return 0;
}

To print true and false as text, you can use the std::boolalpha manipulator:

#include <iostream>

int main() {
    bool flag = true;
    std::cout << std::boolalpha;
    std::cout << "Flag is " << flag << std::endl;
    return 0;
}

6. Outputting Floating-Point Numbers

C++ provides ways to control the precision and format of floating-point numbers:

#include <iostream>
#include <iomanip>

int main() {
    double num = 123.456789;
    std::cout << "Default: " << num << std::endl;
    std::cout << "Fixed: " << std::fixed << num << std::endl;
    std::cout << "Scientific: " << std::scientific << num << std::endl;
    std::cout << "Precision 2: " << std::setprecision(2) << num << std::endl;
    return 0;
}

Conclusion

This chapter covered the basics of outputting text in C++ using std::cout. By mastering these techniques, you can effectively display information and debug your programs. Experiment with different formats and manipulators to enhance your output capabilities.

Join the conversation