Chapter 3. Expressions and Interactivity

1 / 129
About This Presentation
Title:

Chapter 3. Expressions and Interactivity

Description:

The program tracks the inventory of three widget stores // that opened at the same time. ... number of widgets each store has sold from its inventory, ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 130
Provided by: cathe100

less

Transcript and Presenter's Notes

Title: Chapter 3. Expressions and Interactivity


1
Chapter 3. Expressions and Interactivity
2
3.1 The cin Object
  • The cin object reads information types at the
    keyboard.
  • cin is the standard input object
  • Notice the gtgt and ltlt operators appear to point in
    the direction information is flowing.

3
Program 3-1
  • include ltiostream.hgt
  • void main(void)
  • int length, width, area
  • cout ltlt"This program calculates the area of a
    rectangle.\n"
  • cout ltlt"What is the length of the rectangle? "
  • cingtgtlength
  • cout ltlt"What is the width of the rectangle? "
  • cingtgtwidth
  • area length width
  • cout ltlt"The area of the rectangle is " ltlt area
    ltlt ".\n"

4
Program Output
  • This program calculates the area of a rectangle.
  • What is the length of the rectangle? 10 Enter
  • What is the width of the rectangle? 20 Enter
  • The area of the rectangle is 200.

5
Program 3-2
  • // This program reads the length and width of a
    rectangle.
  • // It calculates the rectangle's area and
    displays
  • // the value on the screen.
  • include ltiostream.hgt
  • void main(void)
  • int length, width, area
  • cin gtgt length
  • cin gtgt width
  • area length width
  • cout ltlt "The area of the rectangle is " ltlt area
    ltlt endl

6
Entering Multiple Values
  • The cin object may be used to gather multiple
    values at once.

7
Program 3-3
  • include ltiostream.hgt
  • void main(void)
  • int length, width, area
  • cout ltlt"This program calculates the area of a
    rectangle.\n"
  • cout ltlt"Enter the length and width of the
    rectangle separated by a space. \n"
  • cin gtgt lengthgtgt width
  • area length width
  • cout ltlt"The area of the rectangle is " ltlt area
    ltlt endl

8
Program Output
  • This program calculates the area of a rectangle.
  • Enter the length and width of the rectangle
    separated by a space.
  • 10 20 Enter
  • The area of the rectangle is 200

9
Program 3-4
  • // This program demonstrates how cin can read
    multiple values
  • // of different data types.
  • include ltiostream.hgt
  • void main(void)
  • int whole
  • float fractional
  • char letter
  •   cout ltlt "Enter an integer, a float, and a
    character "
  • cin gtgt whole gtgt fractional gtgt letter
  • cout ltlt "whole " ltlt whole ltlt endl
  • cout ltlt "fractional " ltlt fractional ltlt endl
  • cout ltlt "letter " ltlt letter ltlt endl

10
Program Output
  • Enter an integer, a float, and a character 4 5.7
    b Enter
  • whole 4
  • fractional 5.7
  • letter b

11
Reading Strings
  • cin can read strings as well as numbers.
  • Strings are stored in character arrays.
  • char Company12

12
Program 3-5
  • include ltiostream.hgt
  • includeltstringgt
  • void main(void)
  • string name
  • cout ltlt "What is your name? "
  • cin gtgt name
  • cout ltlt "Good morning " ltlt name ltlt endl

13
Program Output
  • What is your name? Charlie Enter
  • Good morning Charlie

14
Program 3-6
  • // This program demonstrates how cin can read a
  • // string into a character array
  • include ltiostream.hgt
  • void main(void)
  • char name21
  • cout ltlt "What is your name? "
  • cin gtgt name
  • cout ltlt "Good morning " ltlt name ltlt endl

15
Program Output
  • What is your name? Charlie Enter
  • Good morning Charlie

16
Program 3-7
  • // This program reads two strings into two
    character arrays.
  • include ltiostream.hgt
  • void main(void)
  • char first16, last16
  • cout ltlt "Enter your first and last names and I
    will\n"
  • cout ltlt "reverse them.\n"
  • cin gtgt first gtgt last
  • cout ltlt last ltlt ", " ltlt first ltlt endl

17
Program Output
  • Enter your first and last names and I will
  • reverse them.
  • Johnny Jones Enter
  • Jones, Johnny

