C++ Lesson 2: Variables
Hello and welcome to my second lesson on C++. So we're going to create a new C++ project, and yeah, we're gonna save it in our C++ lessons folder which we've created in the last lesson. If you don't know how to do this, just watch Lesson 1; everything I explained will be all in Lesson 1. But I don't haven't already explained that—makes sense? Alright, so we'll call this Lesson 2, and we'll create it. Now we have our main.cpp. I'm just gonna make this a little bit bigger, and then I'll just focus on that, so it's easier to read.
Alright, so first thing we need to know: we don't need this, so we'll delete that. Now we know where to put our code, and we're writing on the program, so we don't need their hello word oral statement. So, and then we're gonna say using namespace std. So there we go.
Now that we've created our program, or our in-program workspace, we can start getting into today's tutorial. Today's tutorial is going to be on variables. So, um, in the last tutorial, I said you can print things out to the console by saying cout and then say a string. So I'll say "Hello, Mr. Cumpston." Alright, and then I'll build that, and then I'll view my console, and it says "Hello, Mr. Console."
Alright, well, what if—well, first off, why? Why have I been calling this a string? Like a string that’s just like a—a little long thing that I know there’s nothing new as a string? Well, actually, it does. So today we're going to be getting into variables and different variable data types.
So a variable, if you've—if you take an algebra or you might—this word might be familiar to you, basically it's something that stores information. So like x equals 5, here's our variable x, and then it's value is 5. Well, it's very similar in C++. So there are different data types and yeah, so I'm going to get into it.
So the first data type is int. Let’s make that a new line. Alright! Int + int is an integer. So I'm going to make all the variable names what they are, so the way I mean. So, and there's many, many different data types. So I'm just gonna get into the main ones that we're gonna use for these tutorials. So int is an integer—and I probably spelled that wrong. I really don't care. So we can say int space integer.
So now we have a new variable; we know its data type is an int, and its variable name is integer. So that’s good. We could leave that as that, or we could specify what integer equals. Like how we said x equals 5; one will say integer equals 5 as well. 5 is a nice month; we'll just—I'll space that out just the way it looks nice.
Alright, so we have int space integer equals 5. Here’s our data type, here’s our variable name, and here’s our variable value. Okay, now I'm going to show you a couple other variable data types. So that’s for an integer, and an integer basically means whole number—if you didn’t know that. So I'll say double, and then decimal equals 5.0.
Alright, so in stores integers; doubles store the decimals. I don't know why they called it double; you're just gonna have to memorize that double is for decimals. So 5.0 and five—I couldn't say integer in decim integer equals 5.0. Just, yeah, alright. So now, mm-hmm, another popular one is string. This is a different color because it's like, yeah, just consider it a variable.
Yeah, so that’s what I’ve been telling you about before. A string, as you already know, is text. So let’s say string space text equals—and then as you know, strings go into quotes—and then string space text equals "five."
Alright, next we have—oh, next we have a char, or as I like to call it, char. It stands for character but when you just write it like that, it looks like it says chars. I call it chars; don’t correct me! I know it’s supposed to be called a char, but I care, but I like to call it char, so that’s what I’ll call it.
So char space and then character—probably spelled that wrong, too. I’m not good with spelling; I'm not gonna check that. Alright, now a character is just the character on the keyboard, and these go in single quotes. So I could say exclamation point, I could say lowercase g, I could say capital G—I could say any number—but just one character. I can't say that; no, that’s just incorrect.
So just the way we don’t get it confused with the integer, I’ll make it an exclamation point. I'm fine now breaking the pattern; that’s okay because I break the pattern angles. So we have int which is for whole numbers, double which is decimals, string which is for text, char which is on our chart, however you want to call it—which is for um one character on the keyboard.
And EK here—and then this is the last one I’m gonna be telling you about. Feel free to look up more data types if you want: bool and bull trim. Now you'll notice that I didn’t put a space; that’s because variable names, first off, they have to start with a lowercase character. So lowercase T, and then you can't have a space in the name. So usually, so if it’s more than one word, people just make the second word and third or whatever capital.
So, away, uppercase. The way they can tell the difference. So bool space true false; and then that does exactly what it sounds like: it can either be true or false. So I'll make it false, and then I can run the code; obviously it’s gonna do absolutely nothing, and it’ll also give us a bunch of warnings. But these warnings are just that we’re not using these variables.
So I’m gonna show you how they’re all used—well, they’re pretty much all used the same way. So, uh, I just—I mildly pool because we're not even gonna get into that today. I just thought I might show you what it is. Bulls are like for if statements and things like that, which we'll get into later.
So I'll say also another way of declaring it. So now that we know all these, I’ll let you take a mental note of that, and I’ll just comment this out—it’s the way it doesn't affect our code. So we can—there's two ways to do a comment: you can either say slash slash, which comments that line. If you want to comment a lot of lines, you say slash star, which will comment everything after that, this last star until you say star slash.
And so now that chunk of text has been commented out. So another way of declaring variables—if you want to have more than one integer, you couldn't obviously say int a. Alright, int b, and then obviously variables are not going to have a value until I give them a value.
So I say equal, T, a bit of value. And int c, give a variable a new value at each time. So now three variables have been declared: one named a, one named b, and one named c. Nice to know, but um, that’s a waste of space. There's three lines there, so a shortcut is int a comma b comma c.
So then you notice the pattern: you just put a comma in between spaces there; you put a comma in between the variable names, and it’ll, as you can see, didn’t give us an error; I gave us a warning that we didn’t use those three variables. So now I think you’ve got a better sense of what variables are and how you can call them.
But when are they useful? Well, I’ll make a variable called age. So int age, and then this equals 17 because I am 17 years old. So now I’ll just—I’ll print out what I just told you in the console.
So cout—or cout—and then I can say—it’s string right here: "John is," and then I’ll put a space because it’s not going to press space automatically for us. And then we're gonna put another parameter here—so basically just another thing. So here was our first thing; it was a string, and our second thing is gonna be our variable. So I can either say "John is 17," or I’ll today I’ll say "John is 8."
Alright, and that sounds kind of weird, but it won’t look weird to the console. So if I build and go, "John is 17." Nice! This is very nice.
Alright, well, this is pretty cool, but our programs aren’t interactive. They actually just do nothing. All they do is fit things out to the console, and you can look at the console in town. Nice to know John 17. But let’s make these programs more interactive! So I'm gonna delete this, and I'm gonna just leave this comment just so that way if I get confused in what data type I’m using, I just look at this.
But in the next tutorial, this won’t be there, so learn to familiarize yourself with the data types. First, also the code runs, as I said, from top to bottom, so you cannot use a variable until you declare it. So we’re gonna make a program that asks someone for their age, and their—well, first of all, we’re gonna make a program to ask them to give them from their age.
Yeah, so, as I said, cout spit something out to the console. Now what I didn’t tell you is cin actually takes the things in from the console, which means the user can now do stuff and they’re not just sitting there reading text.
So first, I’ll say int space age semicolon because, well, we’re gonna ask that for their age. So right now we don’t know their age, so we cannot set it to a value. Then we’re gonna say cout, and then we’ll say "How old are you?" And then we’ll just put a new line so that way they’re not—don’t want to make semicolons.
So we'll put a new line for them to enter text where they want. So now that we see something out to the console, we’re gonna want to read in their age. So we’re gonna cin, and then the brackets go the opposite direction if they're reading in.
So it goes from the console into the program. So I think of it as just arrows that go away. I don’t know; just think about it however you want, but that’s the way the arrows work. Cin and then the variable name that we're gonna spit the data into. So we want to the age—we’re gonna work it up with the information; we’re gonna store the information they give us into the variable name age.
So cin age, and let me say semicolon. And then now we now know what that’s the same thing as saying "a in space age equals," and then whenever they said. But that’s great; nice to know, but time to use that. Time to actually do something with that!
So time for our next line of code, which we will say cout, and we'll say "You are," and then we’ll say space age. Forgot to put brackets here; don’t make that mistake.
So you are age, and there’s a space here because on it stored a number; it didn’t store a string. It didn’t—so, uh, it’s just gonna adhere to—just gonna display the number, nothing else.
So if you want to space in between our and age, you’re gonna put an extra space there. Now I’m gonna go ahead and do an extra step—this is for the faint of heart and people who are ahead of the game. And I’ll say "years old."
And then what did I do? What—oh, I messed up here! I need another space in the flame, and then I’ll say period.
So as you can see, you can keep going on and keep adding things. So I'm gonna add one more thing I don’t say "and," though. So this looks pretty complicated, but I'll break it down for you. So first, we declare a variable that we're gonna store their data called age.
Then we ask them—we print out to the console the string "How old are you?" and then we print a new line, then we read in from the console, and then we're going to store whatever we read in into the variable name age. Then we're gonna read out the string "You are," and then we're gonna read out their age or the variable name age—whatever read out the data that is stored in the variable name age.
And then we’re going to say another string, which is "years old," and then we’re gonna print a new line. So if you if you go through it step by step, you see it shouldn’t be that complicated.
So I’m gonna clear the log; we’re gonna run our program now. The program stopped; it’s not done—it’s a cin; it needs input from the user. So I’m going to go ahead and say 17, and it says "You are 17 years old."
Now, before I end the video, I hope I’m not making the video too long; I’m not really checking on the time. Alright, it’s already pretty long. Alright, I realize I’m going pretty slowly, but hopefully I’m helping you.
Alright, so now we’re gonna make our most advanced program yet. This is going to use more than one datatype and more than one variable. So we’ll say our string space first name.
Alright, and then we’re gonna make another string called last name. Alright, so now we have a first name and a last name. And then we’re gonna have a char or character for middle initial, so I’ll say middle_int.
Alright, now we’ll say out to the console, and basically we’re gonna make a program that tells them their full name. So we’ll say first, "What is your first name?" And I will give them a new line, and then we’re gonna read in—and we’re gonna read into the variable name first name.
And here they’re gonna enter—they can’t—they will enter not a number; they'll enter a chunk of text. So if you want to think about what they’re gonna enter.
So, um, when you when you declare the variable, because if they were to enter a number, then we wouldn’t need a chunk of text. So cin into first_name. Alright, then we’ll cout another statement, cout "What is your last name?" And then we’ll put in and they’ll statement right there, so it looks nice.
And then we're going to read in their last name into that variable. And then we’re gonna ask them what their middle initial is, which is a char. So there’s gonna be a new type of this—is gonna be a new data type that we’re using.
So we’re gonna say cout "What is your middle?" I’m gonna spell this wrong; I guarantee you. Nah, I’ll just say "What is your middle int?" because I don’t want to be embarrassed all over the Internet by spelling something wrong.
Alright, so what is your middle eight? Alright, and since I’m the user, obviously I know what I meant by that, but anyone else would. So learn how to spell and don’t be like me! Alright, so then we’ll give them a new line and just like any other data type, we’re going to read it in the same—what am I doing? No, we’re going to read it in the same way.
So we cin console in and then the name of our variable middle_int semicolon. So now we know their first name, you know their last name, you know their middle initial. Time to tell them their full name!
So we’ll say cout space, and then we’ll say first—we’ll say "Hello,"—we’ll greet them—hello! Then bay, so first we’re gonna say their first name. So my first name is first_name.
And then we’re not gonna want all their names right next to each other, so we’re gonna say we’re gonna have to put a space right there. So we can keep going and keep going, but all the important thing is in between all things; we have to put these brackets.
And I apologize that the video is getting long, but I don’t like making videos in parts. So I'm gonna have a 20-minute video, or however long this is going to be. So first name, and then we’ll say the middle initial, then we put the brackets again, and then we don’t want—we want a space in between their middle initial and last name, so we’re gonna put the quotes.
And I’m actually gonna put a period also because most people put a period after their real initial to abbreviate, and then I’ll say last name, and then I’ll just underline.
So this looks like a pretty long line of code, but it’s pretty simple. It’s not that complicated; just doing a lot. It’s telling them the string hello, then it’s telling them their first name, then it’s telling them last name, period, and afterward, so they’ll see some blank spaces, but it won’t matter.
So now we’re gonna run this code. Hopefully we have no errors. You’re gonna go into the console: "What is your first name?"
John—I’ll say, you know, if it’s my phone, my asking me for my phone. I’ll enter Jonathon Johnny T.
Hmm, what is your last name? What is your middle initial? So again, I can only enter one character. I’m gonna make it a capital M.
Alright, hello Jonathan M Loeb! Well, hi thanks to you; thanks for greeting me. Alright, so hopefully now you get a better sense of what variables are and how to use them.
In this tutorial, we’ve covered different data types, variable names, what data types—what information can be stored into them. We also talked about different ways of declaring variables, and we’ve also made two programs that use variables, and this one actually uses several different data types, and it printed it out, and we learned to read stuff in from the consoles to make interactive programs.
So the video—oh! And that about, wow, 23—about 24 minutes. Sorry for such a long video, but we covered a lot of content. I didn’t know how to make it very clear to beginners and make it a short video.
Anyways, thanks for watching! If there’s something you didn’t fully understand, just shoot me a question, and I’ll be glad to respond. Thanks for watching!