You are on page 1of 6

Input Statements

Subject Code CS 2 Computer Science 2


Module Code 3.0 Programming Basics
Lesson Code 3.5.2 Input Statements
Time Frame 30 minutes

Components Tasks TA ATA


(min) (min)b
a

Target By the end of this learning guide module, the students should be able to: 1

1. Differentiate between input and output


2. Properly use input statements in a program
3. Create a basic program that is able to take in user input and display
output.

Hook The previous module discussed how to display text and variables. That was called 1
output. A lot of times, in order to get the output you want, you first need to give
the computer the correct input. For example, to see your account details and the
content on your social media accounts, you first need to type your username and
password. When searching for something, you first need to type your keywords.
Before the computer can show you something, you need to tell it what you want
first. As mentioned in the previous module, when the flow of the data is from the
user/device (ex. keyboard) to the main memory that is called input.

This module describes the basic method of using input in C++.

Ignite Standard Input (cin) 13

This is used to retrieve input from the device, which is usually the keyboard. The
keyword used is cin which stands for Console INput, meaning you are getting
data from the console (keyboard) to store in the program. (This is usually
pronounced as (see-in).
The cin keyword is paired with the extraction operator ( >> ). This is followed
by the variable where you want to store the data inputted.

CS 2 | Page 1 of 6
In this example, the program asks the user to input an integer. The program will
wait for input from cin and it will not proceed with the rest of the program until
an input is received. Once the user has entered an input, it will be stored in the
variable age.
You may also use multiple extraction operators to get more data in a single
statement. The user may use space or enter to separate the inputs.

When you run the program below, it will only display a blinking cursor. The user
probably does not know what you are looking for or what you want them to do.

That’s why it is important to include cout statements before the cin statements
to prompt the user to input the data needed.

CS 2 | Page 2 of 6
You can input integers, floating-point numbers, single characters, and strings.
Sample code:

Program output:

Sample Code:

Program Output:

Input is dependent on the data type of the variable where you will store it. If you
input something that does not match the data type of the variable, then it might
cause inaccurate values or errors.

Data Type Limitation Example user Value stored in


input memory
Char Reading stops after a abc A
single character
Int Reading stops on 1.5 1
whitespace or non-digit
character
double/ Reading stops on 1.5 1.5
float whitespace or non-digit
character
String Reading stops on Juan Dela Cruz Juan
whitespace

CS 2 | Page 3 of 6
If you input a number with a decimal but want to store it in an integer variable,
then the computer will only store the value until the decimal point. After that it
will stop reading.
If you use the char data type then it will only take in a single character. If you
want to input multiple characters, use the string data type instead. However, since
a string input will stop after the first word/space, to input multiple words you will
need to use a function called getline() which will not be discussed here.

<< vs >>
To remember which operator goes with which keyword, simply notice the
direction of the symbols.
In cout statements, the << points to the cout. This means you are sending the data
from the program to be outputted to the user/display screen.

In cin statements the >> points to the variable name. This means you are sending
the data from the user/keyboard to the program and storing it in the variable.

CS 2 | Page 4 of 6
Navigate 13
Exercises

Part 1.

For the following statements and user inputs, what will be the value stored in
each variable assuming the variables were declared as such:

cin statement User input Value stored


in memory

1. cin >> c; x

2. cin >> c; xy

3. cin >> i; 100


2.25
4. cin >> i >> f; 2.75

5.cin >> c >> i >> f; A 5 1.2


A
6. cin >> c >> i >> f; 5
1.2

Part 2.

Create a program that will ask the user to input their first name, age and height in
centimeters. You may do your code on an IDE or online compiler. The final
output should display:

Hello, (name).
You are (age) years old and are (height) cm in height.

Sample Output:

CS 2 | Page 5 of 6
Knot Summary 2

1. The keyword cin is used to retrieve input in C++ and it is used with the
extraction operator ( >> ).
2. The input is stored in the variable that follows the extraction operator >>
3. The data inputted should match the data type of the variable where it will
be stored or it might cause unexpected results

a
TA – time allocation suggested by the teacher
b
ATA- actual time allocation spent by the student (for information purposes only)

References: using APA format (7th edition)

1. Albarico, J.M. (2013). THINK Framework. Based on Ramos, E.G. and N. Apolinario. (n.d.)
Science LINKS. Quezon City: Rex Bookstore Inc
2. THINK icons and blue car images by Clker-Free-Vector-Images from Pixabay
3. Basic Input/Output. (n.d.). Retrieved July 13, 2020 from
http://www.cplusplus.com/doc/tutorial/basic_io/
4. Basic Input Output (n.d.). Retrieved July 13, 2020 from https://www.geeksforgeeks.org/basic-
input-output-c/
5. Gaddis, T., & Manna, M. M. (2015). Chapter 2 Introduction to C++. In Starting out with C++:
From control structures through objects. Harlow, Essex: Pearson Education Limited.

Prepared by: Reviewed by:

Edlen Mari M. Sanchez Trextan Thaddeus Sanchez


Special Science Teacher II SST III
PSHS-MC PSHS- SMC

CS 2 | Page 6 of 6

You might also like