allizwell.jobsdoor360.in

Basic Input and Output

CS & IT / Data Structures and Algorithms / Basic Input and Output

Basic Input and Output in C++

Input and output are basic operations in every programming language. Input allows a program to receive data from the user, while output allows it to display information on the screen.

In C++, input and output operations are commonly performed using the cin and cout objects provided by the iostream library.

1. What Is Input and Output?

Input

Input means providing data to a program so that it can perform operations using that data.

For example, entering two numbers that a program needs to add.

Output

Output means displaying the result or information generated by a program.

For example, displaying the sum of two numbers on the screen.

2. The iostream Library

To perform basic input and output operations in C++, we include the iostream header file.

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";

    return 0;
}

The statement #include <iostream> makes input and output features available to the program.

It provides commonly used objects such as:

  • cin – used to take input.
  • cout – used to display output.
  • cerr – used to display error messages.
  • clog – used for general logging messages.

3. Output Using cout

The cout object is used to display information on the standard output, usually the screen.

It uses the insertion operator (<<) to send data to the output stream.

Example 1: Displaying Text

#include <iostream>
using namespace std;

int main() {
    cout << "Welcome to C++";

    return 0;
}

Output:

Welcome to C++

Example 2: Displaying Numbers

int age = 20;

cout << age;

Output:

20

Example 3: Displaying Multiple Values

int age = 20;
float marks = 85.5;

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

Output:

Age: 20
Marks: 85.5

We can use multiple << operators in the same statement to display different values.

4. New Line Using endl and \n

Sometimes we want the output to appear on separate lines. C++ provides two commonly used ways to move to a new line.

Using endl

cout << "Hello" << endl;
cout << "World";

Output:

Hello
World

endl inserts a new line and flushes the output stream.

Using \n

cout << "Hello\n";
cout << "World";

Output:

Hello
World

\n inserts a new line without explicitly flushing the output stream. It is often preferred when frequent output is involved.

5. Input Using cin

The cin object is used to take input from the user. It uses the extraction operator (>>) to read data.

Example 1: Taking an Integer Input

#include <iostream>
using namespace std;

int main() {
    int age;

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

    cout << "Your age is " << age;

    return 0;
}

Sample Input:

20

Output:

Enter your age: 20
Your age is 20

Example 2: Taking Multiple Inputs

We can take multiple inputs using a single cin statement.

#include <iostream>
using namespace std;

int main() {
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << "First number: " << a << endl;
    cout << "Second number: " << b;

    return 0;
}

Sample Input:

10 20

Output:

First number: 10
Second number: 20

The extraction operator reads the values in the same order in which the variables are written.

6. Taking Different Data Types as Input

C++ allows us to take input into variables of different data types.

#include <iostream>
using namespace std;

int main() {
    int age;
    float height;
    char grade;
    double percentage;

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

    cout << "Enter height: ";
    cin >> height;

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

    cout << "Enter percentage: ";
    cin >> percentage;

    cout << "\nStudent Details\n";
    cout << "Age: " << age << endl;
    cout << "Height: " << height << endl;
    cout << "Grade: " << grade << endl;
    cout << "Percentage: " << percentage;

    return 0;
}

Each variable receives input according to its declared data type.

7. Taking String Input

In C++, we commonly use two methods to take string input:

  • cin – reads input until whitespace is encountered.
  • getline() – reads an entire line, including spaces.

Using cin for a String

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

int main() {
    string name;

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

    cout << "Hello " << name;

    return 0;
}

If the user enters Rahul Sharma, only Rahul is stored in the variable.

Using getline()

The getline() function reads the complete line, including spaces.

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

int main() {
    string name;

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

    cout << "Hello " << name;

    return 0;
}

Sample Input:

Rahul Sharma

Output:

Hello Rahul Sharma

Important: Using getline() After cin

When getline() is used immediately after reading a value with cin >>, the newline left in the input buffer may be consumed by getline().

We can use cin.ignore() to discard the remaining characters up to the next newline.

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

int main() {
    int age;
    string name;

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

    cin.ignore();

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

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

    return 0;
}

For more robust input handling, especially when invalid or extra characters may be present, cin.ignore(numeric_limits<streamsize>::max(), '\n') can be used with the <limits> header.

8. Difference Between cin and cout

Basis cin cout
Purpose Takes input from the user. Displays output.
Operator Extraction operator (>>). Insertion operator (<<).
Direction Data moves into the program. Data moves out of the program.
Example cin >> age; cout << age;

9. Using cerr and clog

Besides cin and cout, C++ also provides cerr and clog.

cerr

cerr is commonly used to display error messages.

cerr << "Error: Invalid input";

clog

clog is commonly used to display general program messages or logs.

clog << "Program execution started";

10. Basic Formatting of Output

C++ provides formatting tools that help us display output in a readable manner.

Using setw()

The setw() function sets the minimum width for the next output field. It is available in the iomanip header.

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

int main() {
    cout << setw(10) << "Name" << endl;
    cout << setw(10) << "Rahul";

    return 0;
}

Using fixed and setprecision()

The fixed and setprecision() manipulators can control the number of digits displayed after the decimal point.

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

int main() {
    double value = 12.34567;

    cout << fixed << setprecision(2) << value;

    return 0;
}

Output:

12.35

11. Complete Example: Adding Two Numbers

Let's combine input, processing, and output in one program.

#include <iostream>
using namespace std;

int main() {
    int a, b, sum;

    cout << "Enter first number: ";
    cin >> a;

    cout << "Enter second number: ";
    cin >> b;

    sum = a + b;

    cout << "Sum = " << sum << endl;

    return 0;
}

Sample Input:

10
20

Output:

Enter first number: 10
Enter second number: 20
Sum = 30

12. Common Input and Output Mistakes

  • Forgetting the header: Not including #include <iostream>.
  • Using the wrong operator: Using << with cin or >> with cout.
  • Incorrect variable type: Taking input into a variable whose type does not match the expected data.
  • String input issue: Using cin when the complete line contains spaces.
  • Newline issue: Using getline() after cin >> without handling the leftover newline.
  • Ignoring input errors: Not checking whether the input operation succeeded.

13. Practice Questions

  1. What is input and output in programming?
  2. What is the purpose of the iostream library?
  3. What is the difference between cin and cout?
  4. Explain the insertion and extraction operators.
  5. What is the difference between endl and \n?
  6. What is the difference between cin and getline()?
  7. Why is cin.ignore() sometimes required before getline?
  8. What are cerr and clog used for?
  9. Write a program to take three numbers as input and calculate their average.
  10. Write a program to take a student's name and marks in three subjects and display the total and average.
  11. Write a program to display a floating-point number up to two decimal places.

14. Key Takeaways

  • Input allows a program to receive data from the user.
  • Output allows a program to display information.
  • cin is used for input and cout is used for output.
  • >> is the extraction operator and << is the insertion operator.
  • getline() reads a complete line, including spaces.
  • endl and \n are used to move to a new line.
  • iomanip provides useful output formatting tools.
Scroll to Top