Input Validation CSCE 121 J. Michael Moore

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

1 Objectives Understand streamed input and output.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Arrays.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Chapter 3: Input/Output
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Dynamic memory allocation and Pointers Lecture 4.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Announcements Midterm2 on April 18 th Monday at 19:40 This week recitations, we will solve sample midterm questions. I will also send previous years’ exams.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
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.
Topic 2 Input/Output.
I/O Streams File I/O 2-D array review
while Repetition Structure
CS 1430: Programming in C++ No time to cover HiC.
Data Representation Integers
getline() function with companion ignore()
Chapter 2 part #3 C++ Input / Output
Some Basics for Problem Analysis and Solutions
While Loops.
Some Basics for Problem Analysis
Sorting CSCE 121 J. Michael Moore
Stream States CSCE 121 J. Michael Moore
Lecture 5A File processing Richard Gesick.
Exceptions with Functions
Alternate Version of STARTING OUT WITH C++ 4th Edition
IO Overview CSCE 121 J. Michael Moore
Manipulators CSCE 121 J. Michael Moore
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.
Array & Pointers CSCE 121 J. Michael Moore.
Dynamic Memory Copy Challenge
Counting Loops.
Exceptions CSCE 121 J. Michael Moore
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Chapter 3: Input/Output
How Functions Work Part 2
CS150 Introduction to Computer Science 1
Looping III (do … while statement)
Chapter 2 part #3 C++ Input / Output
(Dreaded) Quiz 2 Next Monday.
Input Validation CSCE 121 Based on slides created by Carlos Soto.
Stream States CSCE 121 Based on slides created by Carlos Soto.
getline() function with companion ignore()
Programming Fundamental-1
Presentation transcript:

Input Validation CSCE 121 J. Michael Moore Based on slides created by Carlos Soto.

Using Stream State to Validate Input cout << "Enter an integer: "; int val; cin >> val; cout << "You entered: " << val << endl; Failbit is set. So, we can catch this and ask until we get a number.

Using Stream State to Validate Input cout << "Enter an integer: "; int val; cin >> val; while(!cin.good()) { // any of the stream state bits is set cin.clear(); // set all stream state bits to zero, buffer NOT cleared cin.ignore(numeric_limits<streamsize>::max(), '\n'); // clear the buffer of everything, i.e. make clean slate for input cout << "Enter a valid integer: "; } cout << "You entered: " << val << endl;

Checking for errors in our input cin.clear(); Clear stream states Does NOT affect buffer cin.ignore(numeric_limits<streamsize>::max(), '\n'); First parameter – max number of characters to ignore (i.e. remove from buffer) Second parameter – character to stop ignoring characters numeric_limts – different streams have different sizes, using this ensures that if the buffer is completely full, it will remove all of it. '\n' – character representing end of line. Since end of line is a non-printing character we use the ‘\’ to indicate a character that is not the \. \n designates then end of line in C++.