Updated March 28, 2023
Introduction to C++ user input
The following article provides an outline for C++ user input. In C++, the cin object is used to accept input from a standard input device, such as a keyboard. C++ includes libraries that allow us to perform an input in various ways. In C++, input takes the form of a stream, which is a sequence of bytes. The cin object is an instance of the istream class. It is linked to stdin, the standard C input stream. For reading inputs, the extraction operator(>>) is combined with the object cin. Finally, the data is extracted from the object cin, which is entered using the keyboard, using the extraction operator.
The syntax of the C++ user input –
// user input is stored to a variable
cin >> variable;
Note –
The extractor operator “>>” accepts an input from the standard input stream, cin. Only variables can be used as inputs to store the data. The console provides input data. The namespace std includes cin. This indicated that if the namespace is not utilized, you must use std::cin.
Working of the C++ user input
The cin object in C++ accepts the user input. For example, suppose we have to accept the age of the user from the user. So, first, we should declare a variable of type int called age. Next, we can use the cin object and extractor operator as “cin >> name.” The name is the variable here that stores the given name. This operation causes the program to wait for input from cin; in most cases, this implies that the program will wait for the user to type a sequence of characters. Note that the characters typed on the keyboard are only sent to the application when the ENTER (or RETURN) key is pushed.
Examples for the C++ user input
Different examples are mentioned below:
Example #1
Example of the C++ user input to accept the integer input from the user –
Code:
#include <iostream>
using namespace std;
int main() {
int age;
cout<< "Enter the age: ";
cin>> age;
cout<< "\nThe age entered by the user is: "<< age;
cout<<endl;
return 0;
}
An output of the above code is –
As in the above program, the integer variable age is first declared. Next, use the cin object and extractor operator as “cin >> age,” which causes the program to wait for the input from the user. Once the user enters the value, the cin object accepts it and store it to the age variable. Next, the value of the age variable is printing, as we can see in the output.
Example #2
Example of the C++ user input to accept the character input from the user –
Code:
#include <iostream>
using namespace std;
int main() {
char ch;
cout<< "Enter the characters of your name and when ends enter '.': ";
cin>> ch;
while(ch!='.')
{
cout<< "The characters entered by the user is: "<< ch;
cout<< endl;
cout<< "Enter the characters of your name: ";
cin>> ch;
}
cout<<endl;
return 0;
}
An output of the above code is –
As in the above program, the char variable ch is first declared. Next, use the cin object and extractor operator as “cin >> ch,” which causes the program to wait for the input from the user. When the user enters the value, the cin object accepts it and store it in the ch variable. To accept all the characters of the name while loop is used. Next, the value of the ch variable is printing, as we can see in the output.
Example #3
Example of the C++ user input to accept the string input from the user –
Code:
#include <iostream>
using namespace std;
int main() {
char name[20];
cout<< "Enter your name: ";
cin>> name;
cout<< "The name entered by the user is: "<< name;
cout<< endl;
return 0;
}
An output of the above code is –
As in the above program, the array of character variable names is first declared. Next, use the cin object and extractor operator as “cin >> name,” which causes the program to wait for the input from the user. When the user enters the name, the cin object accepts it and store it in the name variable. Next, the value of the name variable is printing, as we can see in the output.
Example #4
Example of the C++ user input to accept the multiple inputs from the user –
Code:
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "Enter name and age: " << endl;
// accept multiple input using cin object
cin >> name >> age;
// display the provided values
cout << "The name entered by the user is: " << name << endl;
cout << "The age entered by the user is: " << age << endl;
cout << endl;
return 0;
}
An output of the above code is –
As in the above program, the integer and string variables are first declared. Next, use the cin object and extractor operator as “cin >> name >> age,” which causes the program to wait for the two inputs from the user. When the user enters the name and age, the cin object accepts the first value and store to name variable and accept a second value and it and store to the age variable. Next, the values of the variable are printed, as we can see in the output.
Conclusion
In C++, input takes the form of a stream, which is a sequence of bytes. The cin object is an instance of the istream class that is used to accept input from a standard input device, such as a keyboard.
Recommended Articles
This is a guide to C++ user input. Here we discuss the Working of the C++ user input along with the examples and outputs. You may also have a look at the following articles to learn more –