Comparison Operators and Boolean Results
The >> operator reads input into a variable, and the variable's type determines what input is accepted.
A Yes-or-No Question in Code
Many programs need to pause and ask a person a question that has only two sensible answers. Should the program continue? Should it save changes? Should it delete a file? A program that asks such a question has to do two things: collect the reply the person types, and then branch on that reply so the rest of the program behaves differently depending on what was typed.
This article follows one small function that does exactly that. Read the function once and try to decide what it gives back before reading the explanations that follow. The reasoning you do at that moment is the skill this article is building. Notice that the function has no loop and no second question. It reads a single character, makes one comparison, and leaves.
- cout << prints the question to the screen
- char answer = 0 declares a one-character variable with a defined starting value
- cin >> answer reads the typed reply into answer
- if (answer == 'y') chooses the true path only for the letter y
- return false handles every other reply
What do you think happens?
If the person types the letter n, what does ask_to_proceed() return?
Reveal answer
false
The if condition answer == 'y' is false for n, so the if statement does nothing and execution reaches return false. The function returns false for every reply other than y, including n.
Following the Function Line by Line
The function runs its lines in a fixed order, from top to bottom, and each line leaves something behind for the next one. Tracing it means writing down what happens at each line instead of trusting a quick impression of what the function 'probably' does.
- 1The cout line prints the question Do you want to proceed (y or n)? and moves the cursor to a new line.
- 2The char answer = 0 line creates a variable named answer that holds one character and starts with the value 0.
- 3The cin >> answer line waits for the person to type a reply and stores the character in answer.
- 4The if line compares answer with the letter y and decides which path to take.
- 5If the comparison is true, return true ends the function. If it is false, the if does nothing and return false ends the function.
How the Program Reads and Decides
The function does its work in two distinct moves. The first move gets a value from the person. The second move turns that value into a true or false result. The sections below look at each move in turn.
Tracing a Reply Through the Function
Here is the same function traced for two different replies. Work through each step before reading the result.
Tracing the replies y and n
What does ask_to_proceed() return when the person types y, and what does it return when the person types n?
- 1Reply y
cin >> answer stores the character y in answer. The condition answer == 'y' is true, so return true runs and the function ends.
- 2Reply n
cin >> answer stores the character n in answer. The condition answer == 'y' is false, so the if does nothing. Execution continues to return false, which ends the function.
y produces true; n produces false.
Notice that the function returns false for every reply other than y, including n. Uppercase Y is another reply other than y, because the comparison is against the lowercase letter. A false result therefore does not tell you whether the person deliberately refused or simply mistyped their answer.
Finding Where a Result Diverged
Suppose a teammate says the function is broken because it returned false even though the person clearly meant to say yes. Instead of guessing, walk through the code in order and check each step against what the programmer intended. This is the most useful habit when a result diverges from expectation: find the first place where the actual state stops matching the expected state.
- 1Read the code in the order it executes.
- 2Write down the value each line leaves behind, such as the value stored in answer after the cin line.
- 3Compare each value with what you expected at that point.
- 4The first line where the expectation and the actual value differ is the place to look.
Applied to the teammate's report, the trace would show whether answer actually holds y at the moment of the comparison. If the stored character is something other than the lowercase letter y, the if condition is false and return false is the correct result for the code as written. The divergence then lies in what was typed, not in the if statement. Tracing made that visible without any guessing.
A teammate's function returns false when the user types Y with a capital letter. Trace the cin line and the if line, then state the first line where the stored value stops matching what the person meant. Explain whether the if statement or the input is responsible.
Hints
- Ask what value answer holds after cin >> answer when the reply is Y.
- The comparison in the if is against the lowercase letter y.
Mistakes Beginners Make with Comparisons
Common mistake Using a single equals sign in the if condition
What it looks likeif (answer = 'y') return true;Why it failsA single equals sign assigns the value y into answer. It does not compare the stored reply with y.
Do this insteadUse the double equals sign: if (answer == 'y').
Common mistake Assuming false means the person refused
What it looks likeA reply of n and a mistyped reply both produce false.
Why it failsThe function returns false for every reply other than y, so the two cases are indistinguishable in the return value.
Do this insteadRecognize this limitation when designing calling code that must separate a deliberate refusal from a mistake.
Common mistake Expecting the input type to be chosen by the operator
What it looks likeBelieving cin >> answer decides on its own what counts as valid input.
Why it failsThe operator does not decide validity; the declared type of the receiving variable does.
Do this insteadCheck the declared type of the target variable to know what kind of input is accepted.
Common mistake Forgetting that the second return only runs when the first return was skipped
Predict the Return Value
Using the function ask_to_proceed() from earlier, predict what it returns for each of these replies: y, n, and y typed as a capital letter. Then explain in one sentence why each result happens by pointing to the line that decides it.
Hints
- Find the line where the stored character is compared with the letter y.
- Remember that the comparison is case-sensitive and that return false follows the if.
Key Takeaways
- The >> operator reads input into a variable, and the declared type of that variable determines what input is accepted; for a char, the read takes a single character.
- An if statement runs its statement only when its condition is true; a return statement ends the function immediately, so the second return only runs when the first was skipped.
- Use == to compare and = only to assign; in answer == 'y' the double equals sign tests the stored character.
- The function returns false for every reply other than y, so a false result does not identify a deliberate refusal.
- When a result diverges from expectation, trace the code in execution order and find the first line where the actual value stops matching the expected value.