CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Similar documents
CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

CSc Introduction to Computing

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CMSC 202 Midterm Exam 1 Fall 2015

Engineering Problem Solving with C++, Etter

Fundamentals of Programming CS-110. Lecture 2

Chapter 10 Introduction to Classes

Week 3: File I/O and Formatting 3.7 Formatting Output

Chapter 2 Basic Elements of C++

Lab # 02. Basic Elements of C++ _ Part1

Computer Programming

BITG 1113: Function (Part 2) LECTURE 5

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Introduction to Programming EC-105. Lecture 2

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Definition Matching (10 Points)

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

LECTURE 02 INTRODUCTION TO C++

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

2 nd Week Lecture Notes

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Lecture 23: Pointer Arithmetic

Chapter Two MULTIPLE CHOICE

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

Understanding main() function Input/Output Streams

Computer Programming C++ Classes and Objects 6 th Lecture

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Objectives. In this chapter, you will:

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

C++ Input/Output Chapter 4 Topics

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSCE Practice Midterm. Data Types

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Getting started with C++ (Part 2)

BITG 1113: Array (Part 2) LECTURE 9

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

CHAPTER 3 BASIC INSTRUCTION OF C++

Do not turn to the next page until the start of the exam.

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

C++ basics Getting started with, and Data Types.

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Lecture 3 Tao Wang 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

I/O Streams and Standard I/O Devices (cont d.)

Name Section: M/W or T/TH. True or False (14 Points)

CSc 10200! Introduction to Computing. Lecture 1 Edgardo Molina Fall 2013 City College of New York

CS2141 Software Development using C/C++ C++ Basics

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Module 7 b. -Namespaces -Exceptions handling

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2 C++ Fundamentals

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

ROCK PAPER SCISSORS Rock Paper Scissors Lab Project Using C or C++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSCE 110 PROGRAMMING FUNDAMENTALS

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Exercise 1.1 Hello world

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

Name. CPTR246 Spring '17 (100 total points) Exam 2

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

C++ Strings, Enums. Data Processing Course, I. Hrivnacova, IPN Orsay

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

VARIABLES & ASSIGNMENTS

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Computer Programming : C++

Software Design & Programming I

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Lecture 2 Tao Wang 1

Computer Programming

Transcription:

CSc 10200 Introduc/on to Compu/ng Lecture 17 Edgardo Molina Fall 2011 City College of New York 22

NOTE ABOUT BOOK Some example show function prototypes inside of the main() function THIS IS WRONG; DON T DO IT! C++ for Engineers and Scientists, Third Edition 23

Character Manipulation Functions In addition to string functions provided by string class, the C++ language provides several useful character class functions Function declaration (prototype) for each function is contained in the header file string or cctype, which must be included in any program using these functions C++ for Engineers and Scientists, Third Edition 24

Character Manipulation Functions (continued) C++ for Engineers and Scientists, Third Edition 25

Character Manipulation Functions (continued) C++ for Engineers and Scientists, Third Edition 26

Character Manipulation Functions (continued) int isascii(charexp) int isspace(charexp) int isprint(charexp) int isctrl(charexp) int ispunct(charexp) int isgraph(charexp) More documentation: http://en.cppreference.com/w/cpp/string/narrow C++ for Engineers and Scientists, Third Edition 27

Program 9.13 #include <iostream> using namespace std; int main() { string str = "This -123/ is 567 A?<6245> Test!"; char nextchar; int i; int numletters = 0, numdigits = 0, numothers = 0; cout << "The original string is : " << str << "\nthis string contains " << int(str.length()) << " characters, which consist of" << endl; //check each character in the string for (i=0; i<str.length(); i++) { nextchar = str.at(i); if(isalpha(nextchar)) numletters++; else if (isdigit(nextchar)) numdigits++; else numothers++; } cout << "\t" << numletters << " letters" << endl; cout << "\t" << numdigits << " digits" << endl; cout << "\t" << numothers << " other characters." << endl; C++ for Engineers return and Scientists, 0; Third Edition 28 }

Character I/O Table 9.6 Basic Character I/O Functions (Require the header file cctype) C++ for Engineers and Scientists, Third Edition 29

Character I/O (continued) Table 9.6 Basic Character I/O Functions (Require the header file cctype) (continued) C++ for Engineers and Scientists, Third Edition 30

Character I/O (continued) 31

Phantom Newline Character Revisited C++ for Engineers and Scientists, Third Edition 32

Phantom Newline Character Revisited (continued) C++ for Engineers and Scientists, Third Edition 33

Phantom Newline Character Revisited (continued) C++ for Engineers and Scientists, Third Edition 34

A Second Look at User-Input Validation Sign of well-constructed, robust program: Code that validates user input and ensures that program doesn t produce unintended results caused by unexpected input User-input validation: Basic technique for handling invalid data input and preventing seemingly innocuous code from producing unintended results Validates entered data during or after data entry and gives the user a way of reentering data if it is invalid C++ for Engineers and Scientists, Third Edition 35

Input Data Validation Validating user input is essential Successful programs anticipate invalid data and prevent it from being accepted and processed A common method for validating numerical input data is accepting all numbers as strings After string is validated it can be converted to the correct type C++ for Engineers and Scientists, Third Edition 36

Input Data Validation (continued) Table 9.7 C-String Conversion Functions C++ for Engineers and Scientists, Third Edition 37

Program 9.18 C++ for Engineers and Scientists, Third Edition 38

Common Programming Errors Forgetting to include string header file when using string class object Forgetting that newline character, \n, is a valid input character Forgetting to convert string class object by using c_str() function when converting string class objects to numerical data types C++ for Engineers and Scientists, Third Edition 39

Chapter Summary String literal is any sequence of characters enclosed in quotation marks Referred to as a string value, a string constant, and, more conventionally, a string String can be can be constructed as an object of the string class string class is common used for constructing strings for input and output purposes Strings can be manipulated by using functions of the class they re objects of or by using the general purpose string and character functions C++ for Engineers and Scientists, Third Edition 40

Chapter Summary (continued) cin object tends to be of limited usefulness for string input because it terminates input when a blank is encountered For string class data input, use the getline() function cout object can be used to display string class strings C++ for Engineers and Scientists, Third Edition 41