Consider all the programs you have
written so far. They nearly all require user input. In all the examples
given so far, it is assumed the user will input the appropriate
data types when asked to do so. The sum()
routine that
you created earlier expected the user to enter two different numbers.
I expect your program works just fine if the user enters a number when
you ask for one, but it behaves strangely or crashes if the user enters
letters.
When writing a program, it is best
to assume that a user will never use your program as expected. In other
words, assume that whenever user input is required you may get inappropriate
input. It is your job to validate input and make your
programs robust and bullet-proof; able to handle whatever a user throws
at it.
A bulletproof program is one that
can handle anything that comes up at runtime, including bizarre user
input.
Now, let's think about the types of
user input you could you have.
When a user
presses a key such as 'a' on the keyboard, they are inputting
a character datatype. When a user presses something like a '1'
on the keyboard, that '1' they are inputting is still a
character datatype and not a numerical 1.
So, your program may...
-
expect a character or string input
-
expect a numerical input (really keyboard characters
'0' ... '9')
-
expect a combined character and numerical input
In all cases, we are really
getting character datatypes from the keyboard. So, before looking at
each of these types of input, we are going to look at the ASCII
character set, which is a way of representing keyboard characters.
ASCII
Code
Computers store characters as digital
data. If a keyboard character is pressed, then a code representing the
character is sent to the CPU. The set of character codes representing
keyboard and other characters is called the 8-bit ASCII
code.
There are 256 different codes representing
256 different characters in the ASCII set.
For example:-
-
the letter A is represented by the code 01000001, which in base
10 is 65
-
the letter B is represented by the code 01000010, which in base
10 is 66
More character codes are shown in
the picture below.

