Lesson 1.5: Continuing with Hello World! Programming with C++ for Beginners

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.

Screenshot (345)
Hello World! program

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.

Screenshot (350)

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.

Screenshot (351)
Warning!
Screenshot (352)
The warning on line 7  narrows down the problem

Recall in the first lesson, I mentioned statements should end with a semicolon. Can you spot the error below?

Screenshot (353)

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.

Screenshot (354)

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.

Screenshot (355)
The code
Screenshot (356)
The result

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.

Screenshot (357)
An empty program

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.

Screenshot (358)
The code
Screenshot (359)
cout << “How old are you?” << endl;
Screenshot (360)
User inputs a number and that number is stored into age
Screenshot (361)
The value at age is returned

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.

Screenshot (362)
The code
Screenshot (364)
This makes more sense

More examples below:

Screenshot (365)
The code
Screenshot (370)
The result

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

Screenshot (371)
The code
Screenshot (372)
The result

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.

Advertisements

Comments

3 comments on “Lesson 1.5: Continuing with Hello World! Programming with C++ for Beginners”
  1. HI ! I need a lot more practice with C++. Do you recommend any websites?

    Like

  2. torie says:

    There’s an app I recently downloaded which is great so far! Learn to Code with C++ by Sololearn Inc

    It definitely touches on much of what I studied in a classroom. You should be able to check out a few apps and apply that knowledge to build your coding skills.

    I’ll try to write up a post with a few more resources in the next few days. I’m currently super busy with my computer science classes but I’ll try to get more tutorials posted soon!

    Like

  3. Respect to website author , some wonderful entropy.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s