18
Notes on strings
  • If a character array is intended to hold strings,
    it must be at least one character larger than the
    largest string that will be stored in it.
  • The cin object will let the user enter a string
    larger than the array can hold. If this happens,
    the string will overflow the arrays boundaries
    and destroy other information in memory.
  • If you wish the user to enter a string that has
    spaces in it, you cannot use this input method.

19
3.2 Focus on Software Engineering Mathematical
Expressions
  • C allows you to construct complex mathematical
    expressions using multiple operators and grouping
    symbols.

20
Program 3-7
  • // This program reads two strings into two
    character arrays
  • include ltiostream.hgt
  • void main(void)
  • char first16, last16
  •   cout ltlt Enter your first and last names and I
    will\n"
  • cout ltlt "reverse them.\n"
  • cin gtgt first gtgt last
  • cout ltlt last ltlt ", " ltlt first ltlt endl

21
Program 3-7 Output with Example Input
  • Enter your first and last names and I will
  • reverse them.
  • Johnny Jones Enter
  • Jones, Johnny

22
Program 3-8
  • // This program asks the user to enter the
    numerator
  • // and denominator of a fraction and it displays
    the
  • // decimal value
  • include ltiostream.hgt
  • void main(void)
  • float numerator, denominator
  • cout ltlt "This program shows the decimal value of
    "
  • cout ltlt "a fraction.\n"
  • cout ltlt Enter the numerator
  • cin gtgt numerator
  • cout ltlt Enter the denominator
  • cin gtgt denominator
  • cout ltlt The decimal value is
  • cout ltlt (numerator / denominator)

23
Program Output for Program 3-8 with Example Input
  • This program shows the decimal value of a
    fraction.
  • Enter the numerator 3 Enter
  • Enter the denominator 6 Enter
  • The decimal value is 0.1875

24
Table 3-1 Precedence of Arithmetic Operators
(Highest to Lowest)
  • (unary negation) - / -

25
Table 3-2 Some Expressions
26
Associativity
  • If two operators sharing an operand have the same
    precedence, they work according to their
    associativity, either right to left or left to
    right

27
(No Transcript)
28
Converting Algebraic Expressions to Programming
Statements
29
No Exponents Please!
  • C does not have an exponent operator.
  • Use the pow() library function to raise a number
    to a power.
  • Will need include ltmath.hgt for pow() function.
  • area pow(4,2) // will store 42 in area

30
Program 3-9
  • // This program calculates the area of a circle.
  • // The formula for the area of a circle is Pi
    times
  • // the radius squared. Pi is 3.14159.
  • include ltiostream.hgt
  • include ltmath.hgt // needed for the pow
    function
  • void main(void)
  • double area, radius
  • cout ltlt "This program calculates the area of a
    circle.\n"
  • cout ltlt "What is the radius of the circle? "
  • cin gtgt radius
  • area 3.14159 pow(radius,2)
  • cout ltlt "The area is " ltlt area

31
Program 3-9 Output With Example Input
  • This program calculates the area of a circle.
  • What is the radius of the circle? 10Enter
  • The area is 314.159

32
3.3 When you Mix Apples and Oranges Type
Coercion
  • When an operators operands are of different data
    types, C will automatically convert them to the
    same data type.

33
Type Coercion Rules
  • Rule 1 Chars, shorts, and unsigned shorts are
    automatically promoted to int.
  • Rule 2 When an operator works with two values of
    different data types, the lower-ranking value is
    promoted to the type of the higher-ranking value.
  • Rule 3 When the final value of an expression is
    assigned to a variable, it will be converted to
    the data type of the variable.

34
3.4 Overflow and Underflow
  • When a variable is assigned a value that is too
    large or too small in range for that variables
    data type, the variable overflows or underflows.
  • Overflow - when a variable is assigned a number
    that is too large for its data type
  • Underflow - when a variable is assigned a number
    that is too small for its data type

35
Program 3-10
  • // This program demonstrates integer underflow
    and
  • // overflow
  • include ltiostream.hgt
  • void main(void)
  • short testVar 32767
  • cout ltlt testVar ltlt endl
  • testVar testVar 1
  • cout ltlt testVar ltlt endl
  • testVar testVar - 1
  • cout ltlt testVar ltlt endl

36
Program Output
  • 32767
  • -32768
  • 32767

37
Program 3-11
  • // This program can be used to see how your
    system
  • // handles floating point overflow and underflow.
  • include ltiostream.hgt
  • void main(void)
  • float test
  • test 2.0e38 1000 // Should overflow test
  • cout ltlt test ltlt endl
  • test 2.0e-38 / 2.0e38
  • cout ltlt test ltlt endl

