Mastering Module 2

Data Types, Variables & Input

Video Overview

This module explores how C++ stores and processes information by mastering Data Types and Variables. You will learn how to declare memory space for different kinds of data from integers to decimals and how to make your programs interactive by capturing user input using the cin statement.

Goal: Data Handling.

Notes

1. Simple Data Types

C++ classifies data into three main categories. Understanding these helps you manage memory efficiently.

Integral (Whole)
  • int: Whole numbers (e.g., 5000). No decimals!
  • bool: Logic values. Stores true (1) or false (0).
  • char: Single character in single quotes 'A'.
Tip: '\n' is the character for a new line.
Floating-Point
  • float: 4 bytes. Precision up to 7 decimals.
  • double: 8 bytes. The standard for decimals (15 digit precision).
Scientific Notation: 3.4E+38
String Type
  • Sequence of characters in " ".
  • Requires header: #include <string>
Boxes appear here...

2. Memory Specs (Bits & Bytes)

Select a data type to inspect its memory allocation size.

Memory Allocation

00

Select Type

Waiting for input...

3. Variables: The Two-Step Process

Understanding how the computer sets aside memory for your data.

MEMORY_SIM.exe
> Status: Ready to allocate...
RAM Visualization:
0x00
0x01
0x02
0x03
0x04
The 2 Steps
  1. Allocation (Declaration): Instructing the computer to reserve a specific amount of memory.
  2. Storage (Assignment): Putting actual data into that reserved memory space.
4 Characteristics
Name Type Size Value
Named Constants

Variables whose value cannot change after creation.

const double PI = 3.14; *Must assign value immediately!
Assignment vs Initialization
Initialization
At birth int i = 10;
Assignment
Later on i = 20;

4. Input/Output Streams

Interaction in C++ uses "Streams"—a flow of data between your program and the user.

Understanding the Flow
  • cin (Console Input):
    Pulls data from the keyboard into a variable. It stops reading at the first space or newline.
  • cout (Console Output):
    Pushes data from the program to the screen.
The Arrow Trick

The arrows point exactly where the data flows!

cin >> var; Into Variable
cout << "Hi"; Out to Screen
// 1. Outputting text
cout << "Enter miles: ";

// 2. Taking User Input
cin >> miles;

// 3. Cascading (Multiple inputs)
cin >> feet >> inches;
Input Buffer

Typing 10 20 stores both in a "waiting room". A single line like cin >> x >> y; grabs both at once!

What is endl?

"End Line". It moves the cursor to the next line and flushes the buffer to ensure text appears immediately.

Module 2 Knowledge Check

Answer the following multiple-choice questions to test your understanding.

Question 1: Which of the following is the correct way to declare an integer variable named score and initialize it to 100?

Explanation: In C++, the syntax for declaration and initialization is dataType variableName = value;. int is the keyword for integers.

Question 2: Which data type should be used to store a single letter, such as 'A' or 'b'?

Explanation: The char (character) data type is used to store a single 8-bit character. It is always written inside single quotes.

Question 3: In C++, which object is used to take input from the keyboard?

Explanation: cin (console input) is the standard input stream, used with the extraction operator >>.

Question 4: What is the result of the following code snippet?

int x = 5;
int y = 2;
cout << x / y;

Explanation: Since both are int, C++ performs integer division, truncating the decimal portion (0.5 is lost).

Question 5: Which of the following is a valid identifier (variable name) in C++?

Explanation: Identifiers can start with a letter or underscore, but never a digit. main is a function name and should be avoided.