Computer Programming 1
Module #2
Name: Class number: _________________
__________________________________________ Date: _________________________
Section: _________ Schedule: ______________________
Lesson Title: C++ Basic I/O Materials:
Lesson Objectives: Student learning module
1. I can identify C++ data Basic Input/Output.
2. I can create my 1st C++ program. References:
www.programiz.com
Productivity Tip:
“Successful and unsuccessful people do not vary greatly in their abilities. They vary in their desires to reach
their potential.” – John Maxwell
“Start strong! Train your brain to shift to work mode by setting a regular time during the day for your lessons.
Set an alarm and stick to your “working hours”
LESSON PREVIEW/REVIEW
Introduction
Input and output, or I/O is the communication between an information
processing system, such as a computer, and the outside world, possibly a
human or another information processing system. Inputs are the signals or data
received by the system and outputs are the signals or data sent from it. In
today’s lesson we will learn about how to process information in programming
using basic input/output commands.
MAIN LESSON
C++ Output
In C++, cout sends formatted output to standard output devices, such as the screen. We use the cout object along
with the << operator for displaying output.
Example 1: String Output
#include <iostream>
using namespace std;
int main() {
/ prints the string enclosed in double quotes cout << "This is C++ Programming"; return 0;
}
Output: This is C++ Programming
How does this program work?
We first include the iostream header file that allows us to display output.
The cout object is defined inside the std namespace. To use the std namespace, we used the using namespace
std; statement.
Every C++ program starts with the main() function. The code execution begins from the start of the main()
function.
cout is an object that prints the string inside quotation marks " ". It is followed by the << operator.
return 0; is the "exit status" of the main() function. The program ends with this statement, however, this
statement is not mandatory.
#include <iostream>
Note: If we don't include the using
namespace std; statement, we need to use int main() {
std::cout instead of cout. / prints the string enclosed in double quotes
std::cout << "This is C++ Programming";
return 0;
}
Example 2: Numbers and Characters Output
#include <iostream> Output:
using namespace std; 70
256.783
int main() { character: A
int num1 = 70;
double num2 = 256.783;
char ch = 'A';
cout << num1 << endl; // print integer
cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char
return 0;
}
C++ Input
In C++, cin takes formatted input from standard input devices such as the keyboard. We use the cin object
along with the >> operator for taking input.
Example 3: Integer Input/Output
#include <iostream> using Output:
namespace std; Enter an integer: 70
The number is: 70
int main() {
int num; In the program, we used cin >> num; to take input
cout << "Enter an integer: "; from the user. The input is stored in the
cin >> num; // Taking input variable num. We use the >> operator with cin to
cout << "The number is: " << num; take input.
return 0; }
Note: If we don't include the using namespace std; statement, we need to use std::cin instead of cin.
Activity 1: Skill-building Activities
Exercises 1: Creating my 1st C++ program.
Instruction: In this activity we will create a c++ program using a compiler. Ask your instructor to guide you and
ask what compiler you will use. After that write the following codes below and check if you have similar
output as show in the figure.
#include <iostream> Output:
using namespace std; Enter a character and an integer: F
23
int main() { Character: F
char a; Number: 23
int num;
cout << "Enter a character and an integer: ";
cin >> a >> num;
cout << "Character: " << a << endl;
cout << "Number: " << num;
return 0;
}