38
3.5 The Typecast Operator
  • The typecast operator allows you to perform
    manual data type conversion.
  • Val int(number) //If number is a floating
  • //point variable, it will be
  • //truncated to an integer and
  • //stored in the variable Val

39
Program 3-12
  • include ltiostream.hgt
  • void main(void)
  • int months, books
  • float perMonth
  • cout ltlt "How many books do you plan to read? "
  • cin gtgt books
  • cout ltlt "How many months will it take you to
    read them? "
  • cin gtgt months
  • perMonth float(books) / months
  • cout ltlt "That is " ltlt perMonth ltlt " books per
    month.\n"

40
Program Output
  • How many books do you plan to read? 30 Enter
  • How many months will it take you to read them? 7
    Enter
  • That is 4.285714 books per month.

41
Typecast Warnings
  • In Program 3-11, the following statement would
    still have resulted in integer division
  • perMonth float(books / months)
  • Because the division is performed first and the
    result is cast to a float.

42
Program 3-13
  • // This program uses a typecast operator to
    print
  • // a character from a number.
  •  
  • include ltiostream.hgt
  • void main(void)
  • int number 65
  • cout ltlt number ltlt endl
  • cout ltlt char(number) ltlt endl

43
Program Output
  • 65
  • A

44
3.6 The Power of Constants
  • Constants may be given names that symbolically
    represent them in a program.

45
Program 3-14
  • // This program calculates the area of a circle.
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • const float pi 3.14159
  • double area, radius
  • cout ltlt "This program calculates the area of a
    circle.\n"
  • cout ltlt "What is the radius of the circle? "
  • cin gtgt radius
  • area pi pow(radius,2)
  • cout ltlt "The area is " ltlt area

46
The define Directive
  • The older C-style method of creating named
    constants is with the define directive, although
    it is preferable to use the const modifier.
  • define PI 3.14159
  • is roughly the same as
  • const float PI3.14159

47
Program 3-15
  • include ltiostream.hgt
  • include ltmath.hgt // needed for pow function
  • define PI 3.14159
  • void main(void)
  • double area, radius
  • cout ltlt "This program calculates the area of a
    circle.\n"
  • cout ltlt "What is the radius of the circle? "
  • cin gtgt radius
  • area PI pow(radius, 2)
  • cout ltlt "The area is " ltlt area
  •  

48
3.7 Multiple Assignment and Combined Assignment
  • Multiple assignment means to assign the same
    value to several variables with one statement.
  • A B C D 12
  • Store1 Store2 Store3 BegInv

49
Program 3-16
  • // The program tracks the inventory of three
    widget stores
  • // that opened at the same time. Each store
    started with the
  • // same number of widgets in inventory. By
    subtracting the
  • // number of widgets each store has sold from its
    inventory,
  • // the current inventory can be calculated.
  • include ltiostream.hgt
  • void main(void)
  • int begInv, sold, store1, store2, store3
  • cout ltlt One week ago, 3 new widget stores
    opened\n"
  • cout ltlt at the same time with the same
    beginning\n"
  • cout ltlt inventory. What was the beginning
    inventory? "
  • cin gtgt begInv
  • store1 store2 store3 begInv

50
Program 3-16 Continued
  • cout ltlt "How many widgets has store 1 sold? "
  • cin gtgt sold
  • store1 store1 sold //Subtract sold from
    store1
  • cout ltlt "How many widgets has store 2 sold? "
  • cin gtgt sold
  • store2 store2 sold //Subtract sold from
    store2
  • cout ltlt "How many widgets has store 3 sold? "
  • cin gtgt sold
  • store3 store3 sold //Subtract sold from
    store 3
  • cout ltlt "Store1 " ltlt store1 ltlt endl
  • cout ltlt "Store2 " ltlt store2 ltlt endl
  • cout ltlt "Store3 " ltlt store3 ltlt endl

51
Program 3-16 Output with Example Input
One week ago, 3 new widget stores opened at the
same time with the same beginning inventory. What
was the beginning inventory? 100 Enter How many
widgets has store 1 sold? 25 Enter How many
widgets has store 2 sold? 15 Enter How many
widgets has store 3 sold? 45 Enter The current
inventory of each store Store 1 75 Store 2
85 Store 3 55
52
Table 3-8
53
Table 3-9
54
Program 3-17
  • // The program tracks the inventory of three
    widget stores
  • // that opened at the same time. Each store
    started with the
  • // same number of widgets in inventory. By
    subtracting the
  • // number of widgets each store has sold from its
    inventory,
  • // the current inventory can be calculated.
  • include ltiostream.hgt
  • void main(void)
  • int begInv, sold, store1, store2, store3
  • cout ltlt One week ago, 3 new widget stores
    opened\n"
  • cout ltlt at the same time with the same
    beginning\n"
  • cout ltlt inventory. What was the beginning
    inventory? "
  • cin gtgt begInv
  • store1 store2 store3 begInv

