C++ Basic Input/Output


Introduction

In C++, input and output (I/O) operations are fundamental for interacting with users or handling data. The cin object is used to take input from the user, and the cout object is used to display output to the screen. Understanding how to use these two objects effectively is crucial for creating interactive and dynamic programs.

In this post, we'll cover:

  • Using cin for Input
  • Using cout for Output
  • Formatting Output
  • Common Input and Output Operations

1. Using cin for Input

In C++, the cin object is used to accept input from the user. It is part of the iostream library and is typically used in conjunction with the extraction operator (>>).

Syntax:

cin >> variable;

Example:

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";   // Prompt for user input
    cin >> age;                   // Take user input and store it in 'age'
    cout << "You are " << age << " years old." << endl;   // Display output
    return 0;
}

In this example:

  • The program prompts the user to enter their age using cout.
  • The user inputs the age, which is stored in the variable age using cin.
  • The program then prints the entered age using cout.

Key Points:

  • cin is used to receive input from the user.
  • The >> operator is used to extract the data from the input stream and assign it to the variable.

2. Using cout for Output

In C++, the cout object is used to display output to the screen. It is also part of the iostream library and is typically used in conjunction with the insertion operator (<<).

Syntax:

cout << expression;

Example:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;  // Output message to screen
    return 0;
}

In this example, "Hello, World!" is printed to the screen using cout.

Key Points:

  • cout is used to display output to the console.
  • The << operator is used to insert data into the output stream.

3. Formatting Output

C++ allows you to format output in various ways, such as controlling the width of output, setting precision for floating-point numbers, and displaying special characters like newlines.

3.1 Newline and Space with endl

You can use endl to insert a newline in your output. It moves the cursor to the next line, which helps in displaying clean output.

Example:
#include <iostream>
using namespace std;

int main() {
    cout << "Hello" << endl;  // Output "Hello" with a newline
    cout << "World!" << endl;  // Output "World!" on the next line
    return 0;
}

3.2 Manipulating Width and Precision

You can control the width and precision of output using the setw and setprecision manipulators. To use these, you need to include the iomanip library.

  • setw(n): Specifies the width of the output field (e.g., setw(10) ensures the output takes up 10 characters).
  • setprecision(n): Specifies the number of decimal places for floating-point numbers.
Example:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double pi = 3.14159265358979;
    cout << "Value of pi: " << setprecision(5) << pi << endl;   // Set precision to 5 digits
    cout << "Formatted output with setw: " << setw(10) << pi << endl;   // Set field width to 10
    return 0;
}

Key Points:

  • setw and setprecision allow for controlling the width and precision of numeric output.
  • endl is used to insert a newline in the output.

4. Common Input and Output Operations

4.1 Reading Multiple Inputs

You can read multiple inputs in a single statement using cin. Just separate the inputs with spaces or newlines, and cin will store them in corresponding variables.

Example:
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;   // Read two integer values
    cout << "Sum: " << a + b << endl;  // Display sum
    return 0;
}

In this example, the user is prompted to enter two numbers, and the program calculates and displays their sum.

4.2 Reading Strings

You can also read strings using cin. However, by default, cin will stop reading input when it encounters a space. If you want to read a full line (including spaces), use getline().

Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    cout << "Enter your name: ";
    getline(cin, name);  // Read an entire line, including spaces
    cout << "Hello, " << name << "!" << endl;  // Greet the user
    return 0;
}

In this example, getline(cin, name) is used to read the full name of the user, including spaces.

Key Points:

  • cin can be used to read various data types, including integers, floating-point numbers, and strings.
  • getline(cin, string) is used to read a full line of text, including spaces.