Previous lesson: Lesson 1: Hello World! Programming with C++ for Beginners
This is a mini lesson working with the Hello World! program from lesson 1. After this lesson, you should be familiar with compile errors and simple user input and output.
In the first lesson, I mentioned strings should be enclosed between quotation marks. The screenshot above this paragraph is coded correctly. Let’s see what happens when the quotation marks are removed.
Without quotation marks, the compiler treats the string as a variable. It looks for a variable in the program named Hello. Since we never declared a variable named Hello, clicking the build hammer results in a compile error. Compile errors are mistakes that prevent the program from building.
Make it a habit to treat every error seriously. Something as simple as a missing semicolon in one location can create numerous errors elsewhere.
Recall in the first lesson, I mentioned statements should end with a semicolon. Can you spot the error below?
In this small program, it’s easy to find the problem. A semicolon is expected on line 7 before the right closing brace on line 8. These errors are trickier to spot in more complicated code so do your best to avoid making them in the first place. Of course, this is easier said than done and every programmer makes mistakes.
Let’s say you want to print Hello World! as a variable. To do this, we need to declare a string variable. On line 7, I declared a string named word and assigned it “Hello World!” Notice, we need to include the quotation marks because we’re working with a string. On line 8, we tell the compiler to find a variable named word and print to the console.
In the previous example, declaring a variable named word is not necessary because there isn’t anything special being done with that variable. Let’s clear out the code in the main function and put a variable to work.
Take a look at the code below. On line 7, I declared an integer named age. (In the next lesson, I’ll cover declaring different types of variables.) Line 7 declares the variable and assigns it a random number. We will use it to take user input.
On line 9, we print the string “How old are you?” to the console. Then, the user enters a number. On line 11, the user input is stored into age. On line 13, the value stored in age is printed.
When we run this program, we should see the question, it takes the user’s answer, then gives us back that answer.
The program works but doesn’t make a whole lot of sense. Let’s return the value within a sentence. In the code below, user input is combined with strings to return something that makes more sense.
cout << “You are ” << age << ” years old.” << endl;
Notice the quotation marks around each string. There is also a space in the first string after the word are and a space in the second string before the word years. You must format your output exactly how you want it displayed. Luckily, if you compile the program without spaces, it’s an easy mistake to fix.
Also, note that strings are in green and variables are in blue.
More examples below:
In the following code, int age and string name wait for user input but int robotAge is given a value. We could still ask the user for the robotAge and save it as the new value, but the following program only asks for the age and name and uses the declared value for robotAge.
Note: When declaring variable names, use camelCase. The first letter is lowercase while subsequent words use uppercase. nameVariablesLikeThis
In future lessons, I’ll dive into more details to help you start building cool programs with C++. For now, get comfortable with variables and user input. When messing with user input and strings, keep to one word answers. Strings can get rather complicated and don’t always work like you might think.