Starting Out with C++, 3 rd Edition 1 Chapter 3. Expressions and Interactivity.

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 Objectives Understand streamed input and output.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
CS150 Introduction to Computer Science 1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. WAB rev 2 Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis,
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Chapter 3 Expressions and Interactivity. 2 Topics 3.1 The cin Object 3.2 Mathematical Expressions 3.3 When You Mix Apples and Oranges: Type Conversion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
1 CS102 Introduction to Computer Programming Week 3 Chapter 3 Expressions and Interactivity.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3: Expressions & Interactivity
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for CMPS 1043 Computer Science I at MSU.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Lecture 3 Expressions, Type Conversion, Math and String
Introduction to C++ (Extensions to C)
CS102 Introduction to Computer Programming
Chapter 1. Introduction to Computers and Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3. Expressions and Interactivity
Chapter 3: Expressions and Interactivity.
Chapter 3: Expressions and Interactivity
Expressions and Interactivity
Starting Out with C++: From Control Structures through Objects
Expressions and Interactivity
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
C++ for Engineers and Scientists Second Edition
Lecture 3 Expressions, Type Conversion, and string
Presentation transcript:

Starting Out with C++, 3 rd Edition 1 Chapter 3. Expressions and Interactivity

Starting Out with C++, 3 rd Edition 2 3.1The cin Object The cin object reads information types at the keyboard. cin is the standard input object Notice the >> and << operators appear to point in the direction information is flowing.

Starting Out with C++, 3 rd Edition 3 Program 3-1 #include void main(void) { int length, width, area; cout <<"This program calculates the area of a rectangle.\n"; cout <<"What is the length of the rectangle? "; cin>>length; cout <<"What is the width of the rectangle? "; cin>>width; area = length * width; cout <<"The area of the rectangle is " << area << ".\n"; }

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition 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 void main(void) { int length, width, area; cin >> length; cin >> width; area = length * width; cout << "The area of the rectangle is " << area << endl; }

Starting Out with C++, 3 rd Edition 6 Entering Multiple Values The cin object may be used to gather multiple values at once.

Starting Out with C++, 3 rd Edition 7 Program 3-3 #include void main(void) { int length, width, area; cout <<"This program calculates the area of a rectangle.\n"; cout <<"Enter the length and width of the rectangle separated by a space. \n"; cin >> length>> width; area = length * width; cout <<"The area of the rectangle is " << area << endl; }

Starting Out with C++, 3 rd Edition 8 Program Output This program calculates the area of a rectangle. Enter the length and width of the rectangle separated by a space [Enter] The area of the rectangle is 200

Starting Out with C++, 3 rd Edition 9 Program 3-4 //This program demonstrates how cin can read multiple values //of different data types. #include void main(void) { int whole; float fractional; char letter; cout << "Enter an integer, a float, and a character: "; cin >> whole >> fractional >> letter; cout << "whole: " << whole << endl; cout << "fractional: " << fractional << endl; cout << "letter: " << letter << endl; }

Starting Out with C++, 3 rd Edition 10 Program Output Enter an integer, a float, and a character: b [Enter] whole: 4 fractional: 5.7 letter: b

Starting Out with C++, 3 rd Edition 11 Reading Strings cin can read strings as well as numbers. Strings are stored in character arrays. char Company[12];

Starting Out with C++, 3 rd Edition 12 Program 3-5 #include void main(void) { string name; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; }

Starting Out with C++, 3 rd Edition 13 Program Output What is your name? Charlie [Enter] Good morning Charlie

Starting Out with C++, 3 rd Edition 14 Program 3-6 // This program demonstrates how cin can read a // string into a character array #include void main(void) { char name[21] ; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; }

Starting Out with C++, 3 rd Edition 15 Program Output What is your name? Charlie [Enter] Good morning Charlie

Starting Out with C++, 3 rd Edition 16 Program 3-7 //This program reads two strings into two character arrays. #include void main(void) { char first[16], last[16]; cout << "Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; }

Starting Out with C++, 3 rd Edition 17 Program Output Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny

Starting Out with C++, 3 rd Edition 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 array’s 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.

