Lesson 12 of 60 – Input and Output in C++
20%

Input and Output in C++

Input and output are used to communicate with the user. In C++, the standard input and output streams are commonly handled using std::cin, std::cout, and std::cerr.

Note: The <iostream> header provides the standard stream objects commonly used for console input and output.

1. What is Input?

Input means receiving data from the user or another input source. For example, a program can ask the user to enter their age.

int age;

std::cin >> age;

Here, the value entered by the user is stored in age.

2. What is Output?

Output means displaying information produced by a program.

std::cout << "Hello World";

This statement displays text on the standard output stream.

3. The iostream Header

The <iostream> header provides facilities for standard input and output.

#include <iostream>

It is commonly included when using std::cin and std::cout.

4. Using std::cout

std::cout is used to send output to the standard output stream.

#include <iostream>

int main() {

    std::cout << "Welcome to C++";

    return 0;
}

5. Output Text

Text can be displayed by placing it inside double quotation marks.

std::cout << "Hello Students";

The text inside the quotation marks is displayed as output.

6. Output a Variable

Variables can also be displayed using std::cout.

int age = 20;

std::cout << age;

The value stored in age is displayed.

7. The Insertion Operator <<

The << operator is used with std::cout to send data to the output stream.

std::cout << "Age: " << age;

Multiple pieces of data can be sent to the output stream in one statement.

8. Displaying Multiple Values

int age = 20;
double marks = 85.5;

std::cout << "Age: " << age;
std::cout << " Marks: " << marks;

The insertion operator can be chained to display multiple values.

9. New Line with std::endl

std::endl inserts a newline and flushes the output stream.

std::cout << "Hello" << std::endl;
std::cout << "Welcome";

10. New Line with \n

The newline escape sequence \n can also be used to move output to the next line.

std::cout << "Hello\n";
std::cout << "Welcome";

Unlike std::endl, writing \n does not itself request a flush.

11. The std::cin Object

std::cin is commonly used to read formatted input from the standard input stream.

int age;

std::cin >> age;

The entered value is stored in age.

12. The Extraction Operator >>

The >> operator is used with std::cin to extract formatted input.

int number;

std::cin >> number;

The input is converted to the type of the destination variable when the formatted extraction succeeds.

13. Taking Integer Input

#include <iostream>

int main() {

    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "Your age is " << age;

    return 0;
}

This program reads an integer and then displays it.

14. Taking Multiple Inputs

Multiple variables can be read using multiple extraction operations.

int age;
double marks;

std::cin >> age;
std::cin >> marks;

The user can enter the values separately or, for formatted extraction, provide them separated by whitespace.

15. Taking Multiple Values in One Statement

int age;
double marks;

std::cin >> age >> marks;

The extraction operator can be chained to read multiple values.

16. Taking Character Input

char grade;

std::cout << "Enter grade: ";
std::cin >> grade;

std::cout << "Grade: " << grade;

The character entered by the user is stored in grade.

17. Taking String Input with std::cin

For a std::string, std::cin normally reads one whitespace-delimited word.

#include <iostream>
#include <string>

int main() {

    std::string name;

    std::cout << "Enter your name: ";
    std::cin >> name;

    std::cout << "Hello " << name;

    return 0;
}

18. Reading a Full Line with std::getline

Use std::getline() when you want to read an entire line, including spaces.

#include <iostream>
#include <string>

int main() {

    std::string fullName;

    std::cout << "Enter your full name: ";
    std::getline(std::cin, fullName);

    std::cout << "Name: " << fullName;

    return 0;
}

19. std::cin and std::getline Together

When std::getline() is used after formatted extraction such as std::cin >> age, a leftover newline may be read immediately.

int age;
std::string name;

std::cin >> age;
std::getline(std::cin >> std::ws, name);

Using std::ws here consumes leading whitespace before reading the line.

20. Output Escape Sequences

Escape sequences are special character combinations used inside string or character literals.

std::cout << "Hello\nWorld";
std::cout << "Name:\tRahul";

Common examples include \n for a newline and \t for a horizontal tab.

21. Printing Boolean Values

Boolean values can be printed directly.

bool passed = true;

std::cout << passed;

By default, a Boolean value is typically displayed as 1 for true and 0 for false.

22. std::boolalpha

The std::boolalpha stream manipulator can be used to display Boolean values as true and false.

#include <iostream>

int main() {

    bool passed = true;

    std::cout << std::boolalpha << passed;

    return 0;
}

23. std::cerr for Error Messages

std::cerr is the standard error stream and is commonly used for error messages.

#include <iostream>

int main() {

    std::cerr << "An error occurred.";

    return 0;
}

24. std::clog

std::clog is the standard logging stream. It can be used for diagnostic or logging messages.

#include <iostream>

int main() {

    std::clog << "Program started.";

    return 0;
}

25. Input and Output Example

#include <iostream>

int main() {

    std::string name;
    int age;

    std::cout << "Enter your name: ";
    std::cin >> name;

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;

    return 0;
}

26. Input Failure

Formatted input can fail if the entered data cannot be converted to the requested type.

int age;

std::cin >> age;

If the user enters invalid input such as non-numeric text when an integer is expected, the stream can enter a failure state.

27. Checking Input Success

The state of an input stream can be checked using it in a condition.

int age;

if (std::cin >> age) {
    std::cout << "Valid input";
} else {
    std::cout << "Invalid input";
}

This allows a program to respond when input extraction fails.

28. Complete Input and Output Program

#include <iostream>
#include <string>

int main() {

    std::string name;
    int age;
    double marks;

    std::cout << "Enter your name: ";
    std::cin >> name;

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "Enter your marks: ";
    std::cin >> marks;

    std::cout << "\n--- Student Details ---\n";
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Marks: " << marks << std::endl;

    return 0;
}

29. Input and Output Best Practices

  • Include <iostream> when using standard streams.
  • Use std::cout for normal console output.
  • Use std::cin for formatted console input.
  • Use std::getline() when an entire line is required.
  • Use std::cerr for error messages.
  • Check input operations when invalid input is possible.
  • Use meaningful prompts so users know what to enter.

30. Input and Output – Final Summary

Stream / Feature Purpose Example
std::cout Standard output std::cout << "Hello";
std::cin Formatted standard input std::cin >> age;
std::cerr Standard error output std::cerr << "Error";
std::clog Standard logging output std::clog << "Log";
std::getline() Read an entire line std::getline(std::cin, name);
std::endl Insert newline and flush std::cout << std::endl;
\n Insert a newline std::cout << "\n";

📌 Key Points

  • Input means receiving data, while output means displaying data.
  • The <iostream> header provides standard stream facilities.
  • std::cout is commonly used for standard output.
  • std::cin is commonly used for formatted standard input.
  • The << operator sends data to an output stream.
  • The >> operator extracts formatted data from an input stream.
  • std::getline() can read an entire line, including spaces.
  • std::endl inserts a newline and flushes the stream.
  • \n inserts a newline without itself requesting a flush.
  • std::cerr is commonly used for error messages.
  • std::clog can be used for logging or diagnostic messages.
  • Input operations can fail, so programs should check input when appropriate.

🧠 Quick Quiz

Question: Which object is commonly used to take formatted input from the user in C++?