Welcome to Lesson Two!
[If you missed the first lesson, you can find it here]
Wasn’t that last lesson fun? No? It was boring? Okay, I admit, introductions are normally pretty dry and dull. You’re probably itching to try some cool things on your own, but you’re bored of just outputting to the screen. Today’s lesson will give you a little more to play around with, I promise!
Data Types
In C++ (and practically every other programming language), we use something called a variable to hold data for us while the program is running. However, once the program has quit, these variables are lost unless you save them to a file somewhere (or upload them to a server). We won’t be creating or opening files quite yet, that will be further down the road. However, we will be learning some important types of variables and practice when it would be best to use them.
Storing Numbers
To store a number, we have a few different types of variables. For now, we will focus on int, float, and double. A variable with data type int (standing for integer) will hold a whole number from -2147483648 to 2147483647. Remember, this is only for whole numbers. If we wanted to use decimals, we would either use a float or double data type. The float (standing for floating point number) will allow a decimal value of about 7 digits. If you need to store a larger decimal value, try using a double (double precision floating point number). The double data type will hold about 15 digits.
Storing Characters
Characters (that is to say, anything in the ASCII table) are stored individually in a data type called char. One char variable by itself can only hold one character. Later on when we talk about arrays, I will discuss ways to hold more than one character by using more than one char. I’ve said too much, if I tell you any more now, I’ll have to kill you (that escalated quickly).
You can store entire words and sentences using a data type known as string. HOWEVER (I just had to go an ruin it, didn’t I?), string is not supported in the standard C++ libraries, so you have to include it in your code. Remember how we had to type “#include <iostream>” to include input/output? In this case, as you might guess, we will have to write “#include ”
Mr. Bool(ean)
Named after George Boole, the boolean data type holds the simplest of data: true or false; As it’s referred to in C++, “bool” is a very important data type and could possibly save you hours of work and many lines of code. Simply by storing a true or false value, you can set your program up to make “decisions” based on what is true and false. This won’t be very apparent until we discuss in full what is called a “truth table” for boolean logic. That lesson will be in the future. At first, you will either completely understand it or become overwhelmed and lie awake at night wondering just how the hell a 0 and a 1 could be so confusing. I can guarantee that with a little bit of work, you will eventually understand it and it will change your life.
Creating Variables
It’s rather simple to create a variable. Let’s say we wanted to create a variable called ‘x’ that held the value of 3. Since we’re storing a whole number (and let’s assume that we will never store a decimal number in x), we will use the data type int. We can declare (create) and assign (give x its values) the variable in the following ways:
int x; x = 3;
OR
int x = 3;
Furthermore, if you are declaring two or more variables but not assigning any of them until later, you may do something similar to the following:
int x, y, z;
Please be warned that if you do the above declaration, all of your variables must be of the same type!
Naming Your Variables
Perhaps the most important part of creating a variable is knowing what it does! This is why naming your variable something useful is a must. For example, if you are creating an integer that will hold the user’s age, then perhaps you would just call the variable “age” or even “usrAge”. How you name your variables is up to you, but try to keep the name as descriptive and easy to write as possible. For multiple words, I start with the first word lowercase and the first letters of the following words uppercase (as I did with “usrAge”).
Variables must begin with a letter and may contain numbers after a letter. This means that “1234variable” is not an acceptable name. However, “variable1234” is perfectly fine (although not very conventional). You’ll get used to this the more you do it, so without further ado, let us begin the tutorial!
[For more on variable types and conventions, check out cplusplus.com]
Our First Real Tutorial!
Okay, I know we did a “Hello World!” earlier, but let’s get into the real stuff, shall we? Let’s start by asking a user their name, age, and favorite color. First, we’ll need to figure out which libraries we want to include. As I said in the last lesson, we will always include “iostream”, but now that we’re going to be holding information such as the user’s name and their favorite color, we’re going to have to include “string” as well. After we’re done with our includes, we’ll need to tell our compiler that we’re using the standard (std) namespace.
Create a new project (go here if you can’t remember how) named Computore 6000 and add the following to your (empty) main.cpp file:
#include <iostream> #include <string> using namespace std;
Next, we’ll need our main function, so let’s create that by typing:
int main(void) { return 0; }
This is where the meat of our program is going to live. Since we’re going to be storing our user’s name, age, and favorite color, we’ll need three variables. We will be storing the name and favorite color using strings and the age will be stored using an int. Enter the following just after the opening curly brace (‘{‘) of your main function and before your return statement:
string name, color; int age;
Since name and color are both strings, I’ve declared them together. We are going to assume that both the user’s name and favorite color are one word each. Otherwise, we will run into some errors. We will discover solutions in future tutorials, but let’s enter only one word per each.
We will need tho ask the user his or her name, so we will need to use “cout<<“.
cout<< "Hello, my name is Computore 6000, what may I call you?" << endl;
You may notice a weird looking “<< endl” in the above code. “endl”, as you may be able to guess, stands for “end line”, meaning that the console window will begin a new line for the user to enter their information on. You can combine a number of things on one cout statement by adding another streaming operator (“<<“). We will see more in just a few seconds.
The opposite of “cout<<” is (bum budda dah) “cin>>”. Whatever we place after the streaming operators “>>” will be used to store the information that is coming in. Note: cin will only store the first word it sees in the context we are using it at this time. Enter the following code to store the user’s name.
cin>> name;
We will continue asking and repeating the user’s information until our program is finished. Write the following code to do just that:
cout<< "It certainly is nice to meet you, " << name << "!" << endl; cout<< "May I ask your age, " << name << "?" << endl; cin>> age; cout<< "Wow, you look MUCH younger than " << age << "!" << endl; cout<< "May I ask what your favorite color is, " << name << "?" << endl; cin>> color; cout<< "How lovely! I also love the color " << color << "!" << endl;
The above will output:
“It certainly is nice to meet you, [user’s name]!”
“May I ask your age, [user’s name]?”
[The user enters his or her age]
“Wow, you look MUCH younger than [user’s age]!”
“May I ask what your favorite color is, [user’s name]?”
[The user enters his or her favorite color]
“How lovely! I also love the color [user’s favorite color]!”
Altogether, the final code for this project will look like this:
#include <iostream> #include <string> using namespace std; int main(void) { string name, color; int age; cout<< "Hello, my name is Computore 6000, what may I call you?" << endl; cin>> name; cout<< "It certainly is nice to meet you, " << name << "!" << endl; cout<< "May I ask your age, " << name << "?" << endl; cin>> age; cout<< "Wow, you look MUCH younger than " << age << "!" << endl; cout<< "May I ask what your favorite color is, " << name << "?" << endl; cin>> color; cout<< "How lovely! I also love the color " << color << "!" << endl; return 0; }
Conclusions
As always, you can find the source code for Lesson Two by heading to my website’s special location for such files. Just click on lesson_two and then on computore_6000 to find the source code. All-in-all, I believe that this lesson thoroughly covers the concepts aimed at you today. If you have any errors, concerns, comments, or requests, please email me at zdaily@nfptelecom.org.
Play around with your newly found skills until the next lesson appears online. Speaking of that…
Coming Up…
Next, we will talk about if statements in order to make your program print different output according to certain conditions and user input. It will be a relatively short lesson, but will be very important nonetheless.