Understanding variables and assignment is critical to being able to
program but is an easy thing to be confused about. Get over this hurdle and
programming will be far easier.

In this exercise we complete a series of dry run exercises where we step through short fragments of code. This is an important activity to reinforce your understanding of variables. The instructor will step through the first exercise with you.

Specifications

Use your paper from the last Solo Activity to record your answers, making sure to put the exercise name and your name on the paper.

Note that the underlined font shows what is typed by the user. As you work through the exercise, I suggest that you compile after each step so you know where an error is located if you make a mistake. Also, if you get stuck then ask a classmate or the instructor for help.

Specifications

Copy the following program into a text editor, save it as nameapp.cpp, and then compile and run the starter program to make sure you copied it correctly.

Finally, add code to your program to output the fullName variable using cout:

cout << "Full name: " << fullName << "!\n";

Compile and run your program to make sure it works correctly like this:

First name: Bruce
Last name: Hartman
Full name: Bruce Hartman!

Some text characters are hard to print, like a double quote ("). To print these we must escape them from their special meaning by putting a backslash (\) in front of them. Put a double quote mark around the full name by changing the line printing the full name to:

Statements inside the curly braces of the else clause only execute if the test condition in the parenthesis, (7 == guess), evaluates to false. For more information, see section: 3.3.4: Using if-else Statements.

Compile and run your program again and verify the output looks like:

I'm thinking of a number between 1 and 10.
Can you guess it?
Enter your guess: 9
You entered: 9
Sorry, that is not correct.
Rerun and try again.

The error message should appear for any number other than the correct guess.

One problem with our program is that a user may enter numbers outside the range of 1 through 10. We can test for this condition with one or more if statements. Add this code to your program after the input statement and before the other if statements:

if (guess < 1)
{
cout << "Error: guess must be >= 1\n";
return -1;
}

Checking user input is a common use of if statements.

Compile and run your program again and verify the output looks like:

I'm thinking of a number between 1 and 10.
Can you guess it?
Enter your guess: 0
You entered: 0
Error: guess must be >= 1