C++ Put() and get() functions

C++ Put() and get() functions

in the c++ language the classes istream and ostream define two member functions get() and put() respectively to handle the single character input/output operations. There are two types of get functions.

We can use both get(void) and get(char*) prototypes to fetch a character including the blank space, newline and the tab character. The get(char*) version assigns the input character to its argument and second one the get(void) version returns the input character.

Since these all functions are members of the input/output stream classes, we must invoke them using an appropriate object for example .

Example:
char c;
cin.get(c) ; // here is get a character from keyboard
// and assign it to c
while(c != ‘\n’)
{
cout << c; // display the character on screen
cin.get(c); // get another character
}

This code reads and displays a line of text (terminated by a newline character). Remember, the operator >> can also be used to read a character but it will skip the white spaces and newline character.

in the above example the while loop will not work properly if the statement

cin >> c;
is used in place of
cin.get(c);

Note: Try using both of them and compare the results.

The get(void) version will use as follows:
. . . . .
char c;
c = cin.get(); // cin.get(c); replaced
. . . . .
. . . . .

The value returned by the function get() is assigned to the variable c. The put() function is a member of the ostream class, and used to output a line of text, character by character. For example,

cout.put(‘x’);
in the above statement displays the character x and
cout.put(ch);
Here displays the value of variable ch.

The variable ch must contain a character value. We can also use a number as an argument to the function put(). For example,

cout.put(68);

displays the character D. This statement will convert the int value 68 to a char value and display the character whose ASCII value is 68. The following segment of a program reads a line of the text from keyboard and also displays it on the screen.

char c;
cin.get(c); // in this statement read a character
while(c ! = ‘\n’)
{
cout.put(c); // displays the character on screen
cin.get(c);
}

in Program illustrates the use of these two character handling functions.

CHARACTER I/O WITH get() AND put()
#include <iostream>
using namespace std;
int main()
{
int count = 0;
char c;
cout << "INPUT TEXT\n";
cin.get(c);
while (c != '\n')
{
cout.put(c);
count++;
cin.get(c);
}
cout << "\nNumber of characters = " << count << "\n";
return 0;
}

PROGRAM OUTPUT
Input
Object Oriented Programming
output
Object Oriented Programming
Number of characters = 27

Note! When we type a line of input, the text is sent to the program as soon as we press the RETURN key. The program them reads one character at a time using the statement cin.get(c); and displays it using the statement cout.put(c);. The process is terminated when the newline character is encountered.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise