C++ Input and Output

So far, we have displayed fixed values using cout. But real programs often need to take information from the user. For example, a program may ask for your name, age, or marks and then display the information. C++ mainly uses:

  • cout to display output
  • cin to take input

Display Output Using cout

We use cout to display text or values on the screen.

#include <iostream>
using namespace std;

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

    return 0;
}

Output:

Welcome to C++!

You can also display a variable:

#include <iostream>
using namespace std;

int main() {
    int age = 14;

    cout << "Age: " << age;

    return 0;
}

Output:

Age: 14

Take Input Using cin

cin is used to take input from the user.

For example:

#include <iostream>
using namespace std;

int main() {
    int age;

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

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

    return 0;
}

If the user enters:

14

The output will be:

Enter your age: 14
Your age is: 14

Here:

cin >> age;

takes the value entered by the user and stores it in the age variable.

Taking Different Types of Input

You can use cin with different data types.

#include <iostream>
using namespace std;

int main() {
    int age;
    double marks;

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

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

    cout << "Age: " << age << "\n";
    cout << "Marks: " << marks;

    return 0;
}

Example input:

14
88.5

Output:

Age: 14
Marks: 88.5

Taking Multiple Inputs

You can take multiple values using one cin statement.

#include <iostream>
using namespace std;

int main() {
    int age;
    int marks;

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

    cout << "Age: " << age << "\n";
    cout << "Marks: " << marks;

    return 0;
}

The user can enter:

14 90

Output:

Age: 14
Marks: 90

Taking String Input

You can use cin to take a single word as input.

#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

The output will be:

Hello, Rahul

However, cin stops reading when it finds a space. For example, if the user enters:

Rahul Kumar

only Rahul will be stored.

Using getline()

To take a complete line of text, including spaces, use getline().

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

int main() {
    string name;

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

    cout << "Hello, " << name;

    return 0;
}

If the user enters:

Rahul Kumar

Output:

Hello, Rahul Kumar

Moving to a New Line

You can use \n to move the output to a new line.

#include <iostream>
using namespace std;

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

    return 0;
}

Output:

Hello
Welcome to C++

You can also use endl:

cout << "Hello" << endl;
cout << "Welcome to C++";

Both \n and endl can be used to move to the next line.

A Simple Input and Output Program

Let’s create a small program that asks for a student’s name and age.

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

int main() {
    string name;
    int age;

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

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

    cout << "\nName: " << name;
    cout << "\nAge: " << age;

    return 0;
}

Example input:

Enter your name: Anjali
Enter your age: 14

Output:

Name: Anjali
Age: 14

Key Points to Remember

  • cout is used to display output.
  • cin is used to take input.
  • >> is used with cin to read values.
  • getline() can read a complete line of text.
  • \n moves the output to a new line.
  • endl also moves the output to a new line.
  • cin can be used with different data types.

Mini Quiz

0%

C++ PROGRAMMING - QUIZ 4

1 / 5

Q1. Which statement is used to take input from the user in C++?

2 / 5

Q2. What does cin >> age; do?

3 / 5

Q3. What happens when cin reads a string containing spaces?

4 / 5

Q4. Which function is used to take a complete line of text, including spaces?

5 / 5

Q5. Which of the following can be used to move output to a new line?

Your score is

The average score is 0%

0%

Mini Project

Mini Project: Movie Ticket Details

Question:

Write a C++ program that asks the user for movie ticket details and then displays a summary.

  • Ask the user to enter their full name.
  • Ask for their age.
  • Ask for the number of tickets.
  • Ask for the ticket price.
  • Use getline() to accept the full name.
  • Use cin to take the numeric inputs.
  • Display all the entered information on separate lines.
  • Use \n or endl to format the output.
View Answer Code
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;
    int tickets;
    double price;

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

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

    cout << "Enter number of tickets: ";
    cin >> tickets;

    cout << "Enter ticket price: ";
    cin >> price;

    cout << "\n--- Movie Ticket Details ---\n";
    cout << "Name: " << name << "\n";
    cout << "Age: " << age << "\n";
    cout << "Tickets: " << tickets << "\n";
    cout << "Ticket Price: " << price;

    return 0;
}

What’s Next?

Now that you know how to take input and display output, the next chapter will introduce C++ Operators and show you how to perform calculations and comparisons in your programs.


Frequently Asked Questions (FAQs)

Q1. What is C++ Input and Output?

C++ Input and Output allows programs to receive information from users and display results. cin is commonly used for input, while cout is used for output.

Q2. How do you take C++ input from a user?

C++ input is usually taken with cin and the extraction operator >>. For example, cin >> age; reads a value and stores it in the age variable.

Q3. How is C++ output displayed on the screen?

C++ output is displayed using cout. For example, cout << "Hello"; prints Hello on the screen.

Q4. What is the difference between cin and cout in C++?

C++ cin and cout are used for different purposes. cin reads input from the user, while cout displays output on the screen.

Q5. When should you use getline() in C++?

Use C++ getline() when you need to read a complete line of text, including spaces. For example, it can read a full name such as Rahul Kumar.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top