This page was exported from EDUGRABS [ ]
Export date: Fri Apr 27 0:56:46 2018 / +0000 GMT

getline c, get c, put c and write c

getline c, get c, put c and write c - Unformatted stream I/O functions

getline c, get c, put c and write c

Single character functions : get() and put()

Single Character functions are used for reading and displaying a single character respectively. These functions are :
Example of get() and put() :
char ch;
while (cin.get(ch)!='n')
  • The above code keeps on accepting and displaying characters until a new line character (typed by pressing enter key) is encountered.
  • The difference between cin>>ch and cin.get(ch) is that when >> operator is used, the white spaces (e.g. tabs) and newline characters are ignored where as it is not so with cin.get(ch).
char ch;
  • The above code first input a character in variable ch  and prints it on the screen as given by cout.put(ch).
  • The line cout.put(65) will display 'A' on the screen as ASCII value of A is 65.
  • Thus, a number argument can also be given with put() and it will print equivalent character of that number.

Multi Character Functions : getline() and write()

The single character functions defined above can handle single character at a time and cannot handle multiple characters. To solve this problem, there are functions that can handle strings also. These functions are :
Example of getline() and write() :
char name[30];
  •  The above code reads a line of text (max 29 valid characters) and stores it under variable name.
  • The function getline() can read white spaces (spaces, tabs etc.) and newline characters whereas cin>>name will not read any white space or newline character.
  •  For instance if you give following input : "Test Program" then
    • with getline() function, name will be having value "Test Program" but
    • when used with >>, name will be having Test only as any other character following a white space or newline character is ignored by >>.
    • After reading a string, cin automatically adds the terminator character '' to the string.
cout.write(name, 3);
  •  if name stores value "Program", then the above code will display "Pro" only.
  • Two write() functions can be combined together as shown below :
  • cout.write(string1, size1).write(string2, size2);
    • This statement prints string1 followed by string2 i.e. in the same line.



Post date: 2016-01-03 13:29:14
Post date GMT: 2016-01-03 13:29:14
Post modified date: 2016-01-05 11:02:57
Post modified date GMT: 2016-01-05 11:02:57

Powered by [ Universal Post Manager ] plugin. MS Word saving format developed by gVectors Team