Starting Out with C++, 3 rd Edition Focus on Software Engineering: Mathematical Expressions C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.

Starting Out with C++, 3 rd Edition 20 Program 3-7 // This program reads two strings into two character arrays #include void main(void) { char first[16], last[16]; cout << Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; }

Starting Out with C++, 3 rd Edition 21 Program 3-7 Output with Example Input Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny

Starting Out with C++, 3 rd Edition 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 void main(void) { float numerator, denominator; cout << "This program shows the decimal value of "; cout << "a fraction.\n"; cout << “Enter the numerator: “; cin >> numerator; cout << “Enter the denominator: “; cin >> denominator; cout << “The decimal value is “; cout << (numerator / denominator); }

Starting Out with C++, 3 rd Edition 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

Starting Out with C++, 3 rd Edition 24 Table 3-1 Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) - * / % + -

Starting Out with C++, 3 rd Edition 25 Table 3-2 Some Expressions

Starting Out with C++, 3 rd Edition 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

Starting Out with C++, 3 rd Edition 27

Starting Out with C++, 3 rd Edition 28 Converting Algebraic Expressions to Programming Statements

Starting Out with C++, 3 rd Edition 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 for pow() function. area = pow(4,2) // will store 4 2 in area

Starting Out with C++, 3 rd Edition 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 #include #include // needed for the pow function void main(void) { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = * pow(radius,2); cout << "The area is " << area; }

Starting Out with C++, 3 rd Edition 31 Program 3-9 Output With Example Input This program calculates the area of a circle. What is the radius of the circle? 10[Enter] The area is

Starting Out with C++, 3 rd Edition When you Mix Apples and Oranges: Type Coercion When an operator’s operands are of different data types, C++ will automatically convert them to the same data type.

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition Overflow and Underflow When a variable is assigned a value that is too large or too small in range for that variable’s 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

Starting Out with C++, 3 rd Edition 35 Program 3-10 // This program demonstrates integer underflow and // overflow #include void main(void) { short testVar = 32767; cout << testVar << endl; testVar = testVar + 1; cout << testVar << endl; testVar = testVar - 1; cout << testVar << endl; }

Starting Out with C++, 3 rd Edition 36 Program Output

Starting Out with C++, 3 rd Edition 37 Program 3-11 // This program can be used to see how your system // handles floating point overflow and underflow. #include void main(void) { float test; test = 2.0e38 * 1000; // Should overflow test cout << test << endl; test = 2.0e-38 / 2.0e38; cout << test << endl; }

Starting Out with C++, 3 rd Edition 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

Starting Out with C++, 3 rd Edition 39 Program 3-12 #include void main(void) { int months, books; float perMonth; cout << "How many books do you plan to read? "; cin >> books; cout << "How many months will it take you to read them? "; cin >> months; perMonth = float(books) / months; cout << "That is " << perMonth << " books per month.\n"; }

Starting Out with C++, 3 rd Edition 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 books per month.

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition 42 Program 3-13 // This program uses a typecast operator to print // a character from a number. #include void main(void) { int number = 65; cout << number << endl; cout << char(number) << endl; }

Starting Out with C++, 3 rd Edition 43 Program Output 65 A

Starting Out with C++, 3 rd Edition The Power of Constants Constants may be given names that symbolically represent them in a program.

Starting Out with C++, 3 rd Edition 45 Program 3-14 // This program calculates the area of a circle. #include void main(void) { const float pi = ; double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = pi * pow(radius,2); cout << "The area is " << area; }

Starting Out with C++, 3 rd Edition 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 is roughly the same as const float PI= ;

Starting Out with C++, 3 rd Edition 47 Program 3-15 #include #include // needed for pow function #define PI void main(void) { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = PI * pow(radius, 2); cout << "The area is " << area; }

Starting Out with C++, 3 rd Edition 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;

Starting Out with C++, 3 rd Edition 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 void main(void) { int begInv, sold, store1, store2, store3; cout << “One week ago, 3 new widget stores opened\n"; cout << “at the same time with the same beginning\n"; cout << “inventory. What was the beginning inventory? "; cin >> begInv; store1 = store2 = store3 = begInv;

Starting Out with C++, 3 rd Edition 50 Program 3-16 Continued cout << "How many widgets has store 1 sold? "; cin >> sold; store1 = store1 – sold;//Subtract sold from store1 cout << "How many widgets has store 2 sold? "; cin >> sold; store2 = store2 – sold;//Subtract sold from store2 cout << "How many widgets has store 3 sold? "; cin >> sold; store3 = store3 – sold; //Subtract sold from store 3 cout << "Store1: " << store1 << endl; cout << "Store2: " << store2 << endl; cout << "Store3: " << store3 << endl; }

Starting Out with C++, 3 rd Edition 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

Starting Out with C++, 3 rd Edition 52 Table 3-8

Starting Out with C++, 3 rd Edition 53 Table 3-9

Starting Out with C++, 3 rd Edition 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 void main(void) { int begInv, sold, store1, store2, store3; cout << “One week ago, 3 new widget stores opened\n"; cout << “at the same time with the same beginning\n"; cout << “inventory. What was the beginning inventory? "; cin >> begInv; store1 = store2 = store3 = begInv;

Starting Out with C++, 3 rd Edition 55 Program 3-17 Continued cout << "How many widgets has store 1 sold? "; cin >> sold; store1 -= sold;//Subtract sold from store1 cout << "How many widgets has store 2 sold? "; cin >> sold; store2 -= sold;//Subtract sold from store2 cout << "How many widgets has store 3 sold? "; cin >> sold; store3 -= sold; //Subtract sold from store 3 cout << "Store1: " << store1 << endl; cout << "Store2: " << store2 << endl; cout << "Store3: " << store3 << endl; }

Starting Out with C++, 3 rd Edition 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

Starting Out with C++, 3 rd Edition Formatting Output The cout object provides ways to format data as it is being displayed. This affects the way data appears on the screen.

Starting Out with C++, 3 rd Edition 58 Program 3-18 #include 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 << num1 << " "; cout << num2 << " "; cout << num3 << endl; // Display the second row of numbers cout << num4 << " "; cout << num5 << " "; cout << num6 << endl; // Display the third row of numbers cout << num7 << " "; cout << num8 << " "; cout << num9 << endl; }

Starting Out with C++, 3 rd Edition 59 Program Output

Starting Out with C++, 3 rd Edition 60 Program 3-19 //This program displays three rows of numbers. #include #include // 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 << setw(4) << num1 << " "; cout << setw(4) << num2 << " "; cout << setw(4) << num3 << endl;

Starting Out with C++, 3 rd Edition 61 Program continues // Display the second row of numbers cout << setw(4) << num4 << " "; cout << setw(4) << num5 << " "; cout << setw(4) << num6 << endl; // Display the third row of numbers cout << setw(4) << num7 << " "; cout << setw(4) << num8 << " "; cout << setw(4) << num9 << endl; }

Starting Out with C++, 3 rd Edition 62 Program Output

Starting Out with C++, 3 rd Edition 63 Program 3-20 //This program demonstrates the setw manipulator being //used with values of various data types. #include void main(void) { int intValue = 3928; float floatValue = 91.5; char cStringValue[] = "John J. Smith"; cout << "(" << setw(5) << intValue << ")" << endl; cout << "(" << setw(8) << floatValue << ")" << endl; cout << "(" << setw(16) << cStringValue << ")" << endl; }

Starting Out with C++, 3 rd Edition 64 Program Output ( 3928) ( 91.5) ( John J. Smith)

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition 66 Program 3-21 //This program demonstrates how setprecision rounds a //floating point value #include void main(void) { float quotient, number1 = , number2 = 26.91; quotient = number1 / number2; cout << quotient << endl; cout << setprecision(5) << quotient << endl; cout << setprecision(4) << quotient << endl; cout << setprecision(3) << quotient << endl; cout << setprecision(2) << quotient << endl; cout << setprecision(1) << quotient << endl; }

Starting Out with C++, 3 rd Edition 67 Program Output

Starting Out with C++, 3 rd Edition 68 Table 3-11

Starting Out with C++, 3 rd Edition 69 Program 3-22 //This program asks for sales figures for 3 days. The // total sales is calculated and displayed in a table #include void main(void) { float day1, day2, day3, total; cout << "Enter the sales for day 1: "; cin >> day1; cout << "Enter the sales for day 2: "; cin >> day2;

Starting Out with C++, 3 rd Edition 70 Program Continues cout << "Enter the sales for day 3: "; cin >> day3; total = day1 + day2 + day3; cout << "\nSales Figures\n"; cout << " \n"; cout << setprecision(5); cout << “Day 1: " << setw(8) << day1 << endl; cout << “Day 2: " << setw(8) << day2 << endl; cout << “Day 3: " << setw(8) << day3 << endl; cout << “Total: " << setw(8) << total << endl ; }

Starting Out with C++, 3 rd Edition 71 Program Output Enter the sales for day 1: [Enter] Enter the sales for day 2: [Enter] Enter the sales for day 3: [Enter] Sales Figures Day 1: Day 2: Day 3: Total:

Starting Out with C++, 3 rd Edition 72 Program 3-23 //This program asks for sales figures for 3 days. The // total sales is calculated and displayed in a table. #include void main(void) { float day1, day2, day3, total; cout << "Enter the sales for day 1: "; cin >> day1; cout << "Enter the sales for day 2: "; cin >> day2; cout << "Enter the sales for day 3: "; cin >> day3; total = day1 + day2 + day3;

Starting Out with C++, 3 rd Edition 73 Program Continues cout << "\nSales Figures\n"; cout << "------\n"; cout << setprecision(2) << setiosflags(ios::fixed); cout << “Day 1: " << setw(8) << day1 << endl; cout << “Day 2: " << setw(8) << day2 << endl; cout << “Day 3: " << setw(8) << day3 << endl; cout << "Total: " << setw(8) << total << endl; }

Starting Out with C++, 3 rd Edition 74 Program Output Enter the sales for day 1: [Enter] Enter the sales for day 2: [Enter] Enter the sales for day 3: [Enter] Sales Figures Day 1: Day 2: Day 3: Total:

Starting Out with C++, 3 rd Edition 75 IOS Flags setiosflags manipulator can be used to format output in a variety of ways, depending on which flag is specified. setiosflags(ios::fixed) will cause all subsequent numbers to be printed in fixed point notation

Starting Out with C++, 3 rd Edition 76 Program 3-24 //This program asks for sales figures for 3 days. The // total sales is calculated and displayed in a table. #include void main(void) { float day1, day2, day3, total; cout << "Enter the sales for day 1: "; cin >> day1; cout << "Enter the sales for day 2: "; cin >> day2;

Starting Out with C++, 3 rd Edition 77 Program continues cout << "Enter the sales for day 3: "; cin >> day3; total = day1 + day2 + day3; cout << "\nSales Figures\n"; cout << " \n"; cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint); cout << “Day 1: " << setw(8) << day1 << endl; cout << “Day 2: " << setw(8) << day2 << endl; cout << “Day 3: " << setw(8) << day3 << endl; cout << "Total: " << setw(8) << total << endl; }

Starting Out with C++, 3 rd Edition 78 Program Output Enter the sales for day 1: [Enter] Enter the sales for day 2: [Enter] Enter the sales for day 3: [Enter] Sales Figures Day 1: Day 2: Day 3: Total:

Starting Out with C++, 3 rd Edition 79 Table 3-12

Starting Out with C++, 3 rd Edition 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(ios::fixed) cout.setf(ios::fixed);

Starting Out with C++, 3 rd Edition 81 Program 3-25 // This program asks for sales figures for 3 days. The // total sales is calculated and displayed in a table. #include void main(void) { float day1, day2, day3, total; cout << "Enter the sales for day 1: "; cin >> day1; cout << "Enter the sales for day 2: "; cin >> day2; cout << "Enter the sales for day 3: "; cin >> day3;

Starting Out with C++, 3 rd Edition 82 Program continues total = day1 + day2 + day3; cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << "\nSales Figures\n"; cout << " \n"; cout << “Day 1: "; cout.width(8); cout << day1 << endl; cout << “Day 2: "; cout.width(8); cout << day2 << endl;

Starting Out with C++, 3 rd Edition 83 Program continues cout << “Day 3: "; cout.width(8); cout << day3 << endl; cout << "Total: "; cout.width(8); cout << total << endl; }

Starting Out with C++, 3 rd Edition 84 Program Output Enter the sales for day 1: [Enter] Enter the sales for day 2: [Enter] Enter the sales for day 3: [Enter] Sales Figures Day 1: Day 2: Day 3: Total:

Starting Out with C++, 3 rd Edition 85 Program 3-26 // This program asks for sales figures for 3 days. The // total sales is calculated and displayed in a table. #include void main(void) { float day1, day2, day3, total; cout << "Enter the sales for day 1: "; cin >> day1; cout << "Enter the sales for day 2: "; cin >> day2; cout << "Enter the sales for day 3: "; cin >> day3;

Starting Out with C++, 3 rd Edition 86 Program continues total = day1 + day2 + day3; cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << "\nSales Figures\n"; cout << " \n"; cout << “Day 1: " << setw(8) << day1 << endl; cout << “Day 2: " << setw(8) << day2 << endl; cout << “Day 3: " << setw(8) << day3 << endl; cout << "Total: " << setw(8) << total << endl; return 0; }

Starting Out with C++, 3 rd Edition 87 Program Output Enter the sales for day 1: [Enter] Enter the sales for day 2: [Enter] Enter the sales for day 3: [Enter] Sales Figures Day 1: Day 2: Day 3: Total:

Starting Out with C++, 3 rd Edition 88 Table 3-13

Starting Out with C++, 3 rd Edition Formatted Input The cin object provides ways of controlling string and character input.

Starting Out with C++, 3 rd Edition 90 Program 3-27 //This program uses setw with the cin object. #include void main(void) { char word[5]; cout << "Enter a word: "; cin >> setw(5) >> word; cout << "You entered " << word << endl; }

Starting Out with C++, 3 rd Edition 91 Program 3-28 //This program uses cin's width member function. #include void main(void) { char word[5]; cout << "Enter a word: "; cin.width(5); cin >> word; cout << "You entered " << word << endl; }

Starting Out with C++, 3 rd Edition 92 Program Output for Programs 3-27 and 3-28 Enter a word: Eureka [Enter] You entered Eure

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition 94 Reading a “Line” of Input cin.getline(line, 20);

Starting Out with C++, 3 rd Edition 95 Program 3-29 //This program demonstrates cin's getline member function. #include void main(void) { char sentence[81]; cout << "Enter a sentence: "; cin.getline(sentence, 81); cout << "You entered " << sentence << endl; }

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition 97 Program 3-30 #include void main(void) { char ch; cout << "Type a character and press Enter: "; cin >> ch; cout << "You entered " << ch << endl; } Program Output with Example Input Type a character and press Enter: A [Enter] You entered A

Starting Out with C++, 3 rd Edition 98 Program 3-31 #include void main(void) { char ch; cout << "This program has paused. Press enter to continue."; cin.get(ch); cout << "Thank you!" << endl; } Program Output This program has paused. Press Enter to continue. [Enter] Thank you!

Starting Out with C++, 3 rd Edition 99 Program 3-32 #include void main(void) { char ch; cout << "Type a character and press Enter: "; cin.get(ch); cout << "You entered " << ch << endl; cout << "Its ASCII code is " << int(ch) << endl; }

Starting Out with C++, 3 rd Edition 100 Program Output Type a character and press Enter: [Enter] You entered Its ASCII code is 10

Starting Out with C++, 3 rd Edition 101 Mixing cin >> and cin.get Mixing cin.get with cin >> 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

Starting Out with C++, 3 rd Edition Focus on Object-Oriented Programming:More 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

Starting Out with C++, 3 rd Edition 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

Starting Out with C++, 3 rd Edition 104 Table 3-14

Starting Out with C++, 3 rd Edition 105 Table 3-14 continued

Starting Out with C++, 3 rd Edition 106 Table 3-14 continued

Starting Out with C++, 3 rd Edition 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 #include // For sqrt and pow void main(void) { float a, b, c; cout << "Enter the length of side A: "; cin >> a; cout << "Enter the length of side B: "; cin >> b; c = sqrt(pow(a, 2.0) + pow(b, 2.0)); cout.precision(2); cout << "The length of the hypotenuse is "; cout << c << endl; }

Starting Out with C++, 3 rd Edition 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

Starting Out with C++, 3 rd Edition 109 Random numbers rand() (from the cstdlib library)

Starting Out with C++, 3 rd Edition 110 Program 3-34 // This program demonstrates random numbers. #include using namespace std; void main(void) { unsigned seed; cout << "Enter a seed value: "; cin >> seed; srand(seed); cout << rand() << endl; }

Starting Out with C++, 3 rd Edition 111 Program Output Enter a seed value: Program Output with Other Example Input Enter a seed value:

Starting Out with C++, 3 rd Edition Optional Section : Introduction to Simple File Input and Output What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition 114 Figure 3-8

Starting Out with C++, 3 rd Edition 115 Figure 3-9

Starting Out with C++, 3 rd Edition 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.

Starting Out with C++, 3 rd Edition 117 Table 3-16

Starting Out with C++, 3 rd Edition 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”);

Starting Out with C++, 3 rd Edition 119 Closing a File A file should be closed when a program is finished using it. outputFile.close();

Starting Out with C++, 3 rd Edition 120 Writing Information to a File The stream insertion operator (<<) may be used to write information to a file. outputFile << “I love C++ programming !” outputFile << “Price: “ << price;

Starting Out with C++, 3 rd Edition 121 Program 3-35 // This program uses the #include void main(void) { ofstream outputFile; outputFile.open("demofile.txt"); cout << "Now writing information to the file.\n"; // Write 4 great names to the file outputFile << "Bach\n"; outputFile << "Beethoven\n"; outputFile << "Mozart\n"; outputFile << "Schubert\n";

Starting Out with C++, 3 rd Edition 122 Program 3-35 (continued) // Close the file outputFile.close(); cout << "Done.\n"; } Program Screen Output Now writing information to the file. Done. Output to File demofile.txt Bach Beethoven Mozart Schubert

Starting Out with C++, 3 rd Edition 123 Reading Information from a File The stream extraction operator (>>) may be used to read information from a file. inFile >> name;

Starting Out with C++, 3 rd Edition 124 Program 3-36 // This program uses the >> operator to read information from a // file. #include #include void main(void) { ifstream inFile; char name[81]; inFile.open("demofile.txt"); cout > name; cout > name; cout << name << endl

Starting Out with C++, 3 rd Edition 125 Program 3-36 (continued) // Now read name 3 from the file inFile >> name; cout > name; cout << name << endl; // Close the file inFile.close(); cout << "\nDone.\n"; } Program Screen Output Reading information to the file. Bach Beethoven Mozart Schubert Done.

Starting Out with C++, 3 rd Edition 126 Program 3-37 // This program uses the >> operator to read information from a // file. #include #include void main(void) { ifstream inFile; int length, width, area; inFile.open("dimensions.txt"); cout > length; inFile >> width; area = length * width; cout > length; inFile >> width; area = length * width; cout << "Area of rectangle 2: " << area << endl;

Starting Out with C++, 3 rd Edition 127 Program 3-37 (continued) // Process rectangle 3 inFile >> length; inFile >> width; area = length * width; cout > length; inFile >> width; area = length * width; cout > length; inFile >> width; area = length * width; cout << "Area of rectangle 5: " << area << endl; // Close the file inFile.close(); cout << "\nDone.\n"; }

Starting Out with C++, 3 rd Edition 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:

Starting Out with C++, 3 rd Edition 129 Program 3-37 The program's output is shown below. Program Output Reading dimensions of 5 rectangles from the file. Area of rectangle 1: 20 Area of rectangle 2: 35 Area of rectangle 3: 162 Area of rectangle 4: 120 Area of rectangle 5: 24 Done.