Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Input/Output
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Exposure C++ Chapter VII Program Input and Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Input, Output, and Processing
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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:
CPS120: Introduction to Computer Science Formatted I/O.
Chapter 3: Input/Output
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Bill Tucker Austin Community College COSC 1315
Topic 2 Input/Output.
C++ Basic Input and Output (I/O)
Topics Designing a Program Input, Processing, and Output
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3: Expressions and Interactivity.
Chapter 2 part #3 C++ Input / Output
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Introduction to C++ Programming
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
Chapter 3: Input/Output
Introduction to cout / cin
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
What Actions Do We Have Part 1
Chapter 2 part #3 C++ Input / Output
Introduction to cout / cin
Presentation transcript:

Chapter 4 Strings and Screen I/O

Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform basic string operations. Use cin and cout. Use special characters. Format output. Accept characters and strings as input.

Strings and Literals Recall that a group of characters put together is a string. C++ does not have a data type for strings. Hard coded values or strings are called literals. Literals can be numeric literals, string literals, or character literals.

Classes and Objects An object-oriented string data type is referred to as a string class. A string class is actually a definition used to create a string object. A class is a generic definition from which an object is created. An object is said to be an instance of a class.

Using the String Class We will use the apstring class which is made up of two files: apstring.h and apstring.cpp. Declaring a string object is much like declaring other variables: apstring MyString1; apstring MyString2("ABCDEF");

Assigning Strings to String Objects 1.You can assign the contents of one string object to another string object. MyString1 = MyString2; 2.You can assign a string literal to a string object. MyString1 = "string literal"; 3.You can assign a character literal to a string object. MyString1 = 'A';

Messages One of the important concepts behind the use of objects is the idea of containment (or encapsulation). An object hides the details of how data is stored and how operations work. To make the object do what we want it to do, we send the object a message.

Obtaining the Length of a String The message used to obtain the length of a string is simply length. l = MyString2.length(); MyString2 is the name of the string that we want to send a message to. The dot operator separates the name from the message. The code inside the object that performs the length operation is called a method.

String Concatenation Concatenation means adding one string onto the end of another string. The + operator can be used to concatenate strings. MyString1 = MyString1 + ' ' + MyString2; The += operator can also be used. MyString1 += MyString2;

Using cin and cout The stream that brings data from your keyboard is cin, and the stream that takes data to your screen is cout. The >> operator is also referred to as the extraction operator because it extracts data from the stream. The << operator is also referred to as the insertion operator because it inserts data into the stream.

New Line and Other Special Characters The \n character is called the new line character or the end-of-line character. endl can be used in place of the \n. cout << i << '\n'; cout << i << endl; Other special characters include: \t for a tab\\ for a backslash \' for a single quote\" for a double quote

Using setf and unsetf The cout object has format options that can be changed by using setf and unsetf. An example of the syntax: cout.setf(ios::right); Formatting options include: left, right, showpoint, uppercase, showpos, scientific, and uppercase.

Using the I/O Manipulators The most common I/O manipulators in C++ are setprecision and setw. setprecision is used to set the number of digits that will appear to the right of the decimal point. setw is used to change the number of spaces the compiler uses when it displays a number.

I/O Manipulator Examples Using setprecision cout << setprecision(2) << total; Using setw cout << setw(10) << i << setw(10) << j;

Inputting Characters The >> operator can be used to input characters. If the user enters more than one character, only the first character will be stored in the variable.

Inputting Strings The getline method is used to input strings entered by the user. Example cout << "Enter your first name: "; getline(cin, FirstName);

Flushing the Input Stream After you have input a number using a cin statement, the new line character that is generated when you press Enter stays in the input stream. You must remove the extra characters from the input stream before the getline method is executed. You can use the following statement to "flush" the stream: cin.ignore(80, '\n');

Summary Strings allow computers to process text as well as numbers. Hard-coded numeric values are called numeric literals. Hard-coded text is called a string literal. A class is a definition used to create an object. An object is said to be an instance of a class.

Summary You can use cout to display the contents of a string object. To make an object perform an operation on itself, you send the object a message. The length method is used to determine the length of a string stored in a string object.

Summary Concatenation is the operation of adding one string onto the end of another string. The > symbols are actually operators. The cin and cout keywords are actually objects. The cin and cout objects are streams. A stream is data flowing from one place to another.

Summary The \n character is a special character called the new line character or end-of-line character. There are special characters for printing characters such as tab, the backslash, and quotes. You can use endl in place of '\n'. The cout object has format options that can be changed with the setf and unsetf methods.

Summary setprecision is used to set the number of digits that will appear to the right of the decimal point. setw is used to set the field width. The >> operator can be used to input characters. To input strings, use the getline method of the string class. It is sometimes necessary to flush the input stream to remove characters left in it.