0% found this document useful (0 votes)
12 views

Lecture 4 Basic Input-Output Programming

This document is a lecture on basic input and output (I/O) programming in C++. It introduces the cout and cin stream objects for output and input to the screen and keyboard. It explains the insertion and extraction operators << and >> and how to use endl and \n to add newlines when outputting. The document provides examples of basic I/O programs and exercises for students to write programs that input values, perform calculations, and output results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture 4 Basic Input-Output Programming

This document is a lecture on basic input and output (I/O) programming in C++. It introduces the cout and cin stream objects for output and input to the screen and keyboard. It explains the insertion and extraction operators << and >> and how to use endl and \n to add newlines when outputting. The document provides examples of basic I/O programs and exercises for students to write programs that input values, perform calculations, and output results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Basic Input / Output

Lecture 4
PROGRAMMING 1

Engr. Jennelyn P. Cabale


Tasks

Pre Task Dev C++

While Task Lecture Demonstration

Post Task Exercises

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 2


Familiarize with Apply arithmetic,
the input and relational and
output syntax logical operators

Write a basic
Objectives input-output
program

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 3


Standard
Stream
library stream description
standard input
cin
An entity where a stream
program can either Defines a handful of
stream objects that standard output
insert or extract cout
can be used to stream
characters to/from.
access standard standard error
sources and cerr
destinations of (output) stream
Source/destination of characters by the standard logging
characters, and are environment where clog
provided/accepted (output) stream
the program runs
sequentially

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 4


cout - C++ stream
Screen - standard << - insertion
object defined to
output by default operator
access the screen

cout << "Output sentence"; cout << 120; cout << x;

cout << "Hello";



cout << Hello;

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 5


cout << "This " << " is a " << "single C++ statement“

cout << "I am " << age << " years old and my zipcode is " << zipcode;

cout does not automatically add line breaks at the end,


unless instructed to do so.

• cout << "This is a sentence.";


• cout << "This is another sentence.";
• Output: This is a sentence.This is another sentence.

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 6


\n cout << "First sentence.\n"; First sentence.
New-line cout << "Second Second sentence.
character sentence.\nThird sentence."; Third sentence.
Adds line break

cout << "First


sentence." << First
endl - used endl; sentence.
to break Second
cout << "Second
lines sentence.
sentence." <<
endl;

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 7


cin - C++ stream
Keyboard -
object defined to
standard input by
access the
default
keyboard

>> - extraction int age;


operator cin >> age;

Makes the
ENTER or RETURN
program wait for
key is pressed
input from cin.

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 8


cin >> a >> b; cin >> a ; cin >> b;

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 9


cin extraction always
string mystring; considers spaces
(whitespaces, tabs, new-
cin >> mystring; line...) as terminating the
value being extracted

getline - takes the stream


(cin) as first argument,
and the string variable as
second.

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 10


Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 11
<sstream> - standard stringstream allows
header that defines a a string to be
type called stringstream treated as a stream

most useful to convert string mystr ("1204");


strings to numerical int myint;
values and vice versa. stringstream(mystr) >> myint;

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 12


Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 13
Create a program that will accept the
length of the side of a square and print
the Perimeter and the Area. The Create a program that will accept a
formula for computing the perimeter of length value in meters and print its
a square is perimeter = 4 * s and area is equivalent length in yards.
area = s^2; where s is the length of the
side of the square.

Write a program tat exchanges the

Examples: Create a program that will accept the


length of the base and height of a
triangle and print the area.
value of two variables : x and y. The
output must be the value of variable y
will become the value of variable x and
vice versa.

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 14


THE END

Engr. Jenn P. Cabale Lecture 4 - BASIC INPUT/OUTPUT PROGRAMMING 20

You might also like