scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

Similar documents
LESSON 4. The DATA TYPE char

Computer Programming: Skills & Concepts (CP) Files in C

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Computer Programming: Skills & Concepts (CP) Strings

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

8. Characters and Arrays

CS102: Variables and Expressions

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Course organization. Course introduction ( Week 1)

8. Characters, Strings and Files

Computer Programming: Skills & Concepts (CP) Variables and ints

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Tail recursion. Decision. Assignment. Iteration

This Lecture. Computer Programming: Skills & Concepts (INF-1-CP1) Intro to Practical 1; more on scanf. Summary of Lecture 6. descartes.

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Strings. Daily Puzzle

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Fundamentals of Programming

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Fundamental of Programming (C)

Fundamentals of Programming. Lecture 11: C Characters and Strings

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

printf( Please enter another number: ); scanf( %d, &num2);

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

program structure declarations and definitions expressions and statements more standard I/O

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Computers Programming Course 5. Iulian Năstac

Goals of this Lecture

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

CSC 270 Survey of Programming Languages. C-String Values

SWEN-250 Personal SE. Introduction to C

Data Types. Data Types. Integer Types. Signed Integers

today cs3157-fall2002-sklar-lect05 1

Approximately a Final Exam CPSC 206

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Stream Model of I/O. Basic I/O in C

Standard C Library Functions

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

THE FUNDAMENTAL DATA TYPES

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

(Refer Slide Time: 00:23)

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

COMS Spring Homework 3

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

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

Standard File Pointers

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

C Libraries. Bart Childs Complementary to the text(s)

Input / Output Functions

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Lecture 02 C FUNDAMENTALS

Display Input and Output (I/O)

C Language, Token, Keywords, Constant, variable

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

BİL200 TUTORIAL-EXERCISES Objective:

ECET 264 C Programming Language with Applications

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Fundamentals of Programming Session 8

Low-Level Programming

Should you know scanf and printf?

ECET 264 C Programming Language with Applications

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

25.2 Opening and Closing a File

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

Lecture 3. More About C

Systems Programming/ C and UNIX

Basic Types and Formatted I/O

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

C Examples. Goals of this Lecture. Overview of this Lecture

Intermediate Programming, Spring 2017*

Arrays, Strings, & Pointers

Introduction to Programming

Simple Output and Input. see chapter 7

BSM540 Basics of C Language

Standard I/O in C and C++

Goals of this Lecture

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

I/O Management! Goals of this Lecture!

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

I/O Management! Goals of this Lecture!

Lecture 8: Structs & File I/O

Chapter 8 - Characters and Strings

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