You can look at the 7-bit set of character
codes if you like.
Some useful codes to memorize are:-
- digit '0' = 48
- digit '9' = 57
- 'A' = 65
- 'Z' = 90
- 'a' = 97
- 'z' = 122
Note: digit '0
'
is 48
, digit '1
' is 49
, etc.
so you can figure the code for any other digit.
Also, 'A
' is 65
, 'B
'
is 66
, etc. so you can figure out the code for any uppercase
letter, too.
Finally, 'a
' is 97
,
'b
' is 98
, etc. so you can figure out the
code for any lowercase letter, too.
The code below prints out the codes for the characters'A'
to 'Z'
|
int codes()
{
// output the ASCII values
of A..Z
for (int i = 'A'; i <= 'Z'; i++)
cout << "The ASCII
value of " << (char) i << " is " <<
i << endl;
}
|
or you could print out the whole of the ASCII set.
|
int printASCII()
{
// output the ASCII set
for (int i = 0; i <= 255; i++)
cout << "The ASCII
code " << i << " is the character " <<
(char) i << endl;
}
|
Now let's look more closely at the type of user input
you program might require:-
-
character or string input
-
numerical input (really keyboard characters '0'
... '9')
-
combined character and numerical input
Character
or String Input
Suppose a program requires the user
to enter a character or a string. In many cases, it may not matter exactly
what the user enters - their name for example. Your program will not
care in this case what combination of characters a user entered - it
is their choice what name to enter.
However, suppose your program requires
a particular character to be entered. Consider the
code below. This routine asks the user if they want to repeat a test.
It expects an input in the form of Y
or y
or N
or n
. The return value depends on the
users choice. A Y
or y
returns a 0. A N
or n
returns a 1 and anything else returns a 2.
|
int validateYesNo()
{
char YesNo[80];
cout << "Would you like to repeat
the test? Y/N" << endl;
cin >> YesNo;
//check for a Y
if (toupper(YesNo[0]) == 'Y') return 0;
//check for a N
else if (toupper(YesNo[0]) == 'N') return 1;
else return 2;
}
|
Note: Don't forget
to call the validateYesNo() routine from main()
and include the prototype
near the top of your code or
in your own header file. Also, include stdlib.h
.
Try running the code. You can call
this routine from main like so -
cout << validateYesNo();
Then you will see the return value
for the function.
Now try changing the code:-
if (toupper(YesNo[0]) == 'Y')
to
if (toupper(YesNo[0]) == 89)
Run your program. What do you expect
the return value to be?
You should get a return value of 0
if you enter a Y
or a y
. This is because the
ASCII code for Y
is 89
.
Checking a character input through comparing with
ASCII codes can be quite useful.
Numerical
Input
It is simple to test if a character
entered by the user is a digit; you can use the isdigit()
function. If the character supplied to isdigit()
is a digit,
the return value is nonzero. If the character is not a digit then zero
will be returned. The header file for isdigit()
is ctype.h
.
isdigit(ch)… // ch is a char variable
For example:-
isdigit('1')
would return a nonzero. Whereas,
isdigit('a')
would return a zero.
Suppose you had to write a program
that gets the users age. What types of input could there be? Numbers
such as 5
, 21
, 100
are all valid,
but input such as abc
, 12z
, would be invalid.
Now, using cin()
to get the input will return a string.
However, you cannot use any of the string functions such as strcmp()
to check the input because you don't know what to compare the input
to.
One way of checking the string is to check each
character in the string. This is what the code below does. It checks
that each character in the string is a digit.
|
void getage()
{
char ch[80];
int valid = 0;
cout << "Enter
your age" << endl;
cin >> ch;
for (int i = 0; i <
(int) strlen(ch); i++){
if (isdigit(ch[i]))
valid = 1;
else valid = 0;
}
if (valid)
cout << "Your said
your age is " << ch << endl;
else
cout << "Invalid
input " << endl;
}
|
Note: Don't forget to call the
getage() routine from main()
and include the
prototype
near the top of your code or in your own header
file. Also, include ctype.h
.
Suppose you had to write a program that gets a number
between 0 and 9. Instead of using isdigit()
, to check the
input, you could check using ASCII code. This is what the code below
does.
Notice that the code first checks the length of the
user input. This is in case the user enters something like 5w
.
If I did not check the string length, then my program might accept 5w
as valid because it would just compare the first character 5
with a digit.
|
void getSingleDigit()
{
char ch[80];
int valid = 0;
cout << "Enter
a number from 0 to 9" << endl;
cin >> ch;
//check
to see if user entered a single character
If (strlen(ch) > 1) {
cout << "Invalid
input " << endl;
return;
}
//check
to see if user entered a digit
if (ch[0] >= 48 &&
ch[0] <= 57 ) valid = 1;
else valid = 0;
if (valid)
cout << "Your entered
" << ch << endl;
else
cout << "Invalid
input " << endl;
}
|
Note: Don't forget to call the
getSingleDigit routine from main()
and include
the prototype
near the top of your code or in your own
header file. Remember, the strlen() function requires the
string.h
header.
Combined
Character & numerical Input
It is simple to test if a character
entered by the user is an alphanumeric character; i.e.
a letter or digit. You can use the isalnum()
function.
If the character supplied to isalnum()
is a letter or digit,
the return value is nonzero. If the character is not a digit or letter,
a zero will be returned. The header file for isalnum()
is ctype.h
.
isalnum(ch)… // ch is a char variable
For example:-
isalnum('b') or
isalnum('1')
would return a nonzero. Whereas,
isalnum('$') or
isalnum('+'')
would return a zero.
Suppose you had to write a program
that gets the users postcode. What types of input could there be? Input
such as sw3olp
and se204yy
are valid, but
input such as *&9
would be invalid. Now, using cin()
to get the input will return a string. However, you cannot use any of
the string functions such as strcmp()
because you don't
know what to compare the input to.
One way of checking the string is to check each
character in the string. This is what the code below does. It checks
that each character in the string is an alphanumeric character.
|
void postcode()
{
char ch[80];
int valid = 0;
cout << "Enter
your postcode" << endl;
cin >> ch;
for (int i = 0; i <
(int) strlen(ch); i++){
if (isalnum(ch[i]))
valid = 1;
else valid = 0;
}
if (valid)
cout << "Your said
your postcode is " << ch << endl;
else
cout << "Invalid
input " << endl;
}
|
Note: Don't forget to call the
postcode() routine from main()
and include
the prototype
near the top of your code or in your own
header file. Also, include ctype.h
.