Loading...

Variables, Data Types and Input/Output in C++

Imagine you're packing your school bag. You put your math book in one pocket, lunch in another, pens in a pencil case, and your water bottle in its own space.

Each item has its place, and each place holds a specific type of thing.

Variables in cpp

Computers work the same way. They store numbers, letters, and information in special “pockets” called variables, and each variable has a data type that tells the computer what kind of information it can hold.

In this lesson, you’ll learn how to store values, do simple math, and accept input from the user.
By the end, you'll create your first interactive program: a Student Age & Score Recorder.

The Project: Student Age and Score Recorder

Imagine you’re building a tiny school system.
The program should:

  1. Ask the student for their name
  2. Ask for their age
  3. Ask for their math score
  4. Print everything back in a neat format

Here is the expected output:

cpp
1
2
3
4
5
6
==============================
   STUDENT INFORMATION
   Name: Ali
   Age: 14
   Math Score: 92
==============================

You’ll gather the information using cin and display it using cout.

Variables and Data Types in C++

A variable is a container that stores information.

C++ gives us several built-in types:

  • int → whole numbers (13, 92, 1500)
  • float → decimal numbers (5.5, 9.8)
  • double → bigger decimals (99.9999)
  • char → single character ('A', 'x')
  • bool → true/false values

A variable needs two things:

  • A data type
  • A name (called an identifier)

Example:

cpp
1
2
3
4
int age = 14;
double score = 92.5;
char grade = 'A';
bool passed = true;

These tell the computer what to store and how to store it.

Input and Output in C++: cin and cout

  • cout → prints information out to the screen
  • cin → takes information in from the user

Example:

cpp
1
2
3
int age;
cout << "Enter your age: ";
cin >> age;

Now whatever the user types gets stored in age.

Literals and Identifiers

  • A literal is the actual value:
    14, 3.5, 'A', "Hello"
  • An identifier is the name you give a variable:
    age, score, studentName

Identifiers cannot start with numbers and must avoid spaces.

Basic Arithmetic in C++

You can perform simple math using operators:

  • + addition
  • - subtraction
  • * multiplication
  • / division

Example:

cpp
1
2
int total = 10 + 5;
int half = total / 2;

Type Conversion

Sometimes you need to convert from one type to another.

Example:

cpp
1
2
double height = 5.8;
int roundedHeight = (int) height;

This turns 5.8 into 5.

Let’s Build the Student Recorder

Here is the full program:

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main() {
    string name;
    int age;
    int score;

    cout << "Enter your name: ";
    cin >> name;

    cout << "Enter your age: ";
    cin >> age;

    cout << "Enter your math score: ";
    cin >> score;

    cout << "==============================" << endl;
    cout << "   STUDENT INFORMATION" << endl;
    cout << "   Name: " << name << endl;
    cout << "   Age: " << age << endl;
    cout << "   Math Score: " << score << endl;
    cout << "==============================" << endl;

    return 0;
}

Run, and see it come alive.

Try asking AI:

“Explain how cin reads input in C++ and what happens if I enter a wrong value.”

How It Works

  • Three variables store the information.
  • cin takes user input and fills the variables.
  • cout prints everything neatly.
  • The program uses data types like string and int to store text and numbers correctly.

In one simple project, you learned how C++ stores, receives, and displays information, the heart of all real applications.

Learn Together with AI

Try these prompts:

  • “Write a C++ program that asks for two numbers and prints their sum.”
  • “Modify my code to calculate the average of three scores.”
  • “Explain the difference between int, float, and double in C++.”

AI will help you understand faster, debug your mistakes, and explore new ideas.

Practice Challenge

Your turn!

Write a C++ program that:

  • Asks the user for:
    • Their name
    • Their class (like 8, 9, 10)
    • Their science score
  • Prints the following format exactly:
text
1
2
3
4
5
6
==============================
   REPORT CARD
   Name: Sara
   Class: 9
   Science Score: 88
==============================

Use variables, cin, and cout to complete it.

If you want help, ask:

“Explain how to print multiple lines in C++ with cout.”

Coming Up Next

Now that you can store information and take user input, the next step is making your program think.

In Lesson 3, you’ll learn about operators and expression in programming.

Frequently Asked Questions

You’ll learn how to use variables, data types, cin, cout, basic arithmetic, and type conversion to build interactive C++ programs.

The main ones you’ll use are int for whole numbers, double or float for decimals, char for single characters, and bool for true/false values.

C++ uses cin to take input from the user and store it inside variables. For example: cin >> age;.

Yes. You can ask AI to explain errors, rewrite code, generate examples, or modify your program instantly.

Still have questions?Contact our support team