CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[C++] Practical C++ Part 3 - Understanding Input Output


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author Rae On 2007-04-29 11:04:17




View and vote on the article here: Practical C++ Part 3 - Understanding Input Output


Practical C++ Part 3 - Understanding Input Output

Category
C++
Summary
Body
Practical C++ Part 3 - Understanding Input/Output
In this part of the 'Practical C++' series, we look closely at input/output statements of C++. Basic output has already been taught in part 1 of this series. We now build up on those concepts.
  • The command used for output is - cout, and the command used for input is - cin.
A stream is a sequence of bytes used in transferring data between source and destination. "cout" and "cin" statements are nothing but output and input streams for the console (screen).
int a=10;

    cout<<a<<endl;
The above code snippet prints the value of variable 'a' i.e. 10, and then jumps onto the next line. We consider two more cases of the above snippet.
int a=10;

  cout<<++a<<endl;
This code will output the value as 11 for variable 'a' after changing its value in the corresponding memory location.
int a=10;

  cout<<a++<<endl;
The above code snippet will output the value as 10, and then change the value of variable 'a' in the memory location to 11. So the next time the value of 'a' is printed, it is going to be 11.
int a;

  cin>>a;
The above code snippet is an example of taking input from the user. It is done through the "cin" statement. It is interesting to note the use of ">>" instead of "<<", as is done in the "cout" statement.
The "cin" statement can be used for anything from 'int' to 'float' to 'char'. It can also be used to take multiple inputs.
  cin>>a>>b>>c;
  • Now let us look at a program implementing both the "cin" and the "cout" statements.
#include<iostream.h>

    #include<conio.h>
    void main()

    {

    clrscr();

    int roll;

    float sal;

    cout<<"Enter the roll number of the employee : ";

    cin>>roll;

    cout<<endl<<"Enter the salary : ";

    cin>>sal;
   clrscr();

    cout<<"ROLL NO. : "<<roll<<" SALARY : "<<sal<<endl;

    getch();

    }
The above program takes the roll number and salary of an employee from the user, and then displays it.
References :-
Object Oriented Programming Using C++ by E Balagurusamy
Using C++ by Rob McGregor

Written by Rae (12 January 2005)

Rae is a member of CAU Knowledge-Bank Tutorial Writers


This article was imported from the CyberArmy University site. (original author: rae)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02395 seconds


About Us | Privacy Policy | Mission Statement | Help