Wednesday, November 15, 2023

Unformatted input/output Operations

Objects cin and cout are used for input and output of data by using the overloading of >> and << operators.

The >> operator is overloaded in the istream class and << is overloaded in the ostream class.

The following is the format for reading data from keyboard:

cin>>variable1>>variable2>>…………..>>variable n

This statement will cause the computer to stop the execution and look for the input data from the keyboard.

The general form of displaying data on the screen is

cout <<item1<<item2<<…………<<item n

put() and get() functions:-

The classes istream and ostream define two member functions get(),put() respectively to handle the single character input/output operations.

There are two types of get() functions: get(char *) and get(void), used to fetch a character including the blank space, tab and newline character.

The get(char *) version assigns the input character to its argument and the get(void) version returns the input character.

Since these functions are members of input/output Stream classes, these must be invoked using appropriate objects.

Example:

char c; 

cin.get( c ) //get a character from the keyboard and assigns it to c

while( c!=’\n’)

{

cout<< c; //display the character on screen

cin.get( c ) //get another character

}

 

The get(void) version is used as follows:

…………..

char c;

c= cin.get();

…………

The value returned by the function get() is assigned to the variable c.

The function put(), a member of ostream class can be used to output a line of text, character by character.

For example

cout.put(‘x’);

displays the character x and

cout.put(ch);

displays the value of variable ch.

The variable ch must contain a character value.A number can be used as an argument to function put().

For example,

cout.put(65);

displays the character A.This statement will convert the numeric value 65 to a char value and displays character whose ASCII value is 65.

Example: Character I/O with get() and put()

#include <iostream>

void main()

{

char c;

cout<<”INPUT TEXT \n”;

cin.get( c );

while ( c 1=’\n’ )

{

cout.put( c);

cin.get( c );

}

}

getline() and write() functions:-

A line of text can be read or display effectively using the line oriented input/output functions getline() and write().

The getline() function reads a whole line of text that ends with a newline character. This function can be invokedby using the object cin as follows:

cin.getline(line,size);

The reading is terminated as soon as either the newline character ‘\n’ is encountered or size-1 characters are read(whichever occurs first).The newline character is read but not saved, instead it is replaced by the null character

Example: Reading Strings With getline()

#include <iostream>

int main()

{

int size=20;

char city[20];

cout<<”enter city name:\n “;

cin>>city;

cout<<”city name:”<<city<<”\n\n”;

cout<<”enter city name again: \n”;

cin.getline(city,size);

cout<<”city name now:”<<city<<”\n\n”;

}

The write() function displays an entire line and has the following form:

cout.write(line,size)

The first argument line represents the name of the string to be displayed and the second argument size indicates the number of characters automatically when the null character is encountered.If the size is greater than the length of line, then it displays beyond the bound of line.

Displaying String With write()

#include <iostream.h>

#include<string.h>

int main(0

{

char * string1 =”Programming”;

int n=strlen(string1);

cout.write(string2,n);

//crossing the boundary

cout.write(string1,20);

return 0;

}


0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top