55
Program 3-17 Continued
  • cout ltlt "How many widgets has store 1 sold? "
  • cin gtgt sold
  • store1 - sold //Subtract sold from store1
  • cout ltlt "How many widgets has store 2 sold? "
  • cin gtgt sold
  • store2 - sold //Subtract sold from store2
  • cout ltlt "How many widgets has store 3 sold? "
  • cin gtgt sold
  • store3 - sold //Subtract sold from store 3
  • cout ltlt "Store1 " ltlt store1 ltlt endl
  • cout ltlt "Store2 " ltlt store2 ltlt endl
  • cout ltlt "Store3 " ltlt store3 ltlt endl

56
Program 3-17 Output with Example Input
One week ago, 3 new widget stores opened at the
same time with the same beginning inventory. What
was the beginning inventory? 100 Enter How many
widgets has store 1 sold? 25 Enter How many
widgets has store 2 sold? 15 Enter How many
widgets has store 3 sold? 45 Enter The current
inventory of each store Store 1 75 Store 2
85 Store 3 55
57
3.8 Formatting Output
  • The cout object provides ways to format data as
    it is being displayed. This affects the way data
    appears on the screen.

58
Program 3-18
  • includeltiostream.hgt 
  • void main(void)
  • int num1 2897, num2 5,num3 837, num4 34,
    num5 7,
  • num6 1623, num7 390, num8 3456,
    num9 12
  • // Display the first row of numbers
  • cout ltlt num1 ltlt " "
  • cout ltlt num2 ltlt " "
  • cout ltlt num3 ltlt endl
  • // Display the second row of numbers
  • cout ltlt num4 ltlt " "
  • cout ltlt num5 ltlt " "
  • cout ltlt num6 ltlt endl
  • // Display the third row of numbers
  • cout ltlt num7 ltlt " "
  • cout ltlt num8 ltlt " "
  • cout ltlt num9 ltlt endl

59
Program Output
  • 2897 5 837
  • 34 7 1623
  • 390 3456 12

60
Program 3-19
  • // This program displays three rows of numbers.
  •  include ltiostream.hgt
  • include ltiomanip.hgt // for setw function
  • void main(void)
  • int num1 2897, num2 5, num3 837,
  • num4 34, num5 7, num6 1623,
  • num7 390, num8 3456, num9 12
  • // Display the first row of numbers
  • cout ltlt setw(4) ltlt num1 ltlt " "
  • cout ltlt setw(4) ltlt num2 ltlt " "
  • cout ltlt setw(4) ltlt num3 ltlt endl

61
Program continues
  • // Display the second row of numbers
  • cout ltlt setw(4) ltlt num4 ltlt " "
  • cout ltlt setw(4) ltlt num5 ltlt " "
  • cout ltlt setw(4) ltlt num6 ltlt endl
  • // Display the third row of numbers
  • cout ltlt setw(4) ltlt num7 ltlt " "
  • cout ltlt setw(4) ltlt num8 ltlt " "
  • cout ltlt setw(4) ltlt num9 ltlt endl

62
Program Output
  • 2897 5 837
  • 34 7 1623
  • 390 3456 12

63
Program 3-20
  • // This program demonstrates the setw manipulator
    being
  • // used with values of various data types.
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • int intValue 3928
  • float floatValue 91.5
  • char cStringValue "John J. Smith"
  • cout ltlt "(" ltlt setw(5) ltlt intValue ltlt ")" ltlt
    endl
  • cout ltlt "(" ltlt setw(8) ltlt floatValue ltlt ")" ltlt
    endl
  • cout ltlt "(" ltlt setw(16) ltlt cStringValue
  • ltlt ")" ltlt endl

64
Program Output
  • ( 3928)
  • ( 91.5)
  • ( John J. Smith)

65
Precision
  • Floating point values may be rounded to a number
    of significant digits, or precision, which is the
    total number of digits that appear before and
    after the decimal point.

