Unit 1: C++ Foundations & Windows 11 SetupLesson #2 / 20

Lesson 2: Primitive Data Types, Variables & User Input with std::cin

NSNextSemLast Updated: 5 Aug, 2026

🔢 Master C++ Data Types & Memory Storage

In C++, every variable is strongly typed. When you declare a variable, the compiler reserves a specific size of memory on the stack to store that data type.


📊 Primitive Data Types Cheat Sheet

  • 🔢 int (4 Bytes): Stores whole integers e.g. int age = 25; (-2,147,483,648 to 2,147,483,647).
  • 🎯 double (8 Bytes): High-precision double floating-point number e.g. double salary = 75430.50;.
  • 🔤 char (1 Byte): Single character enclosed in single quotes e.g. char grade = 'A';.
  • 🛡️ bool (1 Byte): Boolean true or false value e.g. bool isPassed = true;.
  • 📜 std::string (Included via <string>): Text sequence e.g. std::string name = "NextSem Developer";.

  • 💻 Code Example: Reading Input with std::cin

    C++ Source Code (main.cpp)
    #include <iostream>
    #include <string>
    int main() {
    std::string studentName;
    int rollNumber;
    double gpa;
    std::cout << "Enter Student Name: ";
    std::getline(std::cin, studentName); // Read full line including spaces
    std::cout << "Enter Roll Number: ";
    std::cin >> rollNumber;
    std::cout << "Enter GPA: ";
    std::cin >> gpa;
    std::cout << "
    --- Student Report Card ---" << std::endl;
    std::cout << "Name: " << studentName << std::endl;
    std::cout << "Roll No: " << rollNumber << std::endl;
    std::cout << "GPA: " << gpa << " / 4.0" << std::endl;
    return 0;
    }
    Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
    Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)

    Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

    (c) Microsoft Corporation. All rights reserved.

    C:\Users\Student\CPP_Project>#include <iostream>

    C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp C:\Users\Student\CPP_Project> main.exe Hello World from C++ on Windows 11 Pro! [Process exited with code 0 in 0.001 seconds]

    🛠️ Execution Output in ⊞ Windows 11 Command Prompt:

    Command Execution Snippet
    Enter Student Name: Manoj Kumar
    Enter Roll Number: 101
    Enter GPA: 3.92
    --- Student Report Card ---
    Name: Manoj Kumar
    Roll No: 101
    GPA: 3.92 / 4.0
    Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
    Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)

    Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

    (c) Microsoft Corporation. All rights reserved.

    C:\Users\Student\CPP_Project>Enter Student Name: Manoj Kumar

    C:\Users\Student\CPP_Project> Enter Student Name: Manoj Kumar Execution successful! Output verified on Windows 11 Pro x86_64. [Process exited with code 0 in 0.002 seconds]

    > 💡 Developer Joke: Why was the const double variable so confident?

    > Because it knew its value would never change! 😂

    Interactive Knowledge Check

    Test your understanding of Lesson #2 concepts

    Which input stream operator is used with std::cin to read user input into a variable?