Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.

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

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Programming Functions: Passing Parameters by Reference.
1 Objectives Understand streamed input and output.
Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
C++ Basics Part I Part II. Slide 2 Part I: basics.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Input & Output: Console
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
Chapter 05 (Part III) Control Statements: Part II.
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:
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Objective: Students will be able to: Declare and use variables Input integers.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CSC 270 – Survey of Programming Languages
Chapter 15 Strings as Character Arrays
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
LESSON 2 Basic of C++.
A Sample Program #include using namespace std; int main(void) { cout
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Constants, Data Types and Variables
LESSON 2 Basic of C++.
Exercise 1 – Datentypen & Variablen
CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
مبانی برنامه‌سازی با C++ جلسه دوم
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
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.
הרצאה 03 אבני היסוד של תוכנית ב- C
C++ Basics.
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Using string type variables
Std Library of C++.
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
getline() function with companion ignore()
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Character I/O

COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment resp = 'c'; * Input/Output char a, b, c, d; cin >> a >> b >> c >> d; // user types: 1X3Y // a <- '1', b<-'X', c<-'3', d<-'Y' cout <<a<< " " <<b<< " " <<c<< " " <<d<< endl; // output: 1 X 3 Y

COMP104 Character I/O Slide 3 The ASCII Character Set * 7-bit encoding for 128 possible characters * Some of the 32 non-printing control characters n 0 NUL character (end of string character) n 7 Bell character (makes beep) n 8 Backspace character (\b) n 9Tab character (\t) n 10 Newline character (\n) * space (32) is the first printable ACSII character * '0' - '9' have code values 48 through 57 * 'A' - 'Z' have code values 65 through 90 * 'a' - 'z' have code values 97 through 122 * See Appendix A.1 in book for full ASCII list

COMP104 Character I/O Slide 4 Character Functions in  isupper(c) returns nonzero if c is an uppercase letter  islower(c) returns nonzero if c is a lowercase letter  isalpha(c) returns nonzero if either islower(c) or isupper(c) returns nonzero  isdigit(c) returns nonzero if c is a digit character  isalnum(c) returns nonzero if either isalpha(c) or isdigit(c) returns nonzero  isspace(c) returns nonzero if c is a space, newline, or tab  tolower(c) if c is uppercase it returns lowercase; otherwise it returns c  toupper(c) if c is lowercase it returns uppercase; otherwise it returns c

COMP104 Character I/O Slide 5 Relational char Operators * '0' - '9' have code values 48 through 57 '0' < '1' <... < '9' * 'A' - 'Z' have code values 65 through 90 'A' < 'B' <... < 'Z' * 'a' - 'z' have code values 97 through 122 'a' < 'b' <... < 'z' * (Upper case is always < lower case)

COMP104 Character I/O Slide 6 Characters: Example 1 // islower.cpp #include using namespace std; int islower(char c){ if(c>='a' && c <='z') return 1; else return 0; } void main(){ char c; cout << "Enter a character: "; cin >> c; if(islower(c)) cout << c << " is a lower case letter" << endl; else cout << c << " is not a lower case letter\n"; }

COMP104 Character I/O Slide 7 Characters: Example 2 // program to beep at user #include using namespace std; void main(){ cout << '\7'; }

COMP104 Character I/O Slide 8 Characters: Example 3 // program to output a random character #include using namespace std; void main(){ char c; int r; srand(time(0)); r = rand()%26; c = 'a' + r; cout << "random character: " << c << endl; }

COMP104 Character I/O Slide 9 White Space  cin >> c skips any white space (spaces, tabs, newlines) * Example: char c = ' '; while(c != '\n'){ cin >> c; cout << c; } * Input: a b c d * Output: abcd

COMP104 Character I/O Slide 10 White Space * What if you want to keep the white space?  Use cin.get(c): char c = ' '; while(c != '\n'){ cin.get(c); cout.put(c); // same as: cout << c; } * Input:a b c d * Output: a b c d * cin.get(c) reads white space just like other characters

COMP104 Character I/O Slide 11 cin.get(c) cin.get (char&) read a single character cout.put (char) write a single character  Let's say the user inputs a word, 'hello'. cin.get(c) will return the letter 'h'. The rest of the word is not lost, but stays in the stream. If we perform another cin.get operation, we will get the next letter, 'e'.  cout.put(c) simply outputs one letter at a time (same as cout << c ).

COMP104 Character I/O Slide 12 cin.get(c): Example 1 // Program to count the number of input blanks // (Program outputs characters without blanks) #include using namespace std; void main() { char next; int count = 0; do{ cin.get(next); if(next == ' ') count++; else cout.put(next); }while(next != '\n'); cout << "Number of blanks = " << count << endl; }

COMP104 Character I/O Slide 13 cin.get(c): Example 1 Input: a Output: a Number of blanks = 0 Input: ab cd Output: abcd Number of blanks = 1 Input: Output: Number of blanks = 8

COMP104 Character I/O Slide 14 cin.get(c): Example 2 /* Reads a string of characters and converts the digits in the string to int type */ #include using namespace std; int read_int(); void main() { int number; cout << "Enter a line of digits followed by enter: "; number = read_int(); cout << "The numerical value of the digits" << " in the line is: \n" << number << endl; }

COMP104 Character I/O Slide 15 cin.get(c): Example 2 int read_int(){ const char nwln = '\n'; char next; int digit; int value = 0; do{ cin.get(next); if(isdigit(next)){ digit = int(next) - int('0'); value = 10*value + digit; } }while(next != nwln); return value; }

COMP104 Character I/O Slide 16 cin.get(c): Example 2 Enter a line of digits followed by enter: 1a234 The numerical value of the digits in the line is: 1234 Enter a line of digits followed by enter: The numerical value of the digits in the line is: Enter a line of digits followed by enter: The numerical value of the digits in the line is: (overflows at about billion with 32-bit integers)