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:
coutto display outputcinto 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
coutis used to display output.cinis used to take input.>>is used withcinto read values.getline()can read a complete line of text.\nmoves the output to a new line.endlalso moves the output to a new line.cincan be used with different data types.
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.
