Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Standard Input Output Stream

By Sripriya R | on August 24, 2007 |
C++ Tutorials

In this C++ tutorial, you will learn about standard input stream and standard output stream explained along with syntax and examples.

It is C++ programming language uses the concept of streams to perform input and output operations using the keyboard and to display information on the monitor of the computer.

What is a Stream?

A stream is the source or destination of a series of data, either characters, or in the case of binary files, a sequence of bytes that represent memory content. The standard input and output stream objects of C++ are declared in the header file iostream.

Standard Input Stream

Generally, the device used for input is the keyboard. For inputting, the keyword cin is used, which is an object. The operator of extraction, >>, is used on the standard input stream, in this case: cin stream. Syntax for using the standard input stream is cin followed by the operator >> followed by the variable that stores the data extracted from the stream.

For example:

  1. int prog;
  2. cin >> prog;

In the example above, the variable prog is declared as an integer type variable. The next statement is the cin statement. The cin statement waits for input from the user"s keyboard that is then stored in the integer variable prog.

The input stream cin is waiting for the user to input a value,before proceeding for processing or storing the value. This duration is dependent on the user pressing the RETURN key on the keyboard. It is also possible to request input for more than one variable in a single input stream statement. A single cin statement is as follows:

  1. cin >> x >> y;

is the same as:

  1. cin >> x;
  2. cin >> y;

In both of the above cases, two values are input by the user, one value for the variable x and another value for the variable y.

  1. // This is a sample program         
  2. //This is a comment Statement
  3. //For using MS VS2005 and later, iostream requires the addition of a namespace 
  4. //instead of <iostream.h>, just like in the following example
  5.  
  6.  #include <iostream>			//Header File Inclusion Statement			
  7. using namespace std;				
  8. void main()
  9. {
  10. 	int sample, example;
  11. 	cin >> sample;
  12. 	cin >> example;
  13. }

This produces the following output:

If a programmer wants to write comments in C++ program, the comments should follow after a pair of slashes denoted by //. All the characters after the // are ignored by C++ compiler and the programmer can choose to comment after the //.

In the above example, two integer variables are input with values. The programmer can produce input of any data type. It is also possible to input strings in C++ program using cin. This is performed using the same procedures. The vital point to note is cin stops when it encounters a blank space. When using a cin, it is possible to produce only one word. If a user wants to input a sentence, then the above approach would be tiresome. For this purpose, there is a function in C++ called getline.

}

Standard Output Stream

A By default, the device used for output is the screen of the computer. For outputting values the keyword cout is used, which is an object. The insertion operator << is used on the standard output cout stream. The syntax for using the standard output stream is cout followed by the operator << followed by the value to be inserted or output by the insertion operator.

For example:

  1. int prog;
  2. cin >> prog;
  3. cout << prog;

In the above example, the variable prog is declared as an integer type variable. The next statement is the cin statement that waits for input from the user"s keyboard. This information is then stored in the integer variable prog. The value of prog is displayed on the screen by the standard output stream cout. It is also possible to display a sentence as follows:

cout << "Training given by Exforsys ";

The above gives output as:

Training given by Exforsys

If a programmer chooses to use constant strings of characters, they must be enclosed between double quotes " " .

In this situation, it is important to note the difference between the two statements below:

  1. cout << "exforsys"; 
  2. cout << exforsys;

In the above, the first statement displays on the screen as exforsys. The second statement outputs the value of the variable exforsys. As previously explained, the extraction operator >> can be used more than once in a single cin statement. Similarly, it is possible to use the insertion operator << more than once in a cout statement.

In this situation, it is important to note the difference between the two statements below:

For example:

  1. cout << "Exforsys " << "gives" << " excellent training";

This produces output on the screen as:

Exforsys gives excellent training

The above concept is mainly used if the programmer chooses to print string constants followed by variables.

In this next example, the programmer chooses to display a combination of string constants and variables.

For example:

  1. int a=50; 
  2. cout << "Exforsys has given " << a << " numbers of trainings";

This produces the output as:

Exforsys has given 50 numbers of trainings

Below is one more example:

  1. cout << "Exforsys"; 
  2. cout << "Training";

The above produces output as:

Exforsys Training

An important point to note from the above example is cout does not give a line break unless specified. If the programmer chooses to display output in a new line, it must be explicitly specified in cout by using the n which denotes newline character.

For example:

  1. cout << "Exforsysn"; 
  2. cout << "Training";

gives output as

Exforsys

Training

There is also another way for specifying newline that is by using endl manipulator (end line).

For example:

  1. cout << "Exforsys" << endl;
  2. cout << "Training";

gives output as

Exforsys

Training

Example to demonstrate the use of input and output streams
  1. // Example to demonstrate the use of Input and Output streams
  2.  #include <iostream>			
  3. using namespace std;				
  4.  
  5. void main()
  6. {
  7.    int a,b;
  8.    cout << "Enter the value of a: ";
  9.    cin >> a;
  10.    b=a+10;
  11.    cout << "Value of b is: " << b;
  12. }

This produces the following output:

« « Ajax and Web Applications
C++ Operators Part I » »

Author Description

Avatar

Free Training

RSSSubscribe 385 Followers
  • Popular
  • Recent
  • C++ Static Functions

    February 12, 2007 - 0 Comment
  • C++ Operators Part I

    August 24, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Structure Part II

    August 19, 2007 - 0 Comment
  • C++ Abstraction

    April 26, 2007 - 0 Comment
  • C++ Operators Part II

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part 1

    September 7, 2006 - 0 Comment
  • C++ Encapsulation

    August 16, 2007 - 0 Comment
  • C++ Manipulators

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part II

    September 9, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT