Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.

Slides:



Advertisements
Similar presentations
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Advertisements

Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 6 Control Structures.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Decisions Bush decision making.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
The Ohio State University
Decisions.
Introduction to Computer Programming
Chapter 4: Control Structures I (Selection)
Introduction to Programming
Topic 4: Looping Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Chapter 4: Control Structures I (Selection)
Introduction to Programming
Wednesday 09/23/13.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Decisions, decisions, decisions
Life is Full of Alternatives
Presentation transcript:

Input Validation 10/09/13

Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements Input validation is an important part of working with live human beings. It has been found to be true that, unfortunately, all human beings can mistke makez. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements Let’s look at an elevator program and consider input validation. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements Assume that the elevator panel has buttons labeled 1 through 20 (but not 13!). The following are illegal inputs: –The number 13 –Zero or a negative number –A number larger than 20 –A value that is not a sequence of digits, such as five In each of these cases, we will want to give an error message and exit the program. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements It is simple to guard against an input of 13: if (floor == 13) { cout << "Error: " << " There is no thirteenth floor." << endl; return 1; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements The statement: return 1; immediately exits the main function and therefore terminates the program. It is a convention to return with the value 0 if the program completes normally, and with a non-zero value when an error is encountered. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements To ensure that the user doesn’t enter a number outside the valid range: if (floor 20) { cout << "Error: " << " The floor must be between 1 and 20." << endl; return 1; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements Dealing with input that is not a valid integer is a more difficult problem. What if the user does not type a number in response to the prompt? ‘F’ ‘o’ ‘u’ ‘r’ is not an integer response. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements When cin >> floor; is executed, and the user types in a bad input, the integer variable floor is not set. Instead, the input stream cin is set to a failed state. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements You can call the fail member function to test for that failed state. So you can test for bad user input this way: if (cin.fail()) { cout << "Error: Not an integer." << endl; return 1; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements Later you will learn more robust ways to deal with bad input, but for now just exiting main with an error report is enough. Here’s the whole program with validity testing: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements – Elevator Program #include using namespace std; int main() { int floor; cout << "Floor: "; cin >> floor; // The following statements check various input errors if (cin.fail()) { cout << "Error: Not an integer." << endl; return 1; } if (floor == 13) { cout << "Error: There is no thirteenth floor." << endl; return 1; } if (floor 20) { cout << "Error: The floor must be between 1 and 20." << endl; return 1; } ch03/elevator2.cpp C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Validation with if Statements – Elevator Program // Now we know that the input is valid int actual_floor; if (floor > 13) { actual_floor = floor - 1; } else { actual_floor = floor; } cout << "The elevator will travel to the actual floor " << actual_floor << endl; return 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

DeMorgan’s Law: !(A && B) is the same as !A || !B (change the && to || and negate all the terms) !(A || B) is the same as !A && !B (change the || to && and negate all the terms) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved DeMorgan’s Law

So !(country == "USA“ && state != "AK" && state != "HI") becomes: !(country == "USA“) || !(state != "AK“) || !(state != "HI") and then we make those silly !( …==…)’s and !( …!=…)’s better by making !( == ) be just != and !( != ) be just ==. country != "USA“ || state == "AK“ || state == "HI" C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved DeMorgan’s Law

Chapter Summary Use the if statement to implement a decision. The if statement allows a program to carry out different actions depending on the nature of the data to be processed. Implement comparisons of numbers and objects. Relational operators ( >= == != ) are used to compare numbers and strings. Lexicographic order is used to compare strings. Implement complex decisions that require multiple if statements. Multiple alternatives are required for decisions that have more than two cases. When using multiple if statements, pay attention to the order of the conditions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Chapter Summary Implement decisions whose branches require further decisions. When a decision statement is contained inside the branch of another decision statement, the statements are nested. Nested decisions are required for problems that have two levels of decision making. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Chapter Summary Design test cases for your programs. Each branch of your program should be tested. It is a good idea to design test cases before implementing a program. Use the bool data type to store and combine conditions that can be true or false. The bool type bool has two values, false and true. C++ has two Boolean operators that combine conditions: && (and) and || (or). To invert a condition, use the ! (not) operator. The && and || operators use short-circuit evaluation: As soon as the truth value is determined, no further conditions are evaluated. De Morgan’s law tells you how to negate && and || conditions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Chapter Summary Short Circuit evaluation skips evaluation of the second expression in a && or || operation if it is not neede to determine the result Apply if statements to detect whether user input is valid. When reading a value, check that it is within the required range. Use the fail function to test whether the input stream has failed. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

End Chapter Three Slides by Evan Gallagher C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved