Course code: COSC 1020
Institution: Georgetown University
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)
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>
.
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.
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.
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.
Data value errors (invalid input accepted).
Data type errors (non-conformance to expected data types).
Read failures indicate issues that need addressing.
Linux has three standard streams:
stdin (input)
stdout (output)
stderr (error output)
Facilitates output redirection and piping between programs.
Output streams like cout can be customized using manipulators from iomanip:
Adjust precision
Set width
Set alignment
Example of printing aligned tables using setw for width manipulation and setfill for padding characters.
cout << setw(6) << a << setw(7) << b << endl;
Control significant figures for floating point numbers using setprecision.
Applies until changed or reset, default precision is six.
Floating-point numbers can be displayed in different formats: fixed, scientific, or default.
Strings can be manipulated with various member functions such as length, assign, and concatenation using the + operator.
C-style strings are arrays of characters with a terminating null character. C++ strings are recommended for their enhanced simplicity and capabilities.
An expression is a code segment that simplifies to a single value, including literals, variables, and function calls.
Operators include +, -, *, /, and %.
Ensure correct precedence with logical usage of parentheses.
C++ distinguishes numeric types by range and precision.
Type coercion occurs naturally when mixing different numeric types.
Use static_cast<new_type>(value)
to explicitly control type conversion when needed.
Exceeding data limits leads to unexpected results (overflow or underflow).
Best practices call for awareness of numerical types and careful calculations.
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.
In practice, prefer const
over #define
for defining constants.
Assignment can be chained or combined for convenience using operators like +=
, -=
, etc.
Additional mathematical functions are available in the cmath library (e.g., sqrt, log, sin, etc.).