if Statement
and Keyboard Input of Characters in C++.
Many programs allow users to select from alternative actions by requesting a positive or negative response to a question. This might seem like a simple process, but it actually involves some important considerations. Should the program accept any response that would be interpreted by any intelligent person as positive or negative, such as: "Yes", "OK", "Please do so", "No", "No Thanks" etc.? Another issue that must be considered here is the "case" of the characters. Remember that character 'Y' (uppercase) is not the same as 'y' (lowercase); so they must be tested separately. Just the word "no" can be entered in four different ways: "NO", "No", "no", and "nO". So a program would have to test for four different entries just to test for a "no" answer. There are eight variations of the word "yes". It should be clear then that testing for all possible positive or negative replies to a question would be excessive. So where should we draw the line? Program analysts must balance the efficiency of limiting acceptable user responses against the user-friendly nature of a program that might allow them more flexibility. Typically, the choice is made to prompt the user for only a single character of response, limited to "Y", "y", "N", or "n". Some programs even restrict the responses to just uppercase. But the four characters listed above (Y,y,N, and n) are the most common. In truth, most programs accept any character, then test just for 'Y' or 'y' and base their actions on that. The typical assumption is that any character other than 'Y' or 'y' should be treated as a negative response. This is not always the case, but is typical.
Consider the following situation that might occur in a program that is counting something. If we wanted to offer the user an opportunity to increment the counter (increase the value stored in a variable holding the value of the count), then we might handle it like this:

The flowchart segment above describes a process in which a prompt is displayed asking the user if the counter should be increased. The prompt urges the user to limit the response to one of two characters, "Y" or "N". The user's response is stored in a character variable named ANS, which is then tested for either "Y" or "y". If ANS contains either character, the flow of control passes down the true (T) leg and the value in variable C is increased by 1. If the test for "Y" or "y" returns a false result (as it would for "N", "n", or any other character), then the flow of control passes down the false (F) leg and no action takes place. The design above is based on the assumption that the entry of any character other than "Y" or "y" would be treated the same as the entry of "N" or "n".
Notice that the design above does not actually restrict the user's
entry to the "Y" or "N" suggested in the prompt. Rather
it it only tests for (and therefore acts upon) the entry of "Y" or
"y". All other characters (including "N" and
"n") are ignored because the false leg of this selection structure
is empty (has no steps). As such, there is no else statement in the code
following the if statement.
The C++ Language source code for the flowchart above would be:
#include <iostream>
using namespace std;
int main (void)
{
int C=0; /* Create and initialize a counter variable */
char ANS; /* Create a variable to hold a character response */
cout << "Add 1 to the counter (Y/N)? ";
cin >> ANS;
if (ANS=='Y' || ANS=='y') C=C+1;
return 0;
}
Note the required use of the parentheses around the condition in the code above.
Additional parentheses are often necessary because of the order of precedence of
operators in C++ (see the web page on Expressions and
Operators in C++). Pay attention to the use of the double equals sign
(==) to test for equality. Also note the use of the double pipe
(||) to indicate the use of the logical operation or.
Remember that relational expressions always produce Boolean (true or false) results and logical operators must be given Boolean operands to evaluate. Thus, it would be illegal syntax in C++ to use the expression
ANS=='Y' || 'y'
to check for either the uppercase or lowercase letter "Y" in variable A.
The correct syntax would be
ANS=='Y' || ANS=='y'
Notice that the first (illegal) expression does not have Boolean data on the right side of the || (or) operator, but the second (legal) expression produces Boolean data on both sides of the || operator.