Table of contents
1.
Introduction
2.
What is Input in C++?
3.
What Are Input and Output in C++?
3.1.
Input and Output in C++
3.2.
Iostream Library
3.2.1.
Working of Cin and Cout
3.2.2.
Basic program
3.3.
C++
4.
Various Types of Inputs in C++ 
4.1.
User-Defined Inputs in C++
4.2.
String and Char User Input
4.3.
Implementation of char Input
4.4.
C++
4.4.1.
Input
4.4.2.
Output
4.5.
Implementation of string input
4.6.
C++
4.6.1.
 Input
4.6.2.
Output
5.
Integer User Input
5.1.
Implementation of Integer Input
5.2.
C++
5.2.1.
Input
5.2.2.
Output
5.3.
Float User Input
5.4.
Implementation of float-type input
5.5.
C++
5.5.1.
Input
5.5.2.
Output
6.
Various Ways of Taking Inputs in C++
6.1.
Implementation 
6.2.
C++
6.2.1.
Input
6.2.2.
Output
6.3.
Implementation
6.4.
C++
6.4.1.
Output
7.
How to Take User Input in C++?
7.1.
Implementation
7.2.
C++
7.2.1.
 Input
7.2.2.
Output
8.
Frequently Asked Questions
8.1.
How to Take Enter as an Input in C++?
8.2.
How do you input a number in C++?
8.3.
What are output and input explained?
8.4.
What are cout and cin in C++?
8.5.
How do you get user input in C++?
9.
Conclusion
Last Updated: Jan 17, 2025
Easy

Input in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Input handling is a fundamental aspect of programming in C++. It allows programs to interact with users by receiving data during execution. Whether it's reading numbers, strings, or complex objects, mastering input techniques is essential for building dynamic and user-friendly applications. C++ provides several methods for input, ranging from simple functions like cin to advanced file-handling techniques. In this blog, we’ll explore various input methods and examples.

Introduction

What is Input in C++?

Input in C++ refers to the process of receiving data from an external source, such as a user or a file, during program execution. It enables dynamic interaction and functionality by allowing the program to process and respond based on the received data.

C++ primarily uses the Standard Input Stream (cin) from the <iostream> library for user input through the console. Additionally, other methods like file input using file streams (ifstream) allow the program to read data from files. Input facilitates tasks like reading numbers, strings, or complex data structures essential for interactive and data-driven applications.

What Are Input and Output in C++?

C++ is an object-oriented programming language. C++ is very similar to C invented in the 1970s. C++ is so similar to C that it compiles over 99% of C programs without changing the code. C++ is a more well-structured C as it has OOPs concepts.

Every machine requires input and gives its corresponding output. In the case of C++  language, we can consider it as a processor also to which we share information after the process is done, and it gives the output. 

Taking the example of a calculator, we only get the required work when we provide some numbers input. Without giving any input, we won’t be getting any results.

We can take the example of our exams too. If we don’t study (not giving information), we don’t get the marks (no output).

Input and Output in C++

C++

In C++, also we need to provide input and output. Here we have a standard library that includes all essentials for input and output to work along with other functionalities. This standard library is known as the iostream library. Iostream refers to as Input Output Stream.

Iostream Library

Iostream is of 2 types input stream and outputs stream. Any sequence of characters we write to or read from an input and output device is known as a stream. So these two make up the input-output stream.

Iostream Library defines four objects, namely.

  • Cin- standard input (object of the type input stream or istream).
     
  • Cout- standard output (object of the type output stream or ostream).
     
  • Cerr- standard error (object of the type output stream or ostream).
     
  • Clog- general information (object of the type output stream or ostream).
Iostream Library

The standard important among all four are the cin and cout. We will be using them now and see how they work. To make them work, we must first include the standard library iostream in our file.

You can also do a free certification of Basics of C++.

Working of Cin and Cout

Basic program

  • C++

C++

#include <iostream>