66
Program 3-21
  • // This program demonstrates how setprecision
    rounds a
  • // floating point value
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • float quotient, number1 132.364, number2
    26.91
  • quotient number1 / number2
  • cout ltlt quotient ltlt endl
  • cout ltlt setprecision(5) ltlt quotient ltlt endl
  • cout ltlt setprecision(4) ltlt quotient ltlt endl
  • cout ltlt setprecision(3) ltlt quotient ltlt endl
  • cout ltlt setprecision(2) ltlt quotient ltlt endl
  • cout ltlt setprecision(1) ltlt quotient ltlt endl

67
Program Output
  • 4.91877
  • 4.9188
  • 4.919
  • 4.92
  • 4.9
  • 5

68
Table 3-11
69
Program 3-22
  • // This program asks for sales figures for 3
    days. The
  • // total sales is calculated and displayed in a
    table
  • include ltiostream.hgt
  • include ltiomanip.hgt
  •  
  • void main(void)
  • float day1, day2, day3, total
  •  
  • cout ltlt "Enter the sales for day 1 "
  • cin gtgt day1
  • cout ltlt "Enter the sales for day 2 "
  • cin gtgt day2

70
Program Continues
  • cout ltlt "Enter the sales for day 3 "
  • cin gtgt day3
  • total day1 day2 day3
  • cout ltlt "\nSales Figures\n"
  • cout ltlt "-------------\n"
  • cout ltlt setprecision(5)
  • cout ltlt Day 1 " ltlt setw(8) ltlt day1 ltlt endl
  • cout ltlt Day 2 " ltlt setw(8) ltlt day2 ltlt endl
  • cout ltlt Day 3 " ltlt setw(8) ltlt day3 ltlt endl
  • cout ltlt Total " ltlt setw(8) ltlt total ltlt endl

71
Program Output
  • Enter the sales for day 1 321.57 Enter
  • Enter the sales for day 2 269.62 Enter
  • Enter the sales for day 3 307.77 Enter
  •  
  • Sales Figures
  • -------------
  • Day 1 321.57
  • Day 2 269.62
  • Day 3 307.77
  • Total 898.96

72
Program 3-23
  • //This program asks for sales figures for 3 days.
    The
  • // total sales is calculated and displayed in a
    table.
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • float day1, day2, day3, total
  • cout ltlt "Enter the sales for day 1 "
  • cin gtgt day1
  • cout ltlt "Enter the sales for day 2 "
  • cin gtgt day2
  • cout ltlt "Enter the sales for day 3 "
  • cin gtgt day3
  • total day1 day2 day3

73
Program Continues
  • cout ltlt "\nSales Figures\n"
  • cout ltlt "------\n"
  • cout ltlt setprecision(2) ltlt setiosflags(iosfixed
    )
  • cout ltlt Day 1 " ltlt setw(8) ltlt day1 ltlt endl
  • cout ltlt Day 2 " ltlt setw(8) ltlt day2 ltlt endl
  • cout ltlt Day 3 " ltlt setw(8) ltlt day3 ltlt endl
  • cout ltlt "Total " ltlt setw(8) ltlt total ltlt endl

74
Program Output
  • Enter the sales for day 1 1321.87 Enter
  • Enter the sales for day 2 1869.26 Enter
  • Enter the sales for day 3 1403.77 Enter
  • Sales Figures
  • -------------
  • Day 1 1321.87
  • Day 2 1869.26
  • Day 3 1403.77
  • Total 4594.9

75
IOS Flags
  • setiosflags manipulator can be used to format
    output in a variety of ways, depending on which
    flag is specified.
  • setiosflags(iosfixed) will cause all subsequent
    numbers to be printed in fixed point notation

76
Program 3-24
  • // This program asks for sales figures for 3
    days. The
  • // total sales is calculated and displayed in a
    table.
  •  
  • include ltiostream.hgt
  • include ltiomanip.hgt
  •  void main(void)
  • float day1, day2, day3, total
  • cout ltlt "Enter the sales for day 1 "
  • cin gtgt day1
  • cout ltlt "Enter the sales for day 2 "
  • cin gtgt day2

77
Program continues
  • cout ltlt "Enter the sales for day 3 "
  • cin gtgt day3
  • total day1 day2 day3
  • cout ltlt "\nSales Figures\n"
  • cout ltlt "-------------\n"
  • cout ltlt setprecision(2)
  • ltlt setiosflags(iosfixed iosshowpoint)
  • cout ltlt Day 1 " ltlt setw(8) ltlt day1 ltlt endl
  • cout ltlt Day 2 " ltlt setw(8) ltlt day2 ltlt endl
  • cout ltlt Day 3 " ltlt setw(8) ltlt day3 ltlt endl
  • cout ltlt "Total " ltlt setw(8) ltlt total ltlt endl

