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

Similar documents
More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Software Design & Programming I

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

1 Pointer Concepts. 1.1 Pointer Examples

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

Engineering Problem Solving with C++, Etter

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

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

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

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Chapter 3 - Notes Input/Output

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

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

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C++ For Science and Engineering Lecture 12

The C++ Language. Arizona State University 1

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

C++ Input/Output: Streams

C++ Final Exam 2017/2018

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

Structured Types. 8. Arrays. collection of components whose organization is characterized by method used to access individual components.

CS 103 Lab - Party Like A Char Star

Pointers, Dynamic Data, and Reference Types

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

CS242 COMPUTER PROGRAMMING

Overview of Lecture. 1 Overloading I/O Operators. 2 Overloading << and >> for Fractions. 3 Formatted vs Unformatted Input

Programming Abstractions

Built-in Functions for NTCAs.

Fundamentals of Programming CS-110. Lecture 2

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

CS 115 Exam 3, Spring 2010

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

EE 355 Lab 4 - Party Like A Char Star

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

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

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

TEST 1 CS 1410 Intro. To Computer Science Name KEY. October 13, 2010 Fall 2010

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CS 103 Lab 6 - Party Like A Char Star

Name Section: M/W T/TH Number Definition Matching (8 Points)

Chap 0: Overview. Overview of basic C++ syntax Refresh programming basics C++ Vs. Java differences Coding conventions used. EECS 268 Programming II 1

CS 247: Software Engineering Principles. ADT Design

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Name Section: M/W T/TH Number Definition Matching (6 Points)

Name. Classes and Objects 1. We re going to develop a class to help out Café Below is the definition for a class called

Ch 6. Functions. Example: function calls function

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

BITG 1113: Array (Part 2) LECTURE 9

Arrays. Week 4. Assylbek Jumagaliyev

Module C++ I/O System Basics

Streams. Rupesh Nasre.

CMSC 202 Midterm Exam 1 Fall 2015

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

Introduction to Programming EC-105. Lecture 2

Definition Matching (10 Points)

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

Chapter 9. Strings. 國立雲林科技大學資訊工程研究所張傳育 (Chuan Yu Chang ) 博士

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

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.

120++: a C++ Subset Corresponding to A Project-Based Introduction to C++

Study Guide for Test 2

CSCI 1061U Programming Workshop 2. C++ Basics

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

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

Getting started with C++ (Part 2)

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

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

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

13 4 Understanding Character Strings

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

CSCE 121 ENGR 112 List of Topics for Exam 1

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

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

I/O streams

COL 100. Minor 2 Exam - Practice

Copyright 2003 Pearson Education, Inc. Slide 1

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Chapter 7. Additional Control Structures

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Unit 2: The string class

LECTURE 15. String I/O and cstring library

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Object Oriented Programming COP3330 / CGS5409

Transcription:

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

Character Arrays Reading: pp.554-568 Fall 2017 CS150 - Intro to CS I 2

char Arrays Character arrays can be used as special arrays called C-strings A C-string is just a character array with a null (\0) character added at the end All string literals are stored as C-strings Fall 2017 CS150 - Intro to CS I 3

Example const int size = 11; char name[size] = "Jasmine"; J a s m i' n e \0 Fall 2017 CS150 - Intro to CS I 4

Example const int size = 21; char name[size]; cout << "enter a name:"; cin >> name; cout << name << endl; Fall 2017 CS150 - Intro to CS I 5

Library Functions There are many library functions for manipulating and testing C-strings: strlen strcmp strcpy And many others Fall 2017 CS150 - Intro to CS I 6

Example const int size = 21; char name[size]; int length; cout << "enter a name:"; cin >> name; cout << name << endl; length = strlen(name); cout << "Length is: " << length << endl; Fall 2017 CS150 - Intro to CS I 7

Using const as Array Parameters Page 412 Arrays are by default passed-by-reference Sometimes though, you don t want a function to modify the contents of the array Solution: Pass the array as a constant Fall 2017 CS150 - Intro to CS I 8

Example void showvalues(const int nums[], int size) { for (int i = 0; i < size; i++) { cout << nums[i] << endl; } cout << endl; } Fall 2017 CS150 - Intro to CS I 9

Passing File Objects to Functions Pages 665-666 File stream objects must always be passed to functions by reference bool openfile(ifstream &infile, string name) Fall 2017 CS150 - Intro to CS I 10

Practice Write a program that will ask the user for a file name and open that file. The program then needs to a word from the file and then return the number of vowels in that word. The program must use the functions on the following slide. Fall 2017 CS150 - Intro to CS I 11

Example bool openfile(ifstream &infile, string name); void getword(ifstream &infile, char word[]); int countvowels(const char word[]); Fall 2017 CS150 - Intro to CS I 12

Working With Characters Special functions exist for working with characters Remember, cin >> ch; reads a single character into ch skipping any whitespace What is whitespace again? Fall 2017 CS150 - Intro to CS I 13

cin.get () Sometime we want to read the whitespace! The cin object has a function called get that gets a single character (including whitespace) from the keyboard char ch; cout << "Enter a character: "; cin.get (ch); cout << "Character is: " << ch << endl; Fall 2017 CS150 - Intro to CS I 14

cin.get () The following three program segments are equivalent for inputting a single character char ch; // segment #1 cout << "Enter a character: "; cin.get (ch); cout << "Character is: " << ch << endl; char ch; // segment #2 cout << "Enter a character: "; ch = cin.get (); cout << "Character is: " << ch << endl; char ch; // segment #3 cout << "Enter a character: "; // NOT REALLY EQUIVALENT cin >> ch; // IGNORES WHITESPACE cout << "Character is: " << ch << endl; Fall 2017 CS150 - Intro to CS I 15

Practice What is output for each of the segments below assuming A B C (not ABC)is entered from the keyboard. char ch1, ch2; // segment #1 cout << "Enter: "; cin.get (ch1); cin.get (ch2); cout << "Characters: " << ch1 << ch2 << endl; char ch1, ch2; // segment #2 cout << "Enter: "; cin >> ch1 >> ch2; cout << "Characters: " << ch1 << ch2 << endl; Fall 2017 CS150 - Intro to CS I 16

Processing a Line of Text We can use cin.get() to process an entire line of text including whitespace. What happens if 12345 is entered followed by a return? char ch; int count = 0; cout << "Enter a line of text: "; cin.get (ch); while ('\n'!= ch) { ++count; cin.get (ch); } cout << "Number of characters is: " << count << endl; Fall 2017 CS150 - Intro to CS I 17

cin.peek() char peek(); Look at the next available character but don t remove that character from the stream char nextchar; int intvalue = -1; string textstring = "text"; nextchar = cin.peek(); if (isdigit(nextchar) ) { // you read a digit! cin >> intvalue; } else { cin >> textstring; } http://www.cplusplus.com/reference/istream/istream/peek/ cout << intvalue << endl; cout << textstring << endl; Fall 2017 CS150 - Intro to CS I 18

Reading characters from files Remember, an ifstream object works similarly to cin; // attempt to read character readchar = inputfile.get (); // make sure we read a character while (!inputfile.eof ()) { // process character cout << ">" << readchar << "<"; Note:.eof() is only true after we tried and failed to read from the file! } // attempt to read character again. readchar = inputfile.get (); Fall 2017 CS150 - Intro to CS I 19

Practice Remember, an ifstream object works similarly to cin; Write a C++ program segment to print the number of lines in the file paragraph.txt Fall 2017 CS150 - Intro to CS I 20