Unformatted input/output operations In C++
Last Updated :
11 Nov, 2021
In this article, we will discuss the unformatted Input/Output operations In C++. Using objects cin and cout for the input and the output of data of various types is possible because of overloading of operator >> and << to recognize all the basic C++ types. The operator >> is overloaded in the istream class and operator << is overloaded in the ostream class.
The general format for reading data from the keyboard:
cin >> var1 >> var2 >> .... >> var_n;
- Here, var1, var2, ......, varn are the variable names that are declared already.
- The input data must be separated by white space characters and the data type of user input must be similar to the data types of the variables which are declared in the program.
- The operator >> reads the data character by character and assigns it to the indicated location.
- Reading of variables terminates when white space occurs or character type occurs that does not match the destination type.
Program 1:
C++
// C++ program to illustrate the
// input and output of the data
// entered by user
#include <iostream>
using namespace std;
// Driver Code
int main()
{
int data;
char val;
// Input the data
cin >> data;
cin >> val;
// Print the data
cout << data << " " << val;
return 0;
}
Output:
Explanation: In the above program, 123 is stored in the variable val of integer, and B is passed to the next cin object and stored in the data variable of character.
put() and get() functions:
The class istream and ostream have predefined functions get() and put(), to handle single character input and output operations. The function get() can be used in two ways, such as get(char*) and get(void) to fetch characters including blank spaces, newline characters, and tab. The function get(char*) assigns the value to a variable and get(void) to return the value of the character.
Syntax:
char data;
// get() return the character value and assign to data variable
data = cin.get();
// Display the value stored in data variable
cout.put(data);
Example:
char c;
// directly assign value to c
cin.get(c);
// Display the value stored in c variable
cout.put()
Program 2:
C++
// C++ program to illustrate the
// input and output of data using
// get() and puts()
#include <iostream>
using namespace std;
// Driver Code
int main()
{
char data;
int count = 0;
cout << "Enter Data: ";
// Get the data
cin.get(data);
while (data != '\n') {
// Print the data
cout.put(data);
count++;
// Get the data again
cin.get(data);
}
return 0;
}
Output:
In C++, the function getline() and write() provide a more efficient way to handle line-oriented inputs and outputs. getline() function reads the complete line of text that ends with the new line character. This function can be invoked using the cin object.
Syntax:
cin.getline(variable_to_store_line, size);
The reading is terminated by the '\n' (newline) character. The new character is read by the function, but it does not display it, instead, it is replaced with a NULL character. After reading a particular string the cin automatically adds the newline character at end of the string.
The write() function displays the entire line in one go and its syntax is similar to the getline() function only that here cout object is used to invoke it.
Syntax:
cout.write(variable_to_store_line, size);
The key point to remember is that the write() function does not stop displaying the string automatically when a NULL character occurs. If the size is greater than the length of the line then, the write() function displays beyond the bound of the line.
Program 3:
C++
// C++ program to illustrate the
// input and output of file using
// getline() and write() function
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
char line[100];
// Get the input
cin.getline(line, 10);
// Print the data
cout.write(line, 5);
cout << endl;
// Print the data
cout.write(line, 20);
cout << endl;
return 0;
}
Output:
Similar Reads
How to Take Operator as Input in C++? Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as
2 min read
How to Validate User Input in C++ Validating the user input is very important for any program to ensure that the data being processed is correct and meaningful. In C++, various techniques can be used to validate the user input and in this article, we will learn how to validate the user input in C++. Validate User Input in C++To vali
4 min read
How to Read a Line of Input Text in C++? In C++, we often need to take input from the user by reading an input text line by line but the cin method only takes input till whilespace. In this article, we will look at how to read a full line of input in C++. For Example, Input: This is a text.Output: Entered Text: This is a text.Taking a Line
2 min read
How to Overload the (+) Plus Operator in C++? In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo
2 min read
Overloading of function-call operator in C++ In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by â()â which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overlo
3 min read
Managing Console I/O operations in C++ Every program takes some data as input and generates processed data as an output following the familiar input process output cycle. It is essential to know how to provide the input data and present the results in the desired form. The use of the cin and cout is already known with the operator >
4 min read
How to Take Multiple Input from User in C++? In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin
2 min read
cin.ignore() Function in C++ The cin.ignore() function in C++ is a member function of the std::istream. It is used to ignore (or discard) certain number of characters from the input buffer. This function is very useful when we have to deal with leftover characters in the input stream that could interfere with subsequent input o
3 min read
How to Read Multiple Numbers from a Single Line of Input in C++? In C++, when working with user inputs we often need to take input of multiple numbers from a user in a single line. In this article, we will learn how to read multiple numbers from a single line of input in C++. Example Input: 1 7 0 4 6 8 Output: Entered Number: 1, 7, 0, 4, 6, 8Take Multiple Numbers
2 min read
Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo
5 min read