78
Program Output
  • Enter the sales for day 1 2642.00 Enter
  • Enter the sales for day 2 1837.20 Enter
  • Enter the sales for day 3 1963.00 Enter
  •  
  • Sales Figures
  • -------------
  • Day 1 2642.00
  • Day 2 1837.20
  • Day 3 1963.00
  • Total 6442.20

79
Table 3-12
80
Formatting Output With Member Functions
  • cout.width(5) //calls the width member
  • // function, same as
  • // setw(5)
  • cout.precision(2) //sets precision to
  • // 2 significant
  • // digits
  • // The next statement works like
  • // setiosflags(iosfixed)
  • cout.setf(iosfixed)

81
Program 3-25
  • // This program asks for sales figures for 3
    days. The
  • // total sales is calculated and displayed in a
    table.
  •  
  • include ltiostream.hgt
  • include ltiomanip.hgt
  •  
  • void main(void)
  • float day1, day2, day3, total
  •  
  • cout ltlt "Enter the sales for day 1 "
  • cin gtgt day1
  • cout ltlt "Enter the sales for day 2 "
  • cin gtgt day2
  • cout ltlt "Enter the sales for day 3 "
  • cin gtgt day3

82
Program continues
  • total day1 day2 day3
  • cout.precision(2)
  • cout.setf(iosfixed iosshowpoint)
  • cout ltlt "\nSales Figures\n"
  • cout ltlt "-------------\n"
  • cout ltlt Day 1 "
  • cout.width(8)
  • cout ltlt day1 ltlt endl
  • cout ltlt Day 2 "
  • cout.width(8)
  • cout ltlt day2 ltlt endl

83
Program continues
  • cout ltlt Day 3 "
  • cout.width(8)
  • cout ltlt day3 ltlt endl
  • cout ltlt "Total "
  • cout.width(8)
  • cout ltlt total ltlt endl

84
Program Output
  • Enter the sales for day 1 2642.00 Enter
  • Enter the sales for day 2 1837.20 Enter
  • Enter the sales for day 3 1963.00 Enter
  •  
  • Sales Figures
  • -------------
  • Day 1 2642.00
  • Day 2 1837.20
  • Day 3 1963.00
  • Total 6442.20

85
Program 3-26
  • // This program asks for sales figures for 3
    days. The
  • // total sales is calculated and displayed in a
    table.
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • float day1, day2, day3, total
  •  
  • cout ltlt "Enter the sales for day 1 "
  • cin gtgt day1
  • cout ltlt "Enter the sales for day 2 "
  • cin gtgt day2
  • cout ltlt "Enter the sales for day 3 "
  • cin gtgt day3

86
Program continues
  • total day1 day2 day3
  • cout.precision(2)
  • cout.setf(iosfixed iosshowpoint)
  • cout ltlt "\nSales Figures\n"
  • cout ltlt "-------------\n"
  • cout ltlt Day 1 " ltlt setw(8) ltlt day1 ltlt endl
  • cout ltlt Day 2 " ltlt setw(8) ltlt day2 ltlt endl
  • cout ltlt Day 3 " ltlt setw(8) ltlt day3 ltlt endl
  • cout ltlt "Total " ltlt setw(8) ltlt total ltlt endl
  • return 0

87
Program Output
  • Enter the sales for day 1 2642.00 Enter
  • Enter the sales for day 2 1837.20 Enter
  • Enter the sales for day 3 1963.00 Enter
  •  
  • Sales Figures
  • -------------
  • Day 1 2642.00
  • Day 2 1837.20
  • Day 3 1963.00
  • Total 6442.20

88
Table 3-13
89
3.9 Formatted Input
  • The cin object provides ways of controlling
    string and character input.

90
Program 3-27
  • // This program uses setw with the cin object.
  •  
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • char word5
  •  
  • cout ltlt "Enter a word "
  • cin gtgt setw(5) gtgt word
  • cout ltlt "You entered " ltlt word ltlt endl

91
Program 3-28
  • // This program uses cin's width member function.
  •  
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • char word5
  •  
  • cout ltlt "Enter a word "
  • cin.width(5)
  • cin gtgt word
  • cout ltlt "You entered " ltlt word ltlt endl

