User Input Validation - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

User Input Validation

Description:

We want user-friendly software, so that we can catch user input mistakes and ... If the user types in abc, the cin i statement will ignore it and i will be ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 21
Provided by: mikeb58
Category:

less

Transcript and Presenter's Notes

Title: User Input Validation


1
User Input Validation
  • CTEC1335/2008F

2
Why We Need To Validate Input
  • Incorrect or invalid input can cause programs to
    produce incorrect or invalid outputs, behave
    erratically, or fail.
  • We want user-friendly software, so that we can
    catch user input mistakes and give the user
    another chance
  • DONT TRUST USER INPUT

3
Limitations of gtgt
  • The insertion operators only work for a specific
    type of input (char, int, double) and tend to
    ignore invalid input
  • Sometimes this behaviour makes it difficult to
    know whether the user has entered invalid data or
    just an invalid value.

4
Example
  • int i 0
  • cout ltlt Enter a number
  • cin gtgt i
  • If the user types in abc, the cin gtgt i statement
    will ignore it and i will be unchanged. But what
    if the user types in zero?

5
Strings To The Rescue
  • A string is a sequence of char values, usually
    terminated with a zero byte.
  • In C, a string is either an array of chars or a
    pointer to an array of chars.
  • char name 20
  • char line new char 80
  • C strings are manipulated by functions contained
    in the cstring header file include ltcstringgt

6
C Strings
  • In C, the ANSI/ISO Standard contains a string
    class, which contains several methods (functions)
    to manipulate string objectsinclude ltstringgt
  • string name
  • string p_title new string(
  • C for Engineers and Scientists )

7
String Methods
  • See Table 7.4, pp. 399-401.int length( ) //
    get string length
  • // extract a substringstring substr( int
    startindex, int numchars ) // extract a single
    character
  • char operator ( int index )
  • // join two strings into a new onestring
    operator( const string s1, const string s2
    ) // append a string (on to the end)string
    operator( const string s )

8
String Input
  • Use the getline( ) functionistream getline(
    istream stream, string line, char
    terminator \n )

9
Using getline( )
  • This function, by default, reads all characters
    that the user types in, until Enter is pressed,
    and copies them, excluding the newline character
    generated by the Enter key, into the string
    object.
  • You need to declare the string object (variable)
    for line beforehand, and use cin as the stream.

10
Using getline( )
  • Examplestring name cout ltlt Enter your name
    getline( cin, name )
  • Note that the terminator argument is not
    required, because it has a default value (a
    newline character \n)

11
Why not just use gtgt?
  • The extraction operator gtgt is configured to read
    string data.
  • For examplestring name cout ltlt Enter your
    name cin gtgt name

12
Limitations of gtgt (again)
  • However, the gtgt looks for the first whitespace
    character in the input, either a space, tab or
    newline character, and stops reading there.
  • So if the user types in his or her first and last
    names (separated by a space), only the first name
    is read into the string!
  • You could get around this with an extra read and
    two string variables, but what if the user has
    three names?

13
Entering Numbers
  • In order to do mathematical calculations in C,
    data has to be in the form of int or double
    variables.
  • However, there is no getline( ) function for int
    or double variables, so we need a way to convert
    string data to numeric data.
  • A few ways to go about this

14
C Standard Library Conversion Routines (1)
  • The C Standard Library, which is part of the
    ANSI/ISO C Standard, has two functions to
    convert string data to numeric datainclude
    ltcstdlibgtint atoi( const char ) double atof(
    const char )

15
C Standard Library Conversion Routines (2)
  • The C Standard I/O Library has another routine
    that can convert string data to numeric
    datainclude ltcstdiogtint sscanf( const
    char s, const char format, ... )

16
Limitations of Built-In Functions
  • The three C Standard Library functions may have
    difficulty differentiating invalid input from
    invalid values.
  • For example, both atoi( ) and atof( ) return 0
    (0.0) when invalid string data is encounted,
    i.e., non digit characters.
  • There is no easy way to tell when an actual zero
    value has been converted!

17
Parsing
  • Parsing is a fancy computer science buzzword
    that means making sense of string data.
  • Using string methods, we can go through a string
    character by character and determine exactly what
    was entered.
  • The cost of using parsing is that we typically
    have to write all or part of the parsing code

18
Writing A Parser
  • To correctly write a parser, you need to
    determine what characters constitute valid input
    (i.e., the input format or grammar.)
  • Second, you extract parts of the string called
    tokens and determine if the each token is
    acceptable where it is found. This process is
    called scanning or tokenizing.
  • Then, you convert the extracted tokens into a
    return value that the rest of the program can
    process. This may involve eliminating tokens
    like whitespace or doing some calculations to
    generate a numeric value.

19
Parsers (continued)
  • The parser will return both the value and a
    Boolean (bool) value indicating whether the input
    was valid or not.
  • This way, the program can tell whether a zero
    return value actually means a zero was input.
  • Often, the use of a Finite State Machine (FSM) is
    useful in writing a parser

20
Upcoming topics in Unit 2
  • Using constants
  • Exception handling
  • Finite State Machines
  • Writing a parser FSM
  • File I/O (both text and binary)
Write a Comment
User Comments (0)
About PowerShow.com