Back to Course

Data Structures and Algorithms with C++

0% Complete
0/0 Steps

C++ is a powerful, general-purpose programming language that provides an easy way to write code for a variety of tasks. It is widely used in many areas such as game programming, robotics, data mining, and embedded systems. One of the most important aspects of programming with C++ is understanding how to use basic input and output (I/O) techniques. In this article, we will discuss the fundamentals of input and output in C++, and provide coding exercises to help you practice your new skills.

What is Input and Output?

Input and output (I/O) are the two basic operations for any program. Input is information that is sent to the program from an outside source, such as a user or a file. Output is the result of a program’s execution, and is usually sent to an outside destination, such as a file or printer.

I/O operations allow us to interact with the outside world. Input can come from any source, including the keyboard, a file, or a network connection. Output can be sent to any destination, including the screen, a file, or a network connection.

Input and output operations in C++ are handled using streams, which are objects that allow programs to send and receive data. Streams can be connected to files, the keyboard, or other external devices.

Basic Input and Output in C++

In C++, I/O operations are accomplished using streams. A stream is an abstract object that provides an interface for reading and writing data. Streams can be connected to files, the keyboard, or other external devices.

Streams are classified as either input streams or output streams. An input stream is used for reading data from an external source, such as a file or the keyboard. An output stream is used for writing data to an external destination, such as a file or the screen.

C++ provides several predefined streams for performing I/O operations. The most commonly used streams are:

  • cin: Standard input stream
  • cout: Standard output stream
  • cerr: Standard error stream
  • clog: Standard log stream

The cin Stream

The cin stream is a predefined input stream that is connected to the keyboard. It can be used to read data from the keyboard and store it in a variable.

To use the cin stream, we must include the <iostream> header file in our program. This header file contains the declarations for the standard input/output functions.

Once we have included the header file, we can use the cin stream to read data from the keyboard. The syntax for using the cin stream is as follows:

cin >> variable_name;

The cin stream will read a value from the keyboard and store it in the specified variable. For example, to read an integer from the keyboard and store it in a variable named x, we could use the following code:

int x;
cin >> x;

The cout Stream

The cout stream is a predefined output stream that is connected to the screen. It can be used to write data to the screen.

To use the cout stream, we must include the <iostream> header file in our program. This header file contains the declarations for the standard input/output functions.

Once we have included the header file, we can use the cout stream to write data to the screen. The syntax for using the cout stream is as follows:

cout << expression;

The cout stream will evaluate the expression and print the result to the screen. For example, to print the value of the variable x to the screen, we could use the following code:

cout << x;

Formatting Output with the cout Stream

The cout stream can be used to format output. We can use the stream manipulators to control how the output is displayed. For example, we can use the setw() manipulator to set the width of the output field, and the setfill() manipulator to set the character used to pad the output field.

For example, to print the value of the variable x as a 5-digit number with leading zeros, we could use the following code:

cout << setw(5) << setfill('0') << x;

This code would print the value of x as a 5-digit number, with leading zeros if necessary.

The cerr and clog Streams

The cerr and clog streams are predefined output streams that are connected to the screen. They are used for printing error messages and logging information, respectively.

To use the cerr or clog streams, we must include the <iostream> header file in our program. This header file contains the declarations for the standard input/output functions.

Once we have included the header file, we can use the cerr or clog streams to write data to the screen. The syntax for using the cerr or clog streams is identical to that of the cout stream.

Conclusion

In this article, we have discussed the fundamentals of input and output in C++. We have seen how to use the cin, cout, cerr, and clog streams to read and write data. We have also seen how to use stream manipulators to format output.

Now that you have a basic understanding of input and output in C++, it’s time to practice your new skills with the following coding exercises.

Exercises

Write a program that reads two integers from the keyboard and prints the sum of the numbers to the screen.

#include <iostream>
using namespace std;

int main()
{
    int x, y;
    cout << "Enter two integers: ";
    cin >> x >> y;
    cout << "The sum of the numbers is: " << x + y << endl;
    return 0;
}

Write a program that reads five numbers from the keyboard and prints the average of the numbers to the screen.

#include <iostream>
using namespace std;

int main()
{
    int x, y, z, a, b;
    cout << "Enter five numbers: ";
    cin >> x >> y >> z >> a >> b;
    cout << "The average of the numbers is: " << (x + y + z + a + b) / 5.0 << endl;
    return 0;
}

Write a program that reads a number from the keyboard and prints it to the screen as a five-digit number with leading zeros.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int x;
    cout << "Enter a number: ";
    cin >> x;
    cout << "The number as a five-digit number with leading zeros is: ";
    cout << setw(5) << setfill('0') << x << endl;
    return 0;
}

Write a program that reads a string from the keyboard and prints it to the screen in uppercase.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string s;
    cout << "Enter a string: ";
    cin >> s;
    for (int i = 0; i < s.length(); i++)
    {
        s[i] = toupper(s[i]);
    }
    cout << "The string in uppercase is: " << s << endl;
    return 0;
}

Write a program that reads a number from the keyboard and prints it to the screen as a hexadecimal number.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int x;
    cout << "Enter a number: ";
    cin >> x;
    cout << "The number in hexadecimal is: " << hex << x << endl;
    return 0;
}