92
Program Output for Programs 3-27 and 3-28
  • Enter a word Eureka Enter
  • You entered Eure

93
Important points about the way cin handles field
widths
  • The field width only pertains to the very next
    item entered by the user.
  • cin stops reading input when it encounters a
    whitespace character. Whitespace characters
    include the Enter key, space, and tab.

94
Reading a Line of Input
  • cin.getline(line, 20)

95
Program 3-29
  • // This program demonstrates cin's getline member
    function.
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • char sentence81
  • cout ltlt "Enter a sentence "
  • cin.getline(sentence, 81)
  • cout ltlt "You entered " ltlt sentence ltlt endl

96
Program Output
  • Enter a sentence To be, or not to be, that is
    the question. Enter
  • You entered To be, or not to be, that is the
    question.

97
Program 3-30
  • include ltiostream.hgt
  • void main(void)
  • char ch
  • cout ltlt "Type a character and press Enter "
  • cin gtgt ch
  • cout ltlt "You entered " ltlt ch ltlt endl
  • Program Output with Example Input
  • Type a character and press Enter A Enter
  • You entered A

98
Program 3-31
  • include ltiostream.hgt
  • void main(void)
  • char ch
  • cout ltlt "This program has paused. Press enter to
    continue."
  • cin.get(ch)
  • cout ltlt "Thank you!" ltlt endl
  • Program Output
  • This program has paused. Press Enter to continue.
    Enter
  • Thank you!

99
Program 3-32
  • include ltiostream.hgt
  • void main(void)
  • char ch
  •   cout ltlt "Type a character and press Enter "
  • cin.get(ch)
  • cout ltlt "You entered " ltlt ch ltlt endl
  • cout ltlt "Its ASCII code is " ltlt int(ch) ltlt endl
  •  

100
Program Output
  • Type a character and press Enter Enter
  • You entered
  •  
  • Its ASCII code is 10

101
Mixing cin gtgt and cin.get
  • Mixing cin.get with cin gtgt can cause an annoying
    and hard-to-find problem.
  • Pressing the Enter key after inputting a number
    will cause the newline character to be stored in
    the keyboard buffer. To avoid this, use
    cin.ignore.
  • cin.ignore(20,\n) will skip the next 20 chars
    in the input buffer or until a newline is
    encountered, whichever comes first
  • cin.ignore() will skip the very next character
    in the input buffer

102
3.10 Focus on Object-Oriented
ProgrammingMore About Object- Oriented
Programming
  • A member function is a procedure, written in C
    code, that is part of an object. A member
    function causes the object it is a member of to
    perform an action
  • In this chapter, we have used width, precision,
    setf, and unsetf for the cout object
  • In this chapter we have used width, getline, get,
    and ignore for the cin object

103
3.11 More Mathematical Library Functions
  • The C runtime library provides several
    functions for performing complex mathematical
    operations.
  • In this chapter we have used width, precision,
    setf, and unsetf for the cout object
  • In this chapter we have used width, getline, get,
    and ignore for the cin object

104
Table 3-14
105
Table 3-14 continued
106
Table 3-14 continued
107
Program 3-33
  • // This program asks for the lengths of the 2
    sides of a right
  • // triangle. The length of the hypotenuse is then
    calculated
  • // and displayed.
  • include ltiostream.hgt
  • include ltmath.hgt // For sqrt and pow
  • void main(void)
  • float a, b, c
  •  
  • cout ltlt "Enter the length of side A "
  • cin gtgt a
  • cout ltlt "Enter the length of side B "
  • cin gtgt b
  • c sqrt(pow(a, 2.0) pow(b, 2.0))
  • cout.precision(2)
  • cout ltlt "The length of the hypotenuse is "
  • cout ltlt c ltlt endl

108
Program Output
  • Enter the length of side A 5.0 Enter
  • Enter the length of side B 12.0 Enter
  • The length of the hypotenuse is 13

109
Random numbers
  • rand() (from the cstdlib library)

110
Program 3-34
  • // This program demonstrates random numbers.
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • using namespace std
  • void main(void)
  • unsigned seed
  • cout ltlt "Enter a seed value "
  • cin gtgt seed
  • srand(seed)
  • cout ltlt rand() ltlt endl
  • cout ltlt rand() ltlt endl
  • cout ltlt rand() ltlt endl

111
Program Output
  • Enter a seed value 5
  • 1731
  • 32036
  • 21622
  • Program Output with Other Example Input
  • Enter a seed value 16
  • 5540
  • 29663
  • 9920