Goals of this Lecture

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Transcription:

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Ajitha Rajan What if the user types a word, when an integer is required? As already noted in tutorials: Apart from the action performed by scanf (reading, or attempting to read, the object of the specified type), scan returns an integer, which is the number of input items assigned. This may be fewer than provided for, or even zero, in the event of a matching failure. This returned value can be used to test for a successful read: Monday 30 October 2017 scanf("%d", &a) == 1 if and only if an integer was successfully read into a. CP Lect 13 slide 1 Monday 30 October 2017 CP Lect 13 slide 3 Monday 30 October 2017 Practical programming Robust input handling Characters in C Last lecture This lecture scanf error-checking our input Suppose we want to read in an integer to x: We can test for success by saving the returned value of scanf: if (read_succ == 1) { else { What about the else branch? Print an error message and terminate? Can give the user a second try. CP Lect 13 slide 2 Monday 30 October 2017 CP Lect 13 slide 4 Monday 30 October 2017

scanf error-checking first attempt printf("please input an integer: "); if (read_succ == 1) { else { /* read_succ must have been 0 */ printf("that wasn't an integer! Try again: "); PROBLEM: Guaranteed to fail on error... WHY? scanf error-checking loops printf("please input an integer: "); if (read_succ!= 1) { /* read_succ must have been 0 */ while (read_succ!= 1) { scanf("%*s"); /* scan bad-input, don't try to save */ printf("that wasn't an integer! Try again: "); /* Now we definitely have an int; do the work */ Try it with the Fibonacci programs! CP Lect 13 slide 5 Monday 30 October 2017 CP Lect 13 slide 7 Monday 30 October 2017 scanf error-checking skipping over scanf("%*s"); means skip over first item in read-buffer from standard input (the s is for string (sequence of non-whitespace characters), the * for don t save ). printf("please input an integer: "); if (read_succ == 1) { else { /* read_succ must have been 0 */ scanf("%*s"); /* scan the bad-input, don't save */ printf("that wasn't an integer! Try again: "); Characters What is it that input handling is actually reading from the terminal? Not integers, doubles, or whatever, but characters. The various symbols ( A, a, 0, ;, @, etc) that you might find on the keyboard, together with control characters such as \n (newline), all have integer codes (ASCII). These integers are rather small, so can be wasteful (but sometimes necessary) to use a variable of type int to represent them. CP Lect 13 slide 6 Monday 30 October 2017 CP Lect 13 slide 8 Monday 30 October 2017

The char type The type char is like a small integer type, just big enough (a byte) to hold the usual (in the 1970s) character set. Advantage of char over int: saves space. Is the type used in many text-processing encodings. Disadvantage of char over int: cannot be used in certain situations (as we ll see). Oddly enough, a, b, c, etc., denote integer constants and not characters. Some char values a 97 b 98 z 112 A 65 B 66 Z 90 0 48 1 49 9 57 & 38 * 42 \n 10 32 \a 7 \r 13 is the space character. \r is the carriage return character. \a is a special character that rings a bell! CP Lect 13 slide 9 Monday 30 October 2017 CP Lect 13 slide 11 Monday 30 October 2017 Bytes and char A byte is a binary number of length 8 (8 bits ). 2 options for each bit a byte can take on 2 8 = 256 possible values (0 up to 255). This is enough to cover the English alphabet + other relevant symbols... If you want to play jþ, listen to, discuss the plays of Ἀριστοφάνης, or just ask somebody what their Erdős number is, you need more. In the modern world, real characters have values up to 1114111 but the C char is still 8 bits. If you need to deal with non-ascii, consult a book or the Web! The C char, like int and float, is a signed type, so actually takes values from 128 to 127. Usually it s better to use unsigned char, which really does take values 0 to 255. I/O with characters getchar(): returns the next character from the input stream (could be characters typed at a keyboard, or read from a file). If the end of the stream has been reached (user types CTRL/D or the end of the file is reached) the special value EOF (which is 1 on most systems, but always refer to it as EOF) is returned. putchar(c): writes the character c to the output stream (could be the screen, or another file). These functions are included in <stdio.h>. NOTE: getchar() returns an int, not a char! This is so that it can return all the possible unsigned chars as well as the value EOF. CP Lect 13 slide 10 Monday 30 October 2017 CP Lect 13 slide 12 Monday 30 October 2017

Library functions In addition, #include <ctype.h> gives us various functions on characters: isalpha(c): is c alphabetic? isupper(c): is c upper case? isdigit(c): is c a digit (0 to 9)? toupper(c): if c is a lower case letter, return the corresponding upper case letter; otherwise return c.... and several others: see Kelley and Pohl A.2, or the isalpha man-page. else if (n >= 9) { n = n + 1; putchar('i'); else if (n >= 5) { n = n - 5; putchar('v'); else if (n >= 4) { n = n + 1; putchar('i'); else { n = n - 1; putchar('i'); CP Lect 13 slide 13 Monday 30 October 2017 CP Lect 13 slide 15 Monday 30 October 2017 Printing Roman numerals void PrintNum(int n) { while (n > 0) { if (n >= 100) { n = n - 100; putchar('c'); else if (n >= 90) { n = n + 10; putchar('x'); else if (n >= 50) { n = n - 50; putchar('l'); else if (n >= 40) { n = n + 10; putchar('x'); else if (n >= 10) { n = n - 10; putchar('x'); Idiom for single character I/O We can do a surprising amount by filling in the following template: int c; /* Code for processing the character c. */ The while-loop condition is a bit tricky: it reads a character from the input, assigns it to c and tests whether the character is EOF (i.e., whether we have reached the end of the input)! CP Lect 13 slide 14 Monday 30 October 2017 CP Lect 13 slide 16 Monday 30 October 2017

Continuing the Roman theme: Caesar cypher const int OFFSET = 13, NUMLETS = 26; int c, ord; /* Why is c declared as int and not char? */ c = toupper(c); if (isupper(c)) { ord = c - 'A'; /* Integer in range [0,25] */ ord = (ord + OFFSET) % NUMLETS; /* permute by offset */ c = ord + 'A'; /* back to char */ putchar(c); Idiom for line-oriented I/O We can do a surprising amount by filling in the following template: int c; if (c == '\n') { /* Code for processing the line just read. */ else { /* Code for processing the character c. */ CP Lect 13 slide 17 Monday 30 October 2017 CP Lect 13 slide 19 Monday 30 October 2017 #define NUMLETS 26 int c, i, count[numlets]; Example: Letter frequencies for (i = 0; i < NUMLETS; i++) count[i] = 0; c = toupper(c); if (isupper(c)) { i = c - 'A'; /* Integer in [0,25] */ count[i]++; for (i = 0; i < NUMLETS; i++) { printf("%c: %d\n", i + 'A', count[i]); Example: recording line lengths int c, charcount = 0, linecount = 0; if (c == '\n') { linecount++; printf(" [Line %d has %d characters]\n", linecount, charcount); charcount = 0; else { charcount++; putchar(c); CP Lect 13 slide 18 Monday 30 October 2017 CP Lect 13 slide 20 Monday 30 October 2017

Input and output redirection Suppose we have compiled a program, similar to the ones considered earlier, and placed the resulting object code in the file prog (maybe done by creating a Makefile and using make; or alternatively just by copying a.out into prog). By default, input is from the keyboard, and output is to the screen. So Typing./prog in the shell window runs prog, with input being taken from the keyboard, and output being written to the shell window. However, by extending the command, we may redirect input from the keyboard to a nominated input file, and redirect the output from the screen to a nominated output file. CP Lect 13 slide 21 Monday 30 October 2017./prog < data takes input from the file data, but continues to send output to the shell window../prog > results takes input from the keyboard, but sends output to the file results../prog < data > results takes input from the file data, and sends output to the file results. Reading material :) Kelley and Pohl, subsections 3.2, 3.3 and 3.9 CP Lect 13 slide 22 Monday 30 October 2017