ITclub

العودة إلى C++
محاضرة 2

User Input (cin) and Arithmetic Operations

استقبال مدخلات من المستخدم والعمليات الحسابية الأساسية

ملخص المحاضرة

📜 Lecture 2: Input, Output, and Arithmetic

This lecture covers how to get input from the user (cin) and perform basic math operations.

Key Concepts

  • Output Formatting:
    • \t: Adds a tab to the output for spacing. Example: cout << "Hello \t world";.
    • \n or endl: Adds a new line.
  • User Input:
    • cin >> variableName;: The standard input command (see-in).
    • The >> operator is the "stream extraction" operator.
    • You can chain inputs: cin >> x >> y; reads two values from the user into x and y.
  • Variables Example: The lecture shows how to declare, assign, and print various data types.
    • bool sucess=true; will output 1 when printed.
  • Arithmetic Operations:
    • + (Addition), - (Subtraction), * (Multiplication).
  • Division (/):
    • Integer Division: If you divide two int variables, the result is also an int. The decimal part is truncated (cut off).
      • Example: int z = 5 / 3; results in z being 1.
    • Floating-Point Division: To get a decimal result, at least one of the numbers must be a float or double.
      • Example: float z = 5.0 / 3; results in z being 1.66667.
  • Modulus (%):
    • Finds the remainder of a division.
    • Example: 7 % 3 is 1.
    • Modulus Rules:
      1. number % same_number = 0 (e.g., 5 % 5 = 0).
      2. small % big = small (e.g., 4 % 5 = 4).
      3. big % small = remainder (e.g., 7 % 6 = 1).