B

03.ExpressionsAndInteractivity

Computer Science I COSC 1020

  • Course code: COSC 1020

  • Institution: Georgetown University

Chapter 3: Expressions and Interactivity

Overview

  • Focus on interactivity and expressions in programming.

  • Key topics include:

    • The cin Object (3.1)

    • Formatting output (3.7)

    • Working with chars and strings (3.8)

    • Mathematical Expressions (3.2)

    • Data type conversions (3.3)

    • Overflow and underflow (3.4)

    • Named constants (3.5)

    • Multiple and combined assignment (3.6)

    • Math library functions (3.9)

The cin Object

  • An input stream that receives data from external sources (typically standard input from the keyboard).

  • Provided by the iostream library; requires inclusion with #include <iostream>.

Buffering

  • Keystrokes entered by the user go into an input buffer that operates on a FIFO (First In, First Out) basis.

  • The cin object retrieves data from this buffer according to the expected data type.

  • Data remains in the buffer until processed or discarded.

Stream Extraction Operator

  • The extraction operator (>>) retrieves data from the stream and requires a variable to store the input.

  • Supports multiple values in single or multiple statements.

    • Example: int i; cin >> i; or string w, d; cin >> w >> d;

  • Improper inputs (e.g., negatives for positive requests) require validation.

Input Buffer Mechanics

Reading Inputs

  • When reading an integer:

    • Input such as "27" is followed by a newline character.

    • The cin object stops reading after it can no longer form a valid integer.

  • Reading strings ignores leading whitespace and collects characters until a whitespace is encountered.

Error Handling

Types of Errors

  • Data value errors (invalid input accepted).

  • Data type errors (non-conformance to expected data types).

  • Read failures indicate issues that need addressing.

Standard Streams in Linux

  • Linux has three standard streams:

    • stdin (input)

    • stdout (output)

    • stderr (error output)

  • Facilitates output redirection and piping between programs.

Formatting Output

Output Manipulators

  • Output streams like cout can be customized using manipulators from iomanip:

    • Adjust precision

    • Set width

    • Set alignment

Printing Tables

  • Example of printing aligned tables using setw for width manipulation and setfill for padding characters.

    • cout << setw(6) << a << setw(7) << b << endl;

Numeric Display Precision

Setting Precision

  • Control significant figures for floating point numbers using setprecision.

  • Applies until changed or reset, default precision is six.

Different Display Formats

  • Floating-point numbers can be displayed in different formats: fixed, scientific, or default.

C++ String Operations

  • Strings can be manipulated with various member functions such as length, assign, and concatenation using the + operator.

C-style Strings

  • C-style strings are arrays of characters with a terminating null character. C++ strings are recommended for their enhanced simplicity and capabilities.

Expressions

Defining Expressions

  • An expression is a code segment that simplifies to a single value, including literals, variables, and function calls.

Mathematical Expressions

  • Operators include +, -, *, /, and %.

  • Ensure correct precedence with logical usage of parentheses.

Data Type Conversions

Converting Between Types
  • C++ distinguishes numeric types by range and precision.

  • Type coercion occurs naturally when mixing different numeric types.

Type Casting

  • Use static_cast<new_type>(value) to explicitly control type conversion when needed.

Handling Overflow and Underflow

  • Exceeding data limits leads to unexpected results (overflow or underflow).

  • Best practices call for awareness of numerical types and careful calculations.

Named Constants

Importance

  • Use named variables instead of magic numbers for clarity.

  • Constants are declared using the keyword const, initialized at declaration, and not re-assignable post-declaration.

Preprocessor Constants

  • In practice, prefer const over #define for defining constants.

Assignment Techniques

Combined Assignment

  • Assignment can be chained or combined for convenience using operators like +=, -=, etc.

Math Library Functions

  • Additional mathematical functions are available in the cmath library (e.g., sqrt, log, sin, etc.).