112
3.13 Optional Section Introduction to Simple
File Input and Output
  • What is a File? A file is a collection on
    information, usually stored on a computers disk.
    Information can be saved to files and then later
    reused.

113
The Process of Using a File
  • Using a file in a program is a simple three-step
    process
  • The file must be opened. If the file does not
    yet exits, opening it means creating it.
  • Information is then saved to the file, read from
    the file, or both.
  • When the program is finished using the file, the
    file must be closed.

114
Figure 3-8
115
Figure 3-9
116
Setting Up a Program for File Input/Output
  • Before file I/O can be performed, a C program
    must be set up properly.
  • File access requires the inclusion of the
    fstream.h header file.

117
Table 3-16
118
Opening a File
  • Before data can be written to or read from a
    file, the file must be opened.
  • ifstream inputFile
  • inputFile.open(customer.dat)

119
Closing a File
  • A file should be closed when a program is
    finished using it.outputFile.close()

120
Writing Information to a File
  • The stream insertion operator (ltlt) may be used to
    write information to a file.
  • outputFile ltlt I love C programming !
  • outputFile ltlt Price ltlt price

121
Program 3-35
// This program uses the ltlt operator to write
information to a// file.include
ltiostream.hgtinclude ltfstream.hgtvoid
main(void) ofstream outputFile outputFile.op
en("demofile.txt") cout ltlt "Now writing
information to the file.\n" // Write 4 great
names to the file outputFile ltlt
"Bach\n" outputFile ltlt "Beethoven\n" outputFil
e ltlt "Mozart\n" outputFile ltlt "Schubert\n" 
122
Program 3-35 (continued)
// Close the file outputFile.close() cout ltlt
"Done.\n" Program Screen Output Now writing
information to the file.Done. Output to File
demofile.txt BachBeethovenMozartSchubert
123
Reading Information from a File
  • The stream extraction operator (gtgt) may be used
    to read information from a file. inFile gtgt
    name

124
Program 3-36
// This program uses the gtgt operator to read
information from a// file.include
ltiostream.hgtinclude ltfstream.hgtvoid
main(void) ifstream inFile char
name81 inFile.open("demofile.txt") cout ltlt
"Reading information from the file.\n\n" //
Now read name 1 from the file inFile gtgt
name cout ltlt name ltlt endl // Now read name 2
from the file inFile gtgt name cout ltlt name ltlt
endl
125
Program 3-36 (continued)
// Now read name 3 from the file inFile gtgt
name cout ltlt name ltlt endl // Now read name 4
from the file inFile gtgt name cout ltlt name ltlt
endl // Close the file inFile.close() cout
ltlt "\nDone.\n" Program Screen Output Reading
information to the file.BachBeethovenMozartSc
hubertDone.
126
Program 3-37
// This program uses the gtgt operator to read
information from a// file.include
ltiostream.hgtinclude ltfstream.hgtvoid
main(void) ifstream inFile int length,
width, area inFile.open("dimensions.txt") cou
t ltlt "Reading dimensions of 5 rectangles from the
file.\n\n" // Process rectangle 1 inFile gtgt
length inFile gtgt width area length
width cout ltlt "Area of rectangle 1 " ltlt area
ltlt endl // Process rectangle 2 inFile gtgt
length inFile gtgt width area length
width cout ltlt "Area of rectangle 2 " ltlt area
ltlt endl
127
Program 3-37 (continued)
// Process rectangle 3 inFile gtgt
length inFile gtgt width area length
width cout ltlt "Area of rectangle 3 " ltlt area
ltlt endl // Process rectangle 4 inFile gtgt
length inFile gtgt width area length
width cout ltlt "Area of rectangle 4 " ltlt area
ltlt endl // Process rectangle 5 inFile gtgt
length inFile gtgt width area length
width cout ltlt "Area of rectangle 5 " ltlt area
ltlt endl // Close the file inFile.close() cou
t ltlt "\nDone.\n"
128
Program 3-37
Before Program 3-37 is executed, the file
dimensions.txt must be created with a text editor
(such as Windows Notepad). Here is an example of
the file's contents   10 25 718 96 208 3
129
Program 3-37
The program's output is shown below.   Program
OutputReading dimensions of 5 rectangles from
the file.Area of rectangle 1 20Area of
rectangle 2 35Area of rectangle 3 162Area of
rectangle 4 120Area of rectangle 5 24Done.
Write a Comment
User Comments (0)