C++ for Engineers and Scientists - Assignment, Formatting, and Interactive Input


Objectives

Assignment Operations

Formatting Numbers for Program Output

Using Mathematical Library Functions

Program Input Using the cin Object

Symbolic Constants

 

Objectives (continued)

Applications

Common Programming Errors

A Closer Look: Programming Errors

 

Assignment Operations

Assignment Statement:  assigns the value of the expression on the right side of the = to the variable on the left side of the =

Another assignment statement using the same variable will overwrite the previous value with the new value

    Examples:

          slope = 3.7;

     slope = 6.28;

Assignment Operations (continued)

Right side of an assignment statement may contain any expression that can be evaluated to  a value

    Examples:

          newtotal = 18.3 + total;

     taxes = .06*amount;

     average = sum / items;

Only one variable can be on the left side of an assignment statement

 

 

Assignment Operations (continued)

Assignment Operations (continued)

Assignment Operations (continued)

Coercion: forcing a data value to another data type

Value of the expression on the right side of an assignment statement will be coerced (converted) to the data type of the variable on the left side during evaluation

Variable on the left side may also be used on the right side of an assignment statement

Assignment Operations (continued)

Assignment Operations (continued)

Additional assignment operators provide short cuts: +=, -=, *=, /=, %=

Example:

     sum = sum + 10;

is equivalent to:   sum += 10;

  price *= rate +1;

is equivalent to:

  price = price * (rate + 1);

Assignment Operations (continued)

Assignment Operations (continued)

Assignment Operations (continued)

Counting statement: adds a fixed value to the variables current value

    Syntax:   

          variable = variable + fixedNumber;

    Example:

          i = i + 1;

     count = count + 1;

 

 

Assignment Operations (continued)

Increment operator ++: unary operator for the special case when a variable is increased by 1

Prefix increment operator appears before the variable

    Example:  ++i

Postfix increment operator appears after the variable

    Example: i++

Assignment Operations (continued)

Example:     k = ++n;  //prefix increment

  is equivalent to

    n = n + 1;  //increment n first

  k = n;      //assign ns value to k

Example:     k = n++;  //postfix increment

 is equivalent to

    k = n;       //assign ns value to k

  n = n + 1;   //and then increment n

Assignment Operations (continued)

Decrement operator --: unary operator for the special case when a variable is decreased by 1

Prefix decrement operator appears before the variable

    Example:  --i;

Postfix decrement operator appears after the variable

    Example:  i--;

Formatting Numbers
for Program Output

Proper output formatting contributes to ease of use and user satisfaction

cout with stream manipulators can control output formatting

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

The field width manipulator must be included for each value in the data stream sent to cout

Other manipulators remain in effect until they are changed

iomanip header file must be included to use manipulators requiring arguments

 

 

Formatting Numbers for
Program Output (continued)

Formatting floating-point numbers requires three field-width manipulators to

Set the total width of the display

Force a decimal place

Set the number of significant digits after the decimal point

    Example:

 

    produces this output:

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

If setprecision value is too large, the fractional value is displayed with its current size

 

 

Formatting Numbers for
Program Output (continued)

 

 

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

Formatting Numbers for Program Output (continued)

Formatting Numbers for
Program Output (continued)

Formatting Numbers for
Program Output (continued)

Using Mathematical Library Functions

C++ has preprogrammed mathematical functions that can be included in a program

You must include the cmath header file:

          #include

Math functions require one or more arguments as input, but will return only one value

All functions are overloaded, and can be used with integer and real arguments

Using Mathematical Library Functions (continued)

Using Mathematical Library Functions (continued)

To use a math function, give its name and pass the input arguments within parentheses

Expressions that can be evaluated to a value can be passed as arguments

Using Mathematical Library Functions (continued)

Using Mathematical Library Functions (continued)

Using Mathematical Library Functions (continued)

Run-time cast: the requested conversion is checked at run time and applied if valid

    Syntax:

          staticCast (expression)

    Example:

          staticCast(a*b)

 

Program Input Using the cin Object

cin Object: allows data entry to a running program

Use of the cin object causes the program to wait for input from the keyboard

When keyboard entry is complete, the program resumes execution, using the entered data

An output statement preceding the cin object statement provides a prompt to the user

 

 

Program Input Using the cin Object (continued)

Program Input Using the cin Object (continued)

Program Input Using the cin Object (continued)

Program Input Using the cin Object (continued)

Symbolic Constants

Symbolic Constants (continued)

Proper placement of statements:

     preprocessor directives

     int main()

     {

          symbolic constants

          main function declarations

     

          other executable statements

          return value

     }

Applications

Common Programming Errors

Common Programming Errors (continued)

Summary

Summary (continued)

Use #include for math functions

Arguments to a function must be passed in the proper number, type, and order

Functions may be included within larger expressions

cin object provides data input from a keyboard; program is suspended until the input arrives

 

 

Summary (continued)

Use a prompt to alert the user to provide input

Constants are named values that do not change

A Closer Look: Programming Errors

A Closer Look: Programming Errors