Input / output

Input and output (I/O) are fundamental operations that allow a program to communicate with the user or other systems. C++ provides several ways to handle I/O operations, primarily through the standard input and output streams.

Standard Input and Output Streams

C++ uses streams to perform input and output operations. The most commonly used streams are:

  • std::cin: Standard input stream (usually associated with keyboard input)
  • std::cout: Standard output stream (usually associated with the console)
  • std::cerr: Standard error stream (used for error messages)
  • std::clog: Standard logging stream (used for logging messages)

Including the Necessary Header

To use these streams, include the iostream header:

#include <iostream>

Using std::cout for Output

The std::cout stream is used to output data to the console. The insertion operator (<<) is used to send data to the std::cout stream.

Example

#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    int number = 42;
    std::cout << "The number is: " << number << std::endl;
    return 0;
}

Explanation

  • std::cout: Outputs data to the console.
  • <<: The insertion operator.
  • std::endl: Inserts a newline character and flushes the stream.

Formatted Output

C++ supports formatted output using manipulators from the iomanip header.

#include <iostream>
#include <iomanip>

int main() {
    double pi = 3.14159;
    std::cout << "Pi: " << std::fixed << std::setprecision(2) << pi << std::endl;
    return 0;
}

Explanation

  • std::fixed: Sets the floating-point number to fixed-point notation.
  • std::setprecision(2): Sets the precision of the floating-point number to 2 decimal places.

Using std::cin for Input

The std::cin stream is used to receive input from the user. The extraction operator (>>) is used to read data from the std::cin stream.

Example

#include <iostream>

int main() {
    int age;
    std::cout << "Enter your age: ";
    std::cin >> age;
    std::cout << "Your age is: " << age << std::endl;
    return 0;
}

Explanation

  • std::cin: Reads data from the console.
  • >>: The extraction operator.

Reading Multiple Values

You can read multiple values using a single std::cin statement.

#include <iostream>

int main() {
    int a, b;
    std::cout << "Enter two numbers: ";
    std::cin >> a >> b;
    std::cout << "The numbers are: " << a << " and " << b << std::endl;
    return 0;
}

Handling Strings

Using std::cin with Strings

The std::cin stream reads a single word into a string. To read an entire line, use the std::getline function.

Example

#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "Enter your name: ";
    std::cin >> name;
    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}

Using std::getline for Full Lines

To read an entire line including spaces, use std::getline.

Example

#include <iostream>
#include <string>

int main() {
    std::string fullName;
    std::cout << "Enter your full name: ";
    std::getline(std::cin, fullName);
    std::cout << "Hello, " << fullName << "!" << std::endl;
    return 0;
}

Using std::cerr for Error Messages

The std::cerr stream is used to output error messages. It behaves like std::cout but is typically used for errors.

Example

#include <iostream>

int main() {
    std::cerr << "An error occurred!" << std::endl;
    return 0;
}

Using std::clog for Logging

The std::clog stream is used for logging messages. It also behaves like std::cout.

Example

#include <iostream>

int main() {
    std::clog << "Logging information..." << std::endl;
    return 0;
}

Manipulators for Formatting Output

C++ provides several manipulators for formatting output, available in the iomanip header.

Example

#include <iostream>
#include <iomanip>

int main() {
    int width = 10;
    std::cout << std::setw(width) << "Right" << std::endl;
    std::cout << std::setw(width) << std::left << "Left" << std::endl;
    std::cout << std::setw(width) << std::right << "Right" << std::endl;
    return 0;
}

Explanation

  • std::setw(width): Sets the field width for the next insertion operation.
  • std::left: Left-justifies the output.
  • std::right: Right-justifies the output.

Best Practices

  1. Check Input Validity: Always validate user input to ensure it meets expected formats.
  2. Handle Errors: Use std::cerr to display error messages and handle exceptions properly.
  3. Flush the Output: Use std::endl or std::flush to ensure output is written to the console immediately.
  4. Use std::getline for Strings: Use std::getline to read entire lines of text to avoid input issues with spaces.