codescracker


c++

C++ Input and Output Operators



« Previous Tutorial Next Tutorial »


Input coming from the user's terminal, referred to as standard input is tied to the predefined iostream cin and output directed to the user's terminal, referred to as standard output, is tied to the predefined iostream cout.

C++ Output Operator

The output operator ("<<") ("put to"), also called stream insertion operator is used to direct a value to standard output.

C++ Output Operator Example

Here is an example program, uses C++ output operator.

/* C++ Input and Output Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	cout<<"Welcome to codescracker.com";
	cout<<"\nYou are learning C++ here\n";
	cout<<"You can learn all about C++ here";
	cout<<"\nThis is C++ Input and Output Operators Tutorial";
	getch();
}

Here is the sample output of the above C++ program:

c++ input output operators

C++ Input Operator

The input operator (">>") ("get from"), also known as stream extraction operator is used to read a value from standard input.

C++ Input Operator Example

Here is an example program, uses C++ input operator to store integer value:

/* C++ Input and Output Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int val1, val2;
	cin>>val1>>val2;
	getch();
}

Here is the sample run of this C++ program:

input and output operators in c++

Here is another C++ code fragment, also uses C++ input operator to store character value.

cin>>ch;

But in case, if you want to store string value, then use this:

/* C++ Input and Output Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	char welcome[80];
	char message[80];
	char comment[80];
	char present[80];
	cin.getline(welcome, 80);
	cin.getline(message, 80);
	cin.getline(comment, 80);
	cin.getline(present, 80);
	getch();
}

Here is the sample run of the above C++ program, This C++ program ask from the user to enter four line. Just enter the four line as shown in the above program of C++ output operator:

c++ intput operators

C++ Input/Output Operators Example

Here are some example programs given, using C++ input/output operators:

/* C++ Input and Output Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	char welcome[80];
	char message[80];
	char comment[80];
	char present[80];

	cin.getline(welcome, 80);
	cin.getline(message, 80);
	cin.getline(comment, 80);
	cin.getline(present, 80);

	cout<<endl;
	cout<<welcome<<endl;
	cout<<message<<endl;
	cout<<comment<<endl;
	cout<<present<<endl;

	getch();
}

Here is the sample run of the above C++ program:

c++ output operators

Above C++ program, ask from the user, to enter any four line, these four lines stored in some separate variables using the C++ input operator, and then all the four lines print back to the output screen using the C++ output operator. Let's take another program:

/* C++ Input and Output Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int val1, val2, sum;
	cout<<"Enter the two number : ";
	cin>>val1>>val2;
	sum = val1 + val2;
	cout<<"The sum of the two number is : ";
	cout<<sum;
	getch();
}

Here is the sample run of the above C++ program:

c++ input output operators example

Let's take another C++ program:

/* C++ Input and Output Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	int i;
	float f;
	char ch, fls;
	char str[80];

	cout<<"Enter an integer number: ";
	cin>>i;
	cout<<"Enter a floating-point number: ";
	cin>>f;
	cout<<"Enter a character: ";
	cin>>ch;
	cin.get(fls);
	cout<<"Enter a string: ";
	cin.getline(str, 80);

	cout<<"\nYou entered:\n";
	cout<<"Integer: "<<i;
	cout<<"\nFloating: "<<f;
	cout<<"\nCharacter: "<<ch;
	cout<<"\nString: "<<str;

	getch();
}

Here is the sample run of this C++ program:

intput output operators

More Examples

Here are some more C++ example programs listed, that you can go for these:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test