Day 3 How to Use Variables in C++: A Beginner’s Guide to Data Types and cin

Hey coder! Welcome back to Day 3 of your C++ learning journey. If you followed along with Day 2: How to Set Up C++ on VS Code (Step-by-Step for Absolute Beginners) you’ve already installed your compiler, written your first “Hello World” program, and run it successfully. That means you’re now ready to take the next big step how to use variables in C++.

Variables are like containers in your code they help you store, change, and reuse information. Whether it’s a user’s age, a total price, or a simple message, variables are how your program remembers stuff.

In this post, you’ll also learn about data types, which decide what kind of value (like a number, letter, or word) can go inside each variable.

By the end of this guide, you’ll not only understand how to declare variables, but you’ll also be writing real C++ programs that respond to user input just like real applications do!

What You’ll Learn

What variables are and how they work in C++

All major C++ data types with examples

How to take input from users using cin

How to write a small, fully working interactive program

Full explanation of every line of code (like a mentor sitting beside you)

1. What Are Variables in C++?

A variable is a name you give to a storage location in memory — like a labeled box that holds some value.

You use variables to:

Store numbers (scores, ages, prices)

Store text (names, cities)

Make calculations

Track user data

Syntax:

int age = 21;

int → Data type (declares what kind of data this is)

age → Variable name

21 → The actual value stored in the variable

C++ Data Types: Full Explanation

Let’s now dive into each main C++ data type used in beginner to intermediate programs:

1. int — Integer (Whole Numbers)

Used for numbers like 1, 25, -9 — anything without decimal.

int score = 100;

• Use int when:

Counting things (people, items, marks)

Working with loops

Math that doesn’t need decimals

2. float — Decimal Numbers (Single Precision)

Used for storing decimal values like 3.14 or 99.9

float price = 9.99;

• Use float when:

You need simple decimal numbers

You’re doing basic financial calculations

❗ Note: Less precise than double, but uses less memory.

3. double — Decimal Numbers (Double Precision)

More accurate than float, useful in calculations needing more precision.

double gpa = 8.92;

• Use double when:

Working with more precise values

Doing scientific or engineering calculations

4. char — Single Character

Stores just one character: letter, digit, or symbol, wrapped in single quotes.

char grade = ‘A’;

• Use char when:

You only need to store a letter or symbol

You’re working with grading, menus, initials

5. string — Sequence of Characters (Words or Sentences)

Stores text. You need to #include at the top.

include

string name = “Anjali”;

• Use string when:

You want to store names, messages, sentences

You’re collecting user input as text

6. bool — Boolean (True/False)

Holds a logical value: true or false.

bool isPassed = true;

• Use bool when:

You need to make yes/no decisions

You’re checking conditions (e.g., login success)

Let’s Put It Together: A Program with All Data Types

#include <iostream>
#include <string>
using namespace std;

int main() {
    int age = 21;
    float height = 5.7;
    double pi = 3.14159;
    char grade = 'A';
    string name = "Ravi";
    bool isStudent = true;

    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Height: " << height << endl;
    cout << "PI Value: " << pi << endl;
    cout << "Grade: " << grade << endl;
    cout << "Is a student? " << isStudent << endl;

    return 0;
}

Line-by-Line Explanation

#include <iostream>

✔️ This lets you use cin and cout for input/output

#include <string>

✔️ Required for using the string data type

using namespace std;

✔️ Avoids needing to write std::cout every time

int main() {

✔️ This is where your program starts running

int age = 21;

✔️ Declares an int variable called age and stores 21 in it

float height = 5.7;

✔️ Declares a decimal number (like 5 feet 7 inches)

double pi = 3.14159;

✔️ More precise decimal — good for math or science

char grade = 'A';

✔️ Stores a single letter, like a grade

string name = "Ravi";

✔️ Holds a word or phrase, such as a person’s name

bool isStudent = true;

✔️ Holds a true or false value — very useful in logic

cout << "Name: " << name << endl;

✔️ Prints “Name: Ravi” on the screen

Repeat this pattern for the other cout lines — it displays each variable’s value.

return 0;
}

✔️ Tells the computer that your program ran successfully

Example: College Student Profile

Suppose you’re building a college form app.

You’d need to:

Get the student’s name → string

Their age → int

Height → float

GPA or some calculation → double

Initial or section → char

Check if they’re currently enrolled → bool

That’s 6 data types in one simple use case. Now imagine building forms, game settings, or online accounts — this is how real programs begin.

Practice Challenge (Write Your Own Program)

Try writing a small program that:

Asks for your name, age, and favorite letter

Stores your weight as float

Stores your ID as int

Stores a boolean like isSubscribed = true

Prints them all out neatly

Wrap-Up: What You Learned

Variables store values in memory with a name and type

Data types decide what kind of value a variable holds

You now know how to use: int, float, double, char, string, and bool

What’s Next?

In Day 4, we’ll go into conditional logic where your program starts making decisions:

Using if, else, switch

Checking user input

Adding logic to calculators, quizzes, and more

It’s when code starts getting smart. 💡

Leave a Comment