COMPUTER
PROGRAMMING
Lecture 2
Programming Basics
What will be discussed
Standard Streams
Standard streams
• A mapping between data and input/output device
Using iostream.h
• Include iostream.h instead of stdio.h
• Standard iostream objects:
cout - object providing a connection to the monitor
cin - object providing a connection to the keyboard
cerr - object providing a connection to error streem
• To perform input and output we send messages
to one of these objects (or one that is connected
to a file)
The Insertion Operator (<<)
• To send output to the screen we use the
insertion operator on the object cout
• Format: cout << Expression;
• The compiler figures out the type of the object
and prints it out appropriately
cout << 5; // Outputs 5
cout << 4.1; // Outputs 4.1
cout << “String”; // Outputs String
cout << ‘\n’; // Outputs a newline
The Extraction Operator (>>)
• To get input from the keyboard we use the
extraction operator and the object cin
• Format: cin >> Variable;
• No need for & in front of variable
• The compiler figures out the type of the variable
and reads in the appropriate type
int X;
float Y;
cin >> X; // Reads in an integer
cin >> Y; // Reads in a float
More about cout
• width(int) function sets the width for printing a value
• Only works until the next insertion command comes
int x = 42;
cout.width(5);
cout << x << ‘\n’; // Outputs 42
cout << x << ‘\n’; // Outputs 42
More about cout
• fill(char) function sets the fill character.
• The character remains as the fill character until set again.
int x = 42;
cout.width(5);
cout.fill(‘*’);
cout << x << ‘\n’; // Outputs ***42
More about cout
• precision (int) sets the number of significant
digits of float type numbers
float y = 23.1415;
cout.precision(1);
cout << y << '\n'; // Outputs 2e+01
cout.precision(2);
cout << y << '\n'; // Outputs 23
cout.precision(3);
cout << y << '\n'; // Outputs 23.1
More about cout
• Output Manipulators (not a function)
endl - outputs a new line character, flushes output
dec - sets int output to decimal
hex - sets int output to hexadecimal
oct - sets int output to octal
#include <iomanip.h>
int x = 42;
cout << oct << x << endl; // Outputs 52\n
cout << hex << x << endl; // Outputs 2a\n
cout << dec << x << endl; // Outputs 42\n
Example codes reading
(Program 2-2)
• #include <iostream>
• using namespace std;
Welcome. This program adds
• int main (void) three numbers. Enter three numbers
• { in the form: nnn nnn nnn <return>
• int a; 11 22 33
• int b;
• int c; The total is: 66
• int sum;
Thank you. Have a good day.
• cout << "Welcome. This program adds\n";
• cout << "three numbers. Enter three numbers\n";
• cout << "in the form: nnn nnn nnn <return>\n";
• cin >> a >> b >> c;
• // Numbers are now stored in a, b, and c. Add them.
• sum = a + b + c;
• cout << "\nThe total is: " << sum << "\n";
• cout << "\nThank you. Have a good day.\n";
• return 0;
• } // main
Example
Example
Escape Sequence
• Escape sequences are used to
represent certain special Escape
Description
sequence
characters within string
\' single quote
literals and character literals.
\" double quote
\? question mark
\\ backslash
\a audible bell
\b backspace
\n line feed - new line
\r carriage return
\t horizontal tab
More about cout
• precision (int) sets the number of significant
digits of float type numbers
float y = 23.1415;
cout.precision(1);
cout << y << '\n'; // Outputs 2e+01
cout.precision(2);
cout << y << '\n'; // Outputs 23
cout.precision(3);
cout << y << '\n'; // Outputs 23.1
Naming Identifiers
const double a = 2.54; //conversion constant
double x; //variable to hold centimeters
double y; //variable to hold inches
x = y * a;
Consider the following
const double conversion = 2.54;
double centimeters;
double inches;
centimeters = inches * conversion;
• Run-together-word
inchperfoot inchPerFoot inch_per_foot.
Conclusion
In this lecture …
• What is Standard IO Stream?
• What are the function of cin and cout operators?
• How can we use these operators?
ANY QUERY ?
Practice Questions
• PROBLEMS# (24-32)
• PROBLEMS# (33-37)
• From “A structures approach using C++”
Page (72-73)