Skip to main content

3. Input /output in c++

  ➢  Input/ Output in C++ :-


       C++ comes with libraries which provides us many ways for performing input and output. In C++.

       input and output is performed in the form of sequence of bytes or more commonly known as  streams.


● Input Stream: If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input.

● Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device(display screen ) then this process is called output.


● Header files available in C++ for Input – Output operation are:

• iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout, cerr etc.

• fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.


           wo keywords cout and cin are used very often for taking inputs and printing outputs. These two are the most basic methods of taking input and output in C++. For using cin and cout we must include the header file iostream in our program.

          In this article we will mainly discuss about the objects defined in the header file iostream like cin and cout.


1) Standard output stream (cout):- 

             Usually the standard output device is the display screen. cout is the instance of the ostream class. cout is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator (<<).


● Example :-

#include <iostream>

using namespace std;

int main( )

{

    char sample[] = "Hello";

    cout << sample << " welcome to cpp";

    return 0;

}


● Output :-

Hello welcome to cpp


2) standard input stream (cin):- 

               Usually the input device is the keyboard. cin is the instance of the class istream and is used to read input from the standard input device which is usually keyboard.

               The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keboard.


● Example :-


#include<iostream>

#include<conio>

int main()

{

      int age;

      cout << "Enter your age:";

      cin >> age;

      cout << "\nYour age is: "<<age;

      return 0;

}


● Output:-

Enter your age:18

Your age is:18



Comments

Popular posts from this blog

C++ programming language

                c++ is a general programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.   ❖❖  C++ with oops ❖❖

Pratical -206 paper solution -3

Full course of c++ programming language Click here > C++ programming language Q-1 [A] Write a c program to create structure of employee with members Empid, EmpName, Qualification and EmpSalary by taking input of 5 employees display the employee whose qualification is "MBA" and salary greater than 20000 . #include <stdio.h> #include <conio.h> #include <string.h> struct Employee {     int Empid, EmpSalary;     char EmpName[50];     char Qualification[50]; }; Void main() {     struct Employee emp[5];     int i;     for (i = 0; i < 5; i++)     {         printf("Enter employee ID: ");         scanf("%d", &emp[i].Empid);         printf("Enter employee name: ");         scanf("%s", emp[i].EmpName);         printf("Enter employee qualification: ");         scanf("%s", emp[i].Quali...

9. Jumps Statement in c++

➢ Jumps in Loops:- ➢ C++ break and continue Statement:-                     There are two statements (break; and continue ;) built in C++ programming to alter the normal flow of program.                     Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. The break; statement is also used to terminate switch statement. 1) break Statement :-                       The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears . Syntax of break :- break;              ...