Console Input/Output in C++ · 1/5/2019 · Console Input/Output in C++. Console Output (cout
Transcript of Console Input/Output in C++ · 1/5/2019 · Console Input/Output in C++. Console Output (cout
OOC
6th Sem, ‘A’ Div
2018-19
Ms. Mouna M. Naravani
Console Input/Output in C++
Console Output (cout<<)
➢ The standard output by default is the screen.
➢ cout (prounced as “see-out”) is an object of the class ostream.
➢ cout = console output (usually Monitor/Screen)
➢The << symbol – left shift operator (two less than symbols) – It operates as
“insertion” operator.
➢ It’s a binary operator, which takes two operands.
cout<<content to be printed;
➢ The file ‘iostream.h’ need to be included in the source code for successful
compilation of a program.
➢ It does not need any format specifiers.
The operator << is called the insertion or put to operator. It inserts (or sends) the
contents of the variable on its right to the object on its left.
Examples:
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
➢Multiple insertion operations (<<) may be chained in a single statement:
cout << "This " << " is a " << "single C++ statement";
Output: This is a single C++ statement
cout << "I am " << age << " years old and my zipcode is " << zipcode;
If age=30 and zipcode=586101, What is the output?
Output: I am 30 years old and my zipcode is 586101
➢ What cout does not do automatically is add line breaks at the end, unless instructed to do so.
For example,
cout << "This is a sentence.";
cout << "This is another sentence.";
The output would be in a single line, without any line breaks in between.
This is a sentence.This is another sentence.
➢Line breaks can be added in 2 ways:
1. \n
or
2. endl
Example:
cout << "First sentence.\n";
cout << "Second sentence.\nThird sentence.";
Output:
First sentence.
Second sentence.
Third sentence.
➢ The endl manipulator can also be used to break lines.
Example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;
This would print:
First sentence.
Second sentence.
Console Input (cin>>)
➢ The standard input by default is the Keyboard.
➢ cin (prounced as “see-in”) is an object of the class istream.
➢ cin = console input (usually Keyboard)
➢The >> symbol – right shift operator (two greater than symbols) – It operates as
“extraction” operator.
➢ It’s a binary operator, which takes two operands.
cin>>content to be read;
➢ The file ‘iostream.h’ need to be included in the source code for successful
compilation of a program.
➢ It does not need any format specifiers.
The operator >> is known as extraction or get from operator. It extracts (or takes)
the value from the keyboard and assigns it to the variable on its right.
Example:
int age;
cin >> age;
➢ Extractions on cin can also be chained to request more than one datum in a single statement
cin >> a >> b;
➢ This is equivalent to:
cin >> a;
cin >> b;
In both cases, the user is expected to introduce two values, one for variable a, and another
for variable b. Any kind of space is used to separate two consecutive input operations; this
may either be a space, a tab, or a new-line character.
The iostream File
• #include<iostream>
• This directive causes the pre-processor to add the contents of the iostream file to the
program.
• It should be included at the beginning of all programs that use input/output statements.
• Note: Use iostream.h if the compiler does not support ANSI C++ features.
Namespaces
• Namespace is a new concept introduced by the ANSI C++ .
• This defines a scope for the identifiers that are used in a program.
• using namespace std;
• Here, std is the namespace where ANSI C++ standard class libraries are defined.
• All ANSI C++ programs must include this directive.
• This will bring all the identifiers defined in std to the current global scope.
• Using and namespace are the new keyword of C++.
• It is also used to avoid naming collisions.
Structure of C++ Program• It is a common practice to organize a
program into three separate files.
• The class declarations are placed in a
header file and the definitions of
member functions go into another file.
• This approach enables the programmer
to separate the abstract specification of
the interface(class definition) from the
implementation details (member
function definition).
• Finally, the main program that uses the
class is places in a third file which
“includes” the previous two files as well
as any other file required.
Practise Programs
• Write a program to read and display the following using single cin and cout
statement.
Maths = 90
Physics = 77
Chemistry = 69
• Write a program to read three numbers from the keyboard and display the largest
value on the screen.
• Write a program to input an integer value from the keyboard and display on screen
“WELL DONE” that many times.
• Write a program to read the values of a, b and c and display the value of x, where
x = a / b – c
Test your program for the following values:
i] a=250, b=85, c=25
ii] a=300, b=70, c=70
• Write a program that will ask for a temperature in Fahrenheit and display in
Celsius.
• Redo the above using class called temp and member functions.
References
➢Sourav Sahay, “Objected Oriented Programming with C++”
➢E Balagurusamy, “Objected Oriented Programming with C++”
➢P. B. Kotur, “Objected Oriented Programming with C++”