Basic Input / Output in C++
×

Basic Input / Output in C++

3289

I/o in C++ appears in streams and streams can be represented as a series of bytes that flow from input devices to main memory known as input operation and when it flow from memory into output devices like our monitor screen or a disk drive is known as output operation.


Input-Output operations in C++ programming language are performed with the help of header files. There are three main header files that are used in C++ for input-output operations i.e. iostream, iomanip, stream.

cout and cin are two keywords used for printing output and taking input respectively.

These two methods (cin and cout) are present in iostream

Standard input stream (cin)

cin is referred to as the instance of the iostream class is used along with the extraction operator (>>) used to read the input from an input device such as a keyboard.

Example:

int salary;
cin >> salary;

The first statement declares a variable of type int called salary, and the second statement in the above example extracts from cin, a value to be stored in it.

A simple program displaying the use of cin method in C++:

#include <iostream>
using namespace std;
int main ()
{
int sal;
cout << "Please enter salary in integer value: ";
cin >> sal;
cout << "The value you entered is " << sal;
cout << " and its double is " << sal*2 << ".\n";
return 0;
}

Result:

Please enter salary in integer value: 1000

The value you entered is 1000 and its double is 2000


Standard output stream (cout)

Cout is a predefined object i.e. an instance of ostream class is used along with the insertion operator. The sentence enclosed in double-quotes (") and without double quotes will display different outputs as shown in the example.

Example:

cout<< "sal"; // print sal
cout<< u; // print the value of u on screen
cout<<"hii"; // print hii
cout<< hii; // print the content of the variable hii

A simple program displaying the use of cout is as under:

#include <iostream>
using namespace std;
int main()
{
char str[] = "Welcome to C++ tutorial";
cout << "Content of str is: "<< str << endl;
}

Result:

Content of str is: Welcome to C++ tutorial



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments