Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.

Slides:



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

Numeric Types, Expressions, and Output ROBERT REAVES.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
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.
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 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.
Chapter 3: Input/Output
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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.
CPS120: Introduction to Computer Science Formatted I/O.
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,
Chapter 3: Input/Output
Expressions and Interactivity. 3.1 The cin Object.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Lecture 3 Expressions, Type Conversion, Math and String
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
Chapter 3: Expressions and Interactivity
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Chapter 3: Expressions and Interactivity
Expressions and Interactivity
Starting Out with C++: From Control Structures through Objects
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 5 Input and Output Streams
Expressions and Interactivity
Chapter 3: Input/Output
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
Standard Version of Starting Out with C++, 4th Edition
Lecture 3 Expressions, Type Conversion, and string
Presentation transcript:

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output

Chapter 3 slide 2 Topics (continued) 3.8 Formatting Output 3.9 Working with Characters and String Objects 3.10 C-String 3.11 More Mathematical Library Functions

Chapter 3 slide Formatting Output Can control how output displays for numeric and string data –size –position –number of digits Requires iomanip header file

Chapter 3 slide 4 Stream Manipulators Used to control features of an output field Some affect just the next value displayed –setw(x) : Print in a field at least x spaces wide. Use more spaces if specified field width is not big enough.

Chapter 3 slide 5 Stream Manipulators Some affect values until changed again –fixed : Use decimal notation (not E-notation) for floating-point values. –setprecision(x) : When used with fixed, print floating-point value using x digits after the decimal. Without fixed, print floating-point value using x significant digits. –showpoint : Always print decimal for floating-point values.

Chapter 3 slide 6 Manipulator Examples const float e = 2.718; float price = 25.0; Displays cout << setw(8) << e << endl; ^^^2.718 cout << setprecision(2); cout << e << endl; 2.7 cout << fixed << e << endl; 2.72 cout << setw(6) << price; ^18.00

Chapter 3 slide Working with Characters and String Objects char : holds a single character string : holds a sequence of characters Both can be used in assignment statements Both can be displayed with cout and <<

Chapter 3 slide 8 Character Input Reading in a character char ch; cin >> ch; // Reads in any non-blank char cin.get(ch); // Reads in any char cin.ignore() // Skips over next char in // the input buffer

Chapter 3 slide 9 String Input Reading in a string object string str; cin >> str; // Reads in a string // with no blanks getline(cin, str); // Reads in a string // that may contain // blanks

Chapter 3 slide 10 Input/Output with String Type cin >> name; Type in “Alice Wonderland” Result: name  “Alice” Instead use getline (cin, name); Result: name  “Alice Wonderland”

Chapter 3 slide 11 String Operators = Assigns a value to a string string words; words = “Tasty ”; + Joins two strings together string s1 = “hot", s2 = "dog"; string food = s1 + s2; // food = “hotdog" += Concatenates a string onto the end of another one words += food; // words now = “Tasty hot dog"

Chapter 3 slide Using C-Strings C-string is stored as an array of characters Programmer must indicate maximum number of characters at definition char temp[5] = "Hot"; NULL character ( \0 ) is placed after final character to mark the end of the string Programmer must make sure array is big enough for desired use; temp can hold up to 4 characters plus the \0. H o t \0

Chapter 3 slide 13 C-String Input Reading in a C-string char Cstr[10]; cin >> Cstr; // Reads in a C-string with no // blanks. Will write past the // end of the array if input string // is too long. cin.getline(Cstr, 10); // Reads in a C-string that may // contain blanks. Ensures <= 9 // chars are read in.

Chapter 3 slide 14 C-String Initialization vs. Assignment A C-string can be initialized at the time of its creation (just like a string object can) char month[10] = “April”; However, a C-string cannot later be assigned a value using the = operator; you must use the strcpy() function char month; month = “April” // wrong! strcpy(month, “April”); //correct

Chapter 3 slide More Mathematical Library Functions These require cmath header file Take double arguments and return a double Commonly used functions abs Absolute value sin Sine cos Cosine tan Tangent sqrt Square root log Natural (e) log

Chapter 3 slide 16 More Mathematical Library Functions These require cstdlib header file rand –Returns a random number between 0 and the largest int the computer holds –With the same seed, will yield same sequence of numbers each time program is run srand(x) –Initializes random number generator with unsigned int x