int main()
{
std::cout << "Enter your age: " << std::endl;
int n = 0;
std::cin >> n;
std::cout << "The age is " << n << std::endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Understanding each line of the above code and try it on online C++ compiler.

#include <iostream>


It is the header file that should be included in every c++ program. Also, iostream is the library that enables the cin and cout functioning.

int main()


It is the main function that should be included in every c++ program; without this function, we will get an error. Its return type is an integer, which means it will return an integer after the processing of the function is done.

std::cout<<"Enter your age: "<<std::endl;
int n=0;
std::cin>>n;
std::cout<<"The age is "<<n<<std::endl;

return 0;

 
This is the body of our function, which means the operation that is to be performed.

std::cout<<"Enter your age: "<<std::endl;


As we studied above that, C++ does not define any statements for doing input or output, but it has a standard library that defines these input and output, so this cout is present in the Standard library, and these two colons represent that cout is present within the scope of the standard library.

<<


This is the output operator; we write them after the court.

"Enter your age: "


Whatever is written inside these double quotes will be printed.

std::endl;

 
Tells us to change the line, and written with std means endl is in the standard library and ends the entire cout statement with a semicolon.

int n=0;


Declaring a variable here, n, which is of integer type, and closing the line with a semicolon.

std::cin>>n;


Taking input from the user, here the cin is used along with std telling that it is part of the standard library, and semi-colons tell that cin is in the scope of the standard library. We are storing value in n.

std::cout<<"The age is "<<n<<std::endl;


Printing the sentence using the cout, which we discussed above. Here we output the stored value of n using the cout operator. For string to print, we use double quotes, but for variables, we don't use double quotes. Closing the statement with a semi-colon is a must.

return 0;

 
Since we have our main function returning an integer, we return 0.

Read More About, C++ Basics, Abstract Data Types in C++

Various Types of Inputs in C++ 

We have two different ways of taking input to a program in C++

  • We have the data to be taken written in the code itself.
     
  • The program asks the user to provide the required input value.
Various Types of Inputs in C++

User-Defined Inputs in C++

We take input in C++  from the user or define it in the code itself, so this input can be of many types, such as integer, string, float, etc. We will study them one by one now.

String and Char User Input

  • Here the user will provide the input as a character or string.
     
  • We initialize the variables with the keyword char for characters and string for input as a string.
     
  •  Understanding by code below.

Implementation of char Input

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
// Printing the sentence
cout << "Enter a single letter: " << endl;

char a;

// User giving input as character
cin >> a;

cout << "The letter is " << a << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

Enter a single letter:
g

Output

Output

You can practice by yourself with the help of online c++ compiler.

Implementation of string input

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
// Printing the sentence
cout << "Enter your name: " << endl;

string a;

// User giving input as string
cin >> a;

// Printing the input we took above
cout << "The name is " << a << endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 Input

Enter your name:
akshat

Output

Output

Must Read Lower Bound in C++

Integer User Input

Below understand integer user input.

  • Here the user will provide the input as an integer.
     
  • We initialize the variables with the keyword int for integers.
     
  • Understanding by code below.

Implementation of Integer Input

  • C++

C++

#include <iostream>
using namespace std;
int main()
{
int n;

// Printing the sentence
cout << "Enter your roll no : " << endl;

// User giving input as integer
cin >> n;

// Printing the input we took above
cout << "The roll no is " << n << endl; //Printing the input we took above

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

Enter your roll no :
11

Output

Output

Float User Input

  • Here the user will provide the input as a float.
     
  • We initialize the variables with keyword float for numbers with decimal values.
     
  • Understanding by code below.

Implementation of float-type input

  • C++

C++

#include <iostream>

using namespace std;
int main()
{
// Making float variable
float n;

// Printing the sentence
cout << "Enter your rooms temperature : " << endl;

// User giving input as float value
cin >> n;

// Printing the input we took above
cout << "The temperature is " << n << endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

Enter your rooms temperature :
23.4

Output

Output

Various Ways of Taking Inputs in C++

Ninjas, there are mainly two different ways of taking Input in a C++ program.

  • We have the data to be taken written in the code itself.
     
  • The program asks the user to provide the required input value.
     

We have discussed the user-defined inputs above.

We initialise a variable defining its data type as an integer, string, character, or float type. Then we ask the user to enter the input value similar to the data type we defined for the variable. Then we make use of that particular variable accordingly.

Example of user-defined Input in C++

Example of user-defined Input

Implementation 

  • C++

C++

#include <iostream>
using namespace std;
int main()
{

// Making float variable
float n;

// Printing the sentence
cout << "Enter your rooms temperature : " << endl;

// User giving input as float value
cin >> n;

// Printing the input we took above
cout << "The temperature is " << n << endl; //Printing the input we took above

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

Enter your rooms temperature :
23.4

Output

Output

Another way is that the program itself has the input value, and we use that value to get our respective output.

Implementation

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
// Making integer  variable already has its value
int n = 8;

// Printing the input we took above
cout << "The age  is " << n << endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Output

Here, the variable's value is defined, and we are directly using it. So, this is the second way of taking input.

How to Take User Input in C++?

Essentially, we make a variable defining its data type, whether integer, string, character or float type. Then we ask the user to enter the input value similar to the data type which we defined for the variable, and then we make use of that particular variable accordingly.

Implementation

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
int n;

// Printing the sentence
cout << "Enter your roll no : " << endl;

// User giving input as integer
cin >> n;

// Printing the input we took above
cout << "The roll no is " << n << endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 Input

Enter your roll no :
44

Output

Output

You can also read about Input-Output Processor here.

Frequently Asked Questions

How to Take Enter as an Input in C++?

To detect the Enter key as input, use cin.get() or getline() for strings. For example:

char enter;
cin.get(enter); // Captures the Enter key

 

This is especially useful for pausing or handling line breaks.

How do you input a number in C++?

A user enters the number using the cin operator, where the variable defined is an integer type.

What are output and input explained?

Input is received by the system, and output is given out through the system.

What are cout and cin in C++?

Cin is an object of input stream taking input from the user. Cout is also an object of the output stream that shows output to the user.

How do you get user input in C++?

We use cin in addition to the operator (>>). And this way, we take the input.

Conclusion

We have studied different types of Input in C++. We discussed various types of taking Input too. They are the basics, so they help us build our understanding skills. So ninjas practice them and code themselves after understanding the logic in C++ language.

Refer to the Below topics for more understanding

Live masterclass