C++ Crash Course 👨‍💻 (𝙁𝙧𝙚𝙚)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going it's you bro here and i hope your day is going well and in this video series i'm going to teach you guys c plus plus for free i'm not here to try and sell you some course because i'm your bro and i have your back my lessons are free and they always will be if you want to learn c plus look no further because you're in the right place let's get into it if you wouldn't mind please like comment and subscribe one like equals one prayer for the youtube algorithm let's begin by discussing the advantages of c plus over other programming languages so c plus plus is a fast language in fact it is great for games if not the best programming language for games because of its speed so if you ever want to get into game development i would highly recommend learning c plus at least at some point maybe not as your first language but eventually you want to learn this so c plus plus is also portable between different operating systems such as windows mac and linux c plus plus also supports object oriented programming and c plus plus jobs are in demand currently at least in my geographical location starting salaries for new c plus developers are starting at least at sixty thousand dollars per year but that also depends on your geographical location as well and if we talk about the advantages we also need to discuss the disadvantages of c plus plus over other languages so c plus plus is a difficult language to learn i would not recommend it for your first language now if you're watching this video and you're trying to decide on what you should learn for your very first programming language i would recommend a few other things instead of c plus just because cf plus is a more difficult language to learn if you go to my channel i actually have a few different playlists and if you're undecided on what you want to learn i would recommend starting with python just because i would say that it's the easiest programming language to learn compared to all the others and other disadvantages of c plus plus is the use of pointers and you'll learn what these are later misuse of them may make your system crash there is no to little security with c plus plus and there is no garbage collection but remember that all programming languages have certain advantages and disadvantages the advantages i would say far outweigh the disadvantages well what are the uses of c plus well i'm glad you asked it's used for many popular applications from web browsers to databases to desktop apps it's because the language is so fast and efficient that any application that's requiring a lot of processing power they actually better be using c plus because it's one of the fastest languages c plus plus is also used for many game engines and for example it's used in unreal engine because of its speed and it's also compatible with many embedded systems for electronic devices and microcontrollers the c programming language might be better for this but c plus plus also has the capability to be used for these embedded systems as well as an option now you're probably wondering hey bro what do i need to start programming in this language well to answer your question what i would recommend is downloading an ide that's an acronym for integrated development environment it's basically software that helps us write other software and there's a few recommendations of mine you just got to pick one of these i would probably recommend microsoft visual studio to begin otherwise there is a package for eclipse for c and c plus plus developers otherwise there's code blocks but i think that's a little outdated now but personally i prefer microsoft visual studio because you can also use it for c sharp and there's some packages you can download for game development which is pretty cool so if you guys have no preference i'll walk you guys through how to download microsoft visual studio if you have a different ide in mind feel free to use that one too to begin we need to open up the interwebs go to any web browser and double click it to open up the interwebs good step one is complete step two is to go to google and look this up microsoft visual studio code and then you'll want to look up the community version and it should be this link here visual studio community and we're going to download visual studio and we are going to close out of this because we do not like pop-up ads and if you're running windows you have to open this when done if you're using mac i believe you have to drag this to your applications folder i'm not a mac user so i'm not 100 sure on that we're going to click yes and we're going to wait here a while click continue and you're going to have to wait again well you should be brought to a screen such as this one scroll down and click these two boxes desktop development with c plus as well as universal windows platform development just so you know there's other packages you can download as well you can use visual studio community for c sharp unity and c plus game development so keep those in mind you can always uh select those two if you want all right then we're going to click install we don't feel like taking a survey right now so we're going to click not now and we'll continue waiting well welcome back we're just going to click this launch button sign in with the microsoft account otherwise click not now maybe later alright so with the development settings you'll want to go to visual c plus plus and then you can choose a color theme the standard is blue but if you want to feel like a pretend elite hacker you can select dark mode and for now we will continue without code here at the home screen we'll want to go to the top file new project and we'll scroll down to c plus console app click next we'll need a project name call this whatever you want i'll call mine hello youtube and you can set a location for your project i'm going to set it to a folder on my desktop or you can keep this as the default it doesn't matter and you can pick a solution name a solution is basically like a grouping of projects here it says a container for one or more projects you can just keep it the same if you want and we're going to click create let me show you guys where you would want to create a new c plus file so go to your solution explorer and for some reason if you don't have this window or you can't find it let's just pretend that we close out of it you'll want to go to the top toolbar view solution explorer and you do have the option of moving this around if you prefer this in a different spot and you'll want to go to source files and there may or may not be a file here already so this is like a test file and it's actually right here behind our solution explorer if i can move it all right yeah so this is our test file and all this does is that it displays the words hello world and you can actually run this just by clicking the screen play button and if you did have that file a small console window that says hello world should appear however you might have a different color scheme because i've changed this in past tutorials but at the end of the video i'll show you guys how we can actually change the color scheme for this as well all right so let's say that you don't have this file hello youtube.cpp or whatever else you name this so let's just say that we delete this so we'll remove it now to create a new file go to solution explorer source files right click add new item then click c plus plus file then next you'll need a name for this i think i'll just call this main that works then click add all right let me shift some things around why is this so difficult okay this should be good enough so i zoomed in a little bit if you want to zoom in on your code you can go to the bottom left of the window and you can change it here i think 250 is a good amount for this video at least all right so we have a blank c plus file let's create a program where we display text to the console window well now you should have a blank c plus plus file if you want to create a program to display some text to that console window like we did before this is what you're going to type at the top a hashtag like it's a twitter hashtag type in the word include less than sign greater than sign and within these two symbols type io stream what we just typed this is a header file library it lets us work with input and output next hit enter to go down one or two lines it doesn't matter and then we're going to type int main parentheses then a set of curly brackets so what this is this is the main method so this is the entrance point to our program and when we run the program it's going to execute any code within the main method from the top and it's going to work its way down now let's say that you want to display some output to the console window this is what you type type in std and that is short for standard and i'm not referring to sexually transmitted diseases then you need a set of colons then type in c out which is short for output and when you combine these both together it means standard output then next you need a set of less than signs this passes information to our standard output to display to the console window and then next you can type in some text to display to that console window simply by putting it within a set of double quotes so here you can type in the typical hello world but i like to be different on this channel and i'll type something else so for your project why don't you type in some lyrics to a song you like and then post it in the comments down below because i would like to read it so for one song that i like i'm going to type in this and then finish everything off with a semicolon all right let's run the program yep and here are the lyrics however everything here is kind of compressed you can actually add a new line to the end of this so it gives you some more room so we're going to go back to our code to do that so if you want to add a new line just type in backslash and and that is a new line character so let's rerun this here's the lyrics that we had before plus it added a new line after our lyrics which we intended so if you're annoyed by having your code on a separate panel or window you can just drag and drop these so it's all like on the same window then so it's a little bit more convenient to work with all right so next let's say that you want to add a second line of text well you just type in the same thing basically so std two colons c out two sets of less than signs and then if you want to type in some uh words like lyrics you just put it within a set of double quotes so i'll type in the next line for this set of lyrics [Music] and then we have the option of finishing this off with a new line and then don't forget that semicolon at the end all right so let's run this and here's the results it says never going to give you up never going to let you down so you can have multiple lines to display output if you so wish and in the next video we'll be discussing more on how we can display output so before we wrap up this video let me show you guys a few tricks that we can use to change the color scheme of the ide as well as the text and the color of the console window so let's say that you don't like your current ide color theme let's say that you don't like the stark theme i don't know why you wouldn't if you want to change it go to tools options and then you can change it here you could go to let's say blue then hit ok and this should change the color scheme but that is very hard in the eyes but some people might prefer this and they prefer to be blinded but i'm going to go back to dark because i am a pretend elite hacker if you want to zoom in or out of your code you can change that here in the bottom left corner of your window you can either pick one of these few settings or you can type in a number that you like i think 250 for me is a good amount so lastly you can change the color scheme of the console window that displays more specifically this one i think the default is white text on black background if you want to change that actually i already changed mine but if you want to change it click the top left corner then go to properties and then here you can change the font size you can change the cursor size the layout of the window size the colors actually let me pick a new font close enough alright and then uh you can change the colors here you could have like green on green background which i would not recommend i think i'll stick with the green text on black background oh i guess you can also change the opacity oh that's kind of neat [Music] then just click ok with any of your changes and you can see that i changed the font style slightly so that's how you can change the uh appearance of your console window so that will conclude this introduction to c plus plus in the next video we will get more in depth with displaying output and eliminating some redundancies that we have here i'll explain how that works in the next video but i'll also include some useful links in the description down below so this video was very exhausting to make feel free to smash that like button and drop a comment down below hey what's going on everybody it's bro here and in this video we're going to be discussing output and a few other miscellaneous features with c plus let's get into it if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running first things first i want to discuss with you guys the use of namespaces in programming we can't have variables functions and other things with the same name so to avoid that from happening we can use a namespace different namespaces allow us to reuse the same names for things as long as they are within a different namespace we can reuse that same name in a different namespace in our standard output we are saying that we are using the standard namespace for our output and it can be annoying to have to type std before every line but there's a trick that we can use where we don't have to write this every time so outside of your main method this is what we're going to type type using namespace std so this is telling the c plus compiler that just assume everything here is using this namespace so we no longer have to type this out explicitly and this will work just the same then next let's discuss the return zero statement that you might see at the end of a main method so programs may calculate or process something and return the result what you may see at the bottom of many functions in c plus is this return keyword and they usually return some sort of value at the bottom of your main function we're going to want to return zero and this isn't necessary but i'll explain what this does in just a moment so in c plus this is sometimes referred to as the exit status what this does is that our program if it runs fine without any errors it will return zero and this is like a success status like saying the program ran fine with zero errors however this is only required in c programs it's optional in c plus plus the compiler automatically adds a return zero if we don't explicitly write this but in case you do see this this is what this means let's move on to line breaks when we write output statements our output will only add a line break if we explicitly let the compiler know that we intend it to so for example let's remove these new line characters and see what happens when we run the program so our output is all in one long run on sentence it's all on the same line even though our code is in two different separate statements so you have to let the compiler know that you intend to break this line and go down to the next line as if you're hitting enter and there's actually two ways to do this the first is that we covered it in the first lesson you just use the new line character and i'll get into this more in depth later on in this video the other way is that we can add these two left angle brackets and these are sometimes referred to as the insertion operator they basically pass along data and then add end l then semicolon and this is short for end line but make sure you take out that other semicolon and this will work just the same then so let's add that here as well so this is another way of writing what we had before each has their own uses yeah and you can see that each line has a line break and shifts down to the next line after it's finished the second option to use a line break is to use backslash n which is what we did previously but i'll explain this more in detail so the backslash portion is part of something called an escape sequence depending on what character that we add after the backslash it will have a certain effect on our output if we add backslash n it will add a new line and for example if i add backslash n before each of these words it's actually going to print each of these words on a new line so let's try it out and you can see that changed our output so you can use this escape sequence for a new line anywhere you want within here and there's other escape sequences as well besides new line like backslash t is for tab so let's try that out so this will add a tab before each word and you can tap things and space things if you need to write a set of quotes like you're quoting something you might have trouble because your program won't know where our string of characters begins and where our string of characters ends then so you can use an escape sequence to add some quotes uh which is backslash and then single quote or double quote so if you want to like quote something like we're quoting lyrics to a song here you could just add backslash and then a set of double quotes and let's try this out and you can see that our first line is now within quotes as if we're quoting something and for some reason if you need to print out a backslash you actually have to type double backslashes and let's try that out and here's our backslash because remember if you have just a single backslash the compiler thinks it's a beginning of an escape sequence so if you need to print it out you need double backslashes so those are a few of the basic escape sequences that you should be aware of there's still many more out there and if you have time i would suggest looking through online to see what else is out there if there's any that would be relevant for you but those are just a few of the basics before we really start to dive into programming with z plus plus lastly for this video we are going to discuss comments comments are a portion of code that is ignored by the compiler it's like a secret message and it won't be displayed or processed they're used to leave notes for yourself or for other people reviewing your code to read it's like a secret message here's an example of us using a single line comment so what you're going to need to do is to type in two forward slashes and you can see on my screen that this text became a green color and we can write a comment here so perhaps for a comment i'll write lyrics for a song that i enjoy so if we were to run this program it's not actually going to display this comment and you can see that it skipped it it's basically like a secret message for yourself or for somebody else that's looking over this code so you can also write a multi-line comment all text that you want to be included within a multi-line comment should begin with a forward slash followed by a asterisk and you'll notice that all this text now became green because we're saying that we want all of this to become a comment everything after these two characters so you'll want to put a ending position for where you want this comment to end if it's on multiple lines so let's say that we want our comment to end here what we need to type is then asterisk and then a forward slash so everything within here will be a comment and we can add multiple lines to here like i can say something such as these lyrics are actually very good all right so then let's run this and all of this will be ignored it's like a secret message that either i myself can read or other people can read well that will conclude this topic on writing output escape sequences comments and a few other things i forgot to discuss in the first lesson so in the next lesson i will be teaching you guys about variables but yeah that will conclude this topic on some of the fundamentals of c plus plus hey what's going on everybody it's youbro here and in this video we're going to be discussing variables in c plus plus let's get into it do you guys remember in math class in school where there was an equation such as this and you can solve for what the value of x is and then you can reuse x as if it were that actual number like here in our example x equals two and then if we use this in another equation x behaves as if it's the number two and we can produce a result so for all intents and purposes our variable here could store some sort of value and it will behave as the value that it contains and we can reuse those variables elsewhere in our c plus programs however we are not limited to only assigning numbers to variables we can also assign words single characters or even these things called boolean values which hold either true or false heck we can even assign entire sentences to a variable now these are the steps to create a variable in c plus plus the first is that you need to come up with the name let's pick something simple to begin with such as x just because we're used to using x like in math class we'll say x equals and then we can give it a number if we wanted to let's say 2020 and then finish this with the semicolon all right so there's one more step that we need we need to declare what type of value that this variable is going to hold so there's certain rules that apply when declaring variables and if this variable is going to store a value of some sort we have to declare what type of value that is going to be stored within this variable for example this could be a whole integer a decimal value a single character a string that's like a series of characters like a word a boolean value etc so for example if we wanted to store a whole integer value we would have to declare this of the int data type which is short for integer but there's other data types that we should cover so let's discuss what those data types are now before we really begin to discuss data types these are only a few of the data types that are available to us i'm only going to be covering a few of the basic ones just because they can get somewhat complex but i want to give you just enough information to get started with using data types and then as you understand how these work you can look at other data types to see what would suit your needs so to begin let's discuss the integer data type this will only store a whole number an example would be an age or a year like you couldn't store a decimal value within here like you wouldn't say for like a year you wouldn't say it's like the year 2020.5 i guess for an age though if you're a young kid you might say like i'm seven and a half years old but typically people don't put like a decimal in their edge so if you want to declare a variable of the integer data type you just write int before the variable name a double will store a decimal an example would be a price and you just write double before the variable name and the reason that this is named double it has to do with double floating point precision it relates to computer science but think of double as a decimal so you can store something such as a price anything that contains a decimal portion the character data type sometimes referred to as char like you're pronouncing charizard will hold a single character like a letter or a symbol a string that can hold one or more characters an example would be somebody's name or even an entire sentence or a paragraph and this also has the capability of holding a single character but we treat strings and characters differently and we also have boolean which can hold either a true or false and this is useful if you need to verify if something's correct or not if something's on or off and to declare something of the boolean data type you just write b-o-o-l before the variable name now remember these are only a few of the most commonly used data types that would be used for beginners so there's definitely more out there just so you're aware but these are a few just to get us started let's head back to our program now if we want to store a whole integer within a variable we have to declare this of the integer data type and according to that cheat sheet we had to place int here for integer now we can reuse this variable as if it was this number we can actually display this if we really wanted to we'll use our display method c out and then we'll just place x here now we don't need to put anything within double quotes because that will literally print something so we can just reuse x and then i'm forgetting that semicolon at the end all right let's display this and here is our result 20 20. now when naming variables it's important to name it something that will help you and other people identify what this is the letter x isn't too descriptive we can rename this something perhaps as year and that's much more descriptive and we will have to change it here as well so you're not limited to just having a variable named x you can name it other things too like year is more descriptive of what kind of value that this variable will contain and this will work just the same as it did before despite the name change how about we create a couple more variables and let's have them all be related to how about cars i like cars so let's create a few variables based on the data types that we just learned about so if we want a price we can use a double value because that can hold a decimal so we'll say double and then we'll create a variable named price equals and then we can assign a decimal value here so 9999.99 sounds pretty good to me i'm thinking for our next variable we can assign a char value a single character so we need to say char and then how about a status like is our car new or is it used so we'll say status equals and then if you're assigning a chart value you need to put it within single quotes and you can put any one character in here doesn't matter which one uh so how about we say n for new u for used so that would work we need a string still so how about a model of a car so we'll say string and then model equals and then if you're assigning a string you need it within double quotes so pay attention that with integers you don't need any quotes same with doubles uh for chart values you need single quotes strings you need double quotes so i'll pick my favorite car which is a mustang and lastly we could use a boolean value so we'll say b-o-o-l short for boolean and how about we create a variable named for sale like is the car for sale true or false yes or no so we can say you know true if it is or false if it isn't but i want this card to be for sale so i'll say true so there we go we have successfully created five different variables and now we can reuse these variables elsewhere in our program and we can display these variables much like we did with the year however if we're going to be displaying multiple variables we probably want to separate these by line so actually i'm going to add e and dl at the end of this just so we can move each of these variables to the next line so endl then we'll say c out how about status next endl same process with model and for sale all right let's display this all right so pay attention for the boolean value of for sale it actually came up as a one and that means true if it was zero that means false now when we display variables we can also display these variables along with some text as well so let's say that we want to make some sort of chart where we display all this information for a car well the first step to do that is that we'll want to write a string of what we want to display so let's say that we want to display the word year and not the variable perhaps a colon and then a space and then we'll display whatever value is within year however compared to other programming languages you have to add like a plus here to display the variable however in c plus you need to use the insertion operator so that's one thing that's different and this should go away in a moment yep okay so that cleared up so if you want to combine displaying variables and some text you need to separate each of these with an insertion operator so let's do the same with each of these variables here so we'll say price then use the insertion operator so status i believe that was new or used so probably i'll put like new slash used so it's more understandable and this will just take a moment so we'll say model and then for sale actually uh i think i'll say availability i can never spell this word available it's probably wrong but close enough all right so let's display this now all right so you can see that we displayed some text along with our variable for each of these lines except i messed up spelling model and availability so let me go and fix those because it's actually going to bother me if i don't okay that's a lot better so we have like the year here 2020 it's a price uh hundred ninety nine dollars a new mustang availability one which also means true zero means false now i'm going to take the next part of this video just to show you guys a few different tricks that we can do with variables for example we can reassign values later on in our program let's say that this car is no longer for sale we can say for underscore sale the name of this variable now equals false so when we display this it's now going to show the updated value when it was last updated so this should display zero now and take notice that i didn't have to declare what the data type of this variable is because you only have to do that once and if you declare this variable type once you don't have to do it again also it's possible to declare a variable and not assign it a value right away in case like you want to assign it later for example we could just say for sale is going to be a boolean variable but we don't know what value we want to assign to it quite yet so we can just declare a variable and not assign any value and later on in our program we can assign that value later on one thing that you should be aware of when using variables that contain a integer or a decimal is that you can use these as if they were this number and that means you can also use this for math functions let's say for our price there's like a 10 discount what we can do is we could say the price equals whatever this price currently is times 0.9 that would be the same as a 10 discount and this price will actually be updated so let's run this and these are actually treated as numbers now like you can see the new price is eight thousand nine hundred ninety nine dollars now with strings you can also combine strings together for example let's say that we want a variable named make so the make of this mustang would be ford all right so we have a make and a model so we can also combine these together so we could say perhaps string how about car equals make plus model and let's display this car variable instead of model so we can change this to car and we will display the car variable now so this should say ford mustang all right but see that it combined both of these words together and it doesn't have any space between them so you also have to take that into account so we could say make plus a space plus model equals car and this would also be an option too if you want to do some separation so it's kind of like our car which is a string data type is holding like a whole sentence now it seems so those are the fundamental uses of variables they can store a value but you have to list what data type they're storing and they will behave as the value that they contain and you can reuse these later on in your program for any purpose and you can name these basically whatever you want but there's a few keywords that are off limits now please be aware that there are more data types than this i went over only a few of the more basic ones just so that we can get the hang of things um there's more if you look online for example with doubles there's also these things called floats which also hold decimal values they're referred to as floating point numbers but they use 32 bits of precision whereas in doubles have double that precision 64 bits of precision and it's usually better to use doubles overflows because they are more accurate so people tend to use doubles over floats so if you guys are looking for some practice i'll post the source code in the comments down below and why don't you guys fill in some variables here of a card that you would like and post it in the comments down below but yeah that is everything you need to know to get started working with variables in c plus plus hey what's going on everybody it's you bro here and in this video i'm going to teach you guys how we can accept user input using c plus let's get into it computer programs are usually a two-way street they accept user input they do something with that input and they produce output not always but most of the time so up until now we have only been displaying output without accepting any user input but today that's all going to change because i'll show you how what we can do is that we can accept user input and store it within a variable that we can use later on in our program so to begin we'll want to create a variable and for our example let's create a string variable simply named name so we'll say string name and we don't necessarily need to assign this variable a value right away we can simply just declare it without assigning it so effectively this variable will not contain any value until we enter in some user input and that user input will be assigned to this variable later on in this program so next we'll want to create a c out statement that will function as a prompt to let the user know that we want them to enter in their name so we'll use the see out statement and let's create a prompt such as please enter your name all right and this is what you do if you want to accept user input you need to type c in for input and next you need to use the extraction operator which is two right angle brackets and then you're going to put your variable here so in our case it's going to be name so this is what you do to assign user input next we can use this variable elsewhere in our program so now we can display a message using another c out statement that includes our variable so we can say welcome name so let's try this out and see if it works alright so let's run this so here's our prompt please enter your name so here i'm going to type in bro and hit enter and it says welcome bro and that was our user input that we entered in so going back to our code i'd like to show you guys something now with the c out statement you'll notice a pattern we say c out for output then we use the insertion operator and then we write whatever we want to display as output then with input we do something kind of like the opposite we write c in for input then we use the extraction operator which is two right angle brackets instead of two left angle brackets and then we write something to function as input it's kind of like the c out and the c in statements are kind of opposites we have c out and cn output input we have the insertion operator which is two left angle brackets and the extraction operator which is two right angle brackets and then for c out we have the output that we want and for cn the input that we want so you're also not limited to only using strings as variables for example let's create an int which is an integer and we can change this to perhaps age and we can say please enter your age and we will change name to age and for our output we'll say something such as your age is whatever we enter in all right so let's try this out it's going to be the same process basically please enter your age i'll just make up an age and say i'm 18 hit enter and it says your age is 18 so you can enter in whatever sort of data type that you need so one problem that you may encounter in the beginning stages of your programming career is that if somebody enters in input that's not of the type that you anticipate for example for our prompt here it says please enter your age and if i don't enter a number and put a word here such as pizza because that's the first thing that comes to mind and hit enter it's actually going to cause an unexpected result and sometimes cause an exception so later on in a future lesson we're going to cover exception handling where we can prevent people from doing things such as this and take corrective actions so in the meantime you just have to be sure that you're entering in information that is of the correct data type that you're anticipating all right so there's another type of input function that you guys should be aware of and i'll give you guys a scenario so what i'm going to do is reverse all these changes back to when we entered in our name all right so let's run this and i'll show you an example alright so let's say that we enter in a first name and a last name and i'll just enter in a last name that i make up like bro washington and then i'll hit enter alright so you can see that it actually didn't enter in my last name it wasn't stored within that name variable because when we use this cn statement it stops at any blank space c in considers any white spaces as terminating characters so that means we can only use this to assign a single value or word without any white or empty spaces so if we were to rerun this and we entered in this first name and last name all within one word this would still work then but any blank spaces are treated as terminating characters so if you need to accept user input that will ignore spaces there's a different form of user input that we need to use and that is the get line function that will read a line of text for us so in order for us to use this get line function we need to include something at the top of our program so we're going to at the top right include then within a set of angle brackets type in string all right so then what we'll do is we'll replace this c in statement with get line then a set of parenthesis and what we're going to type within here is first cn comma and then the name of the variable in which we want to store something and then type a semicolon at the end then let's rerun this and see what our result is so it says please enter your name so what if we typed in our full name so bro washington hit enter and it now displays our entire full name despite there being a blank space between the first name and the last name so you can use this get line function to read an entire line of text and it really doesn't matter the length i'll just type in something that's insanely long and hit enter and you can see that it read this entire line of text up until we hit enter just so you're aware you can't use the getline function for any data type that is of the integer float or double data type for example if i were to change name to an integer data type this would not work but the same thing would also apply to a hr value i guess it would only apply to string data types because it reads a line of text and basically a string is one or more characters it's basically like a line of text you can think of it that way so it's useful if you need to read a line of text that includes spaces so that's the basics of accepting user input using c plus plus now if you're looking for practice or a project what we're going to do is that we're going to create a program that's going to simulate a cash register where we can ring up an item and we'll actually accept user input we'll ask the user what item they'd like to buy will record a price we'll have the user enter that in as well a quantity and then we'll generate like a final total or bill so let's work on that program let's clear all this out and start over all right so if we want to write this program we're going to want to declare all of our variables right away so what we'll do is create a string and we'll call this item what item do we want the user to buy or what item will they enter in we'll want a price this will be a double value so that's basically a decimal value for a price we'll need a quantity that could be a integer value so we'll say int quantity and lastly a double value which will function as the total which is the total amount that the user has to pay all right so first we'll want to create a prompt that will ask the user what item they want to buy so let's create a prompt using the see out statement and we'll say what item do you want to buy all right then we need to use the get line function for this because the user input might include a space if they're entering in an item name so we'll say get line parentheses we'll type c in comma then item because this is the item name we're going to store the name of an item within here all right next we'll ask for a price so we'll need another prompt we'll say what is the price for each then use whatever type of currency you want i'll use american dollars all right and then we can use simply the c in statement for this we don't need to use getline c and price and we can use the cn statement for double values all right so next we'll use the see out statement to ask for a quantity how many do you want to buy then we'll use c in again and ask for the quantity or store the quantity quantity i can't spell all right and then lastly we need to generate what the total price is going to be but first i'm going to add a end line just to give us some space after generating the total bill so i'll just say c out endl for endline all right so now what we can do to create the total is we can say total equals price times quantity so but make sure you spell it right now for the second part of this program let's create a receipt that will display the results so we'll type this after we calculate the total so let's create a c out statement and we'll write something such as item or you can type in like item name whatever you want doesn't matter too much what you type here so we'll type in item and let's add an endline keyword after this and it's going to be a similar process for the other inputs that we have so price and then price so you don't have to type these strings exactly the way that i do this is just how i would create like some sort of builder receipt but you know do whatever you want here so perhaps for a quantity i'll just put x for like a multiplier of some sort like you bought this at this price times however many you bought so we'll say quantity here then end line all right and then we will say see out your total is then i'll just add the dollar sign or whatever currency you're using you can add that here total e-n-d-l all right it all looks good let's run this all right what item do you want to buy bag of chips potato chips what is the price for each how about one dollar and five cents how many do you want to buy i feel like five bags of potato chips all right item bag of potato chips price 105 times five your total is five dollars and 25 cents i forgot to add the dollar sign for the price but it would still work regardless without it i'm just very particular with small details like that for output but yeah that is some of the basics for accepting user input if you would like a copy of this program i'll post it as source code in the comments down below so feel free to try this out for yourself and if you you know practice with this or generate some output post it in the comments down below as well because i'd like to see what you came up with basically or if you'd like to create your own version of this feel free to post that as well but yeah that is how to accept user input using c plus plus hey what's going on everybody it's youbro here and in this video i'm going to teach you guys all about using arithmetic operators in c plus plus let's get into it all right well in this video i'm going to be discussing five of the basic arithmetic operators that you'll see in c plus plus some rules regarding them and some cool tricks that you can use so just a fair warning that this topic is somewhat dull and boring but i'm going to try my best to keep it fresh and interesting and if you can make it through this lesson i would say that every lesson after this is going to be more interesting so let's begin let's go over the five basic arithmetic operators the first is addition and anytime you want to add something you just use the plus symbol next we have subtraction and use a minus symbol i know i know we've covered this in grade school you know many years ago uh but there's a few other arithmetic operators that you should really be aware of first is multiplication and do you remember back in math class where you would use x to multiply something for example like 5 times 5 is 25 and you use x to multiply well in programming we use the asterisk symbol so use this anytime you want to multiply something next we have division and you use a forward slash anytime you want to divide something and lastly we have this thing called a modulus and it's a percent sign so the modulus is unique it basically what it does is that if you divide two numbers you have a i believe it's called a dividend and a divisor and you use the modulus between these two and instead of giving you the quotient it gives you the remainder but don't fret right now if you don't quite understand what a modulus is i'll give you an example of how it works and some practical applications of us using one how about we go over an example of each of these sounds good right all right let's get started alright so with addition we could say something such as c out and then this is an example of us using addition we could say 5 plus perhaps two then semicolon and this will just display the number seven we're using c out to display whatever the result is which is seven so i'm going to comment this out and then just write this as an example and when we're finished with this i'll post this in the comments down below and pin it to the top if you would like a copy for notes or something all right so an example of us using subtraction c out then we need to use the insertion operator then we could say five minus two and then just display the result and the result is going to be obviously three so let's turn that to a comment all right multiplication we'll say c out and then we could say 5 times 2. so remember we don't use x for multiplication we use the asterisk symbol for multiplication and the result is going to be [Music] 10. so let's turn that into a comment all right so division this is where it gets tricky so c out how about 5 divided by 2. so the answer should be 2.5 right well that is actually not the case the answer that we get is 2 and this is the reason why it's this thing called integer division so if you divide by a whole integer or something of the integer data type it's not going to record that decimal portion and it gets truncated so if we have 5 divided by 2 the result is going to be you know 2.5 obviously but it can't handle that decimal portion so it just drops it or eliminates it and we're stuck with the result of two which is not correct but you know the computer thinks it's correct so one trick that you can do is that you can change this to a decimal value or a double value so if we were to say 5 divided by 2.0 this will actually save the decimal portion and we are ending up the result of 2.5 so make sure you remember that because when you first start writing programs you might not notice that you're using integer division and you might get an unexpected result so be sure that if you're dividing by a certain value make sure that it's a like double value or something with a decimal and there's this concept called casting where you can convert from one data type to another such as an integer to a double type but for now just make sure that if you're dividing and you want to keep the decimal portion for the result make sure that you're dividing by a number with a decimal all right so let's turn that into a comment and move on to modulus oh quick note so i decided to just do a little bit of formatting with these comments i also added the results for each of these operations for our examples just so you can have some notes to look at and remember that i'll post this in the comments down below okay so the modulus the modulus works exactly like division except instead of giving you a quotient as an answer it's going to give you the remainder for example if we said c out 5 modulus 2 instead of displaying 2 or 2.5 if this is a decimal value or double value it's actually going to display the remainder so 5 does not divide by 2 evenly with integer division so it's going to give us a remainder of 1 and this is actually useful if you need to determine if a number is even or odd because if you place the number that you want to examine here then use modulus 2 if it divides evenly by 2 this result is actually going to be 0 so let's try it out so 4 divided by 2 gives us a remainder of 0 and when we get to if statements we could say if the result is equal to zero then this number is even however if this number uh modulus two is equal to one then it's going to be an odd number but i'll show you a program like that maybe in the future for a different example but that's one practical application of how using a modulus would be useful because you can determine if a number is even or odd if you put your number here then use modulus too so let's turn that into a comment and then move on to something else so we'll say result equals one because that's the remainder my next topic for this video we will be discussing the use of the increment and the decrement operators so with programming we do a lot of counting in fact we do so much counting that we have our own operator that's specifically designed for counting up or down by a certain number so let's say that we have an integer variable named total and let's just set this equal to perhaps one all right so let's say that we want to add one to whatever this total is well we would normally have to write a statement such as total equals total plus one and then we can display whatever the new total is so if we were to run this one which is the total plus one equals of course it's going to be two alright so since we do so much counting with programming we have a shorthand way of writing uh statements such as this if we need to increment something by one so instead of saying total equals total plus one what you just do is say total plus plus and this will do the exact same thing we will add one to whatever the total currently is or whatever variable you're working with so this works just the same now the opposite of this is the decrement operator so we could say total minus minus and this is the same as saying total equals total minus one so we could say total minus minus and this will decrement something by one each time we use this so the result here would end up being zero now you can increment or decrement by a given value so with total plus plus this increments by one if you want to increment by let's say two you can do something else you could say total plus equals two and this will count up by two so the result here would be three and if you were to say total minus equals to this will subtract or count down by two and the result here would be negative one so let's add that to our notes the increment operator we can take a value or variable and just say plus plus right after the end and then let's do the same thing for the decrement operator so we'll say x minus minus so use the increment operator if you want to add one to something to some number or variable and then use the decrement operator if you want to subtract one from a value or variable alright well next on my agenda i have to teach you guys about typecasting because i said i would mention that now what i'm going to do is copy this statement here where i say c out 5 divided by 2.0 but let's change that to 2. all right so normally this results in the answer being 2 because with integer division we can't handle that decimal portion so that gets cut off or truncated so we have a few options to work with if we want our answer to retain that decimal portion in the answer when we're using any sort of integer division uh the first is that we would need either our dividend or our divisor to have a decimal portion that can be of the um float data type or the double data type so if we said 5 divided by 2.0 we would end up keeping that decimal portion with our answer here otherwise the dividend can also be a float or a double data type and this would work just the same you can do it either way another option is this concept called casting so you can convert a number that's of one data type to another and what you do before that number is that within parentheses you just write the data type that you want to convert this number or variable into so with this whole integer of 5 we can convert this to let's say a double and if we were to run this the result is going to be 2.5 and we end up keeping that decimal portion now check this out what if we put our dividend and our divisor within its own set of parentheses around this part of the equation and then we cast this afterwards well what's going to happen is that it's actually going to take 5 divided by 2 first and then it's going to truncate the decimal and then cast it to a double and we still lose that decimal part of this answer likewise you can take a result that's normally a double and you can cast it into an integer data type intentionally so you can purposefully cut off the decimal portion so an example of this is that we'll say c out perhaps 5.051 divided by 2.021 i'm just making up numbers here uh let's comment this out so normally this dividend divided by this divisor equals 2.49926 all right let's say that we want to cut off the decimal portion or truncate it well what we'll do is that we'll put this within parentheses and we're going to take the result and cast this of the integer data type so our result is that it's going to be just 2 then before we move on i'm just going to add a note that says cast to a double cast to an end all right so we finally made it to the final topic of this video and we need to cover operator precedence so do you guys remember in grade school where if we have a multi-part equation such as this with multiple different arithmetic operators each of these operators has a certain precedence one over another and what we learned in grade school was this acronym it was pemdas and one way of remembering this which was kind of silly is that we would say please excuse my dear aunt sally or something very similar to that that used the same letters for this acronym and what this meant is that we would solve anything within parentheses first followed by exponents multiplication division addition and then lastly subtraction so if we were to solve this using uh this order of operations pemdas we would solve let's see 2 times 3 first because any multiplication takes precedence over addition so two times three is six plus one the result is going to be seven then so we have a different set of rules for programming and it's actually a little bit more simple i would say than pemdas so with programming we solve anything within parentheses first for step one step two we do any multiplication or any division it doesn't matter which one comes first we always start from the left and work to the right and then step three we do any addition or subtraction at the very end all right so here's an example so if you want why don't you pause the video and see if you can guess what the total is going to be and i'll just walk you through these steps using our order of operations for programming so step one we have to solve anything within parentheses first and we start from the left and work our way to the right so the only place that i see parentheses is at the very end so we're going to solve everything within here first so we're going to say 6 plus 7 first which is 13 all right so we can get rid of these parentheses then because they're no longer relevant step 2 we need to do any multiplication and division first so we start from the left and work our way to the right i see 3 times 4 so we need to solve that first all right so then we have 12 divided by 5.0 and i changed this from 5 to 5.0 because of integer division so this is going to be 12 divided by 5 is 2.4 so let's change that all right so there is no more multiplication or division so we do any addition or subtraction so 1 plus 2 is 3. 3 minus 2.4 is 0.6 so 0.6 plus 13 is 13.6 all right so let's go back to see if that actually was the answer all right so let's run this we probably need to display what the total is going to be and the answer is 13.6 well good job make it to the end i knew this was a somewhat more dull topic but it was necessary for us to nail down some of these basics and if you want a copy of what we did here today i'm going to post it as a comment down below and pin it to the top but yeah that is everything you need to know to get started working with just basic arithmetic in c plus hey what's going on everybody it's your bro here and in this video i'm going to teach you guys how we can use if statements in c plus let's get into it if statements are pretty sweet because it's like a basic form of decision making we can specify some condition and then if our condition that we specify is true we can have our program do something if that condition is not true we can skip that or have a program do something else so in c plus this is what you do to create an if statement you just type in if then you need a set of parentheses and then a set of curly brackets so anything within the parentheses will be our condition it's like the rule we're going to do whatever is within the subset of code between these two curly brackets if this condition that we specify is true so there's many different things we can use as a condition but i think we'll just start with something simple let's say that we want to check to see if somebody's age is equal to 18 first let's create a variable we could say int age equals 18. and then within the parentheses of the if statement we can check to see if age is equal to 18. honestly we can really do anything with the statements we can check just about anything um but let's check to see if age is equal to 18 now if you're checking to see if one value is equal to another you don't just use a single equal sign that is the assignment operator so if you were to say age equals 18 well c plus plus thinks you're attempting to assign the value of 18 to the variable age so if you want to compare if two values are equal you need to use two equal signs and this is also known as the equals to comparison operator so basically if our age is equal to 18 we're going to do whatever is within the subset of code let's place a c out statement within these two curly brackets so we can just verify that the if statement actually worked so we'll say c out then use the insertion operator then let's display a message congrats you are 18 years old i'll just say y-o for years old yo all right so let's try this out and our result is congrats you are 18 years old so what if we changed age to a different number like 17 well let's see what happens when we try this so it doesn't appear to do anything and this is the reason why within our if statement within the condition of the if statement we're checking to see if age is equal to 18 since our age does not equal 18 it's 17 this is going to evaluate as false and it's going to skip over anything within your subset of code there's other comparison operators that you should also be aware of too so this is the equals to comparison operator it compares to see if two values are equal if you use an exclamation point then an equal sign this is the not equals comparison operator this will check to see if two values are not equal if both of these values are not equal then this entire condition will be true then so if we check to see if age does not equal 18 it will execute whatever is within our if statement so a more appropriate c out message would be something such as you are not 18 years old so let's try this out and see if a message displays this time yeah and it says you are not 18 years old and if we were to go back and change our variable age to 18 and then re-run this program well we don't get any result this time because we're checking to see if our age does not equal 18 currently it does equal 18 so this entire condition will become or evaluate as false so then if this is false we will skip whatever is within the subset of code for this if statement and we continue on like it never even happened there's other comparison operators too these you'll probably be a little more familiar with we can say things such as greater than or less than less than or equal to greater than or equal to so i'm thinking what we'll do for this we'll change our if statement to say if our age is greater than or equal to 18 we'll display a different message congrats you are an adult so we can basically change our age to anything our age variable that is let's say that we are 90 years old and we're going to check to see if our age is greater than or equal to 18 and it says congrats you are an adult well you know we're 90 years old so we've been an adult for a long time uh but yeah you can check to see if like numbers are within a certain range too and stuff and if we were to say our age is like perhaps 12 you know then run this well it's going to skip over this if statement because our age is less than 18 it's not greater than or equal to 18. let's move on to phase two of if statements so there's this thing called an else statement if our condition turns out to be false normally we'll just skip over whatever is within the subset of code for the if statement we can use an else statement if we want our program to do something else so after your if statement just type in e-l-s-e for else and then it has its own block of code then so basically our if statement is going to check to see if somebody is greater than or equal to 18 years old if it is or if that's true we will do this if not we will do something else so let's display a different message let's make a see out statement that says you are not an adult okay let's test this theory so our age is currently 12 so it says you are not an adult and remember before when we didn't have this else statement it just skipped over the if statement completely but if you have an else statement it's going to instead do whatever is within the code block of the else statement so it's like you're at a fork in the road and your program's going to decide which path it's going to take so i'll give you a visual example of this so here's a visual if this helps you understand it i actually copied this from a few other lessons on the same topic that i made in other programming languages so don't tell anybody but basically it works just the same so we have our variable age and when we reach our if statement if our age is greater than or equal to 18 we're going to go down this path and do something else with our program if our if statement is false we are going to use this else statement and our program is going to take this path and display a different message so this is like a visual representation if that helps you so with that being said i'd like to introduce a variation of the same program or something similar to it now with this if statement this condition will evaluate or return a answer of being true or false so what if you had a boolean variable we could create a boolean variable such as adult boolean adult equals and we can assign this value of true or perhaps pulse and then instead of having a condition such as a just greater than or equal to 18 you could just put the boolean variable name within here and it's kind of like the same thing it's going to check to see if this evaluates as true or false if adult evaluates to be true then we will display congrats you are an adult if adult evaluates as false then we will do whatever is within this else statement and since our boolean variable is set to false it displays you are not an adult but then if we were to set this to true then it's going to perform whatever is within the if statement then where it says congrats you are an adult so that's an alternative way to write something within an if statement because basically it's going to return a result of being true or false and if you were just to stick a boolean variable in here you can check to see if this variable contains the value of true or false let's move on to phase three on this topic of if statements and we're going to cover the else if statement so i think this is best if we were to create a entirely new program so what i think we'll do is that we will prompt the user to enter in a temperature and if the temperature falls within a certain range we will display a certain message depending on what the user typed in for a temperature so to begin let's create a variable perhaps an int called temperature and we can just declare this we don't need to necessarily assign a value right away and let's create a prompt to ask for a temperature and we will say please enter the temperature perhaps we'll put this in celsius then we need to place a cn statement after this we will say temperature all right so this will prompt for a temperature and depending on what the value of temperature is we will display a particular message so let's write an if statement if i'm just going to copy temperature because it's not that fun to type this out if temperature is greater than perhaps 30 degrees celsius we will display a message so we'll say c out it is hot outside maybe i'll add an emoji here it's not required but i think it's silly it's a dude's sweating all right so if our temperature is greater than 30 we will display a message it is hot outside so what if we want to check something else too uh before actually using an else statement well we can use an else if statement it's kind of like if our if statement turns out to be false we can check one more thing or a few more things before we result to the else statement so let me give you an example let's check to see if our temperature is less than zero so i think that's the freezing point in celsius let's display a message that says see out it is cold outside for then let's add an else statement actually you know what i think i'm going to change this to greater than or equal to 30 and less than or equal to zero all right so let's add an else statement so basically if our temperature is not greater than 30 greater than or equal to 30 and our temperature is not less than or equal to zero it's going to fall within a certain range of 1 to 29 degrees celsius so anything that falls within that range we could say it is perhaps warm outside it is warm outside okay let's test out this program okay please enter the temperature it is 50 degrees celsius outside it is hot outside let's try a different answer please enter the temperature perhaps negative 20 degrees celsius it is cold outside now if our temperature falls within a certain range that's basically how we set this up it's going to just rely on the l statement because it's checking one if statement after another first it checks to see if our temperature is greater than or equal to 30 and if it's false we're going to use the elsif statement and it's going to check to see if our temperature is less than or equal to zero if that's also false then we resort to the else statement and that's kind of like a last resort so if it's not hot outside and it's not cold outside then it must be warm outside so let's try this one last time just to verify if that's true so please enter the temperature perhaps 25 degrees celsius it is warm outside well that's the fundamentals of if statements it's a basic form of decision making we can check to see if some condition that we specify is true then we can have our program do something if it's false we can have our program do nothing or something else entirely i'll include copies of the programs that we made in this video and i'll post it as a comment down below and pin it to the top but yeah that is everything you need to know to get started using if statements in c plus plus hey what's going on everybody it's your bro here and in this video i'm going to show you how switches work in c plus plus let's get into it a switch is basically a bunch of consecutive if statements however using a switch is much more efficient you could say that it's harder better faster stronger so here's an example of when we might want to use a switch in place of a bunch of if statements let's write a program where we can enter a number between 1 and 12 and it will give us a corresponding month depending on the number that we type in so before we get this started let's just include one thing we're going to type include then a set of angle brackets and then we're going to type in string all right let's create a integer named month and we will not assign this value right away we'll just declare it and let's create a prompt to ask for a month enter the month then we'll put the numbers 1 through 12 here i think i'm going to get rid of that colon all right and then we're going to use c in month i finished typing everything so here's what you don't want to do writing an if statement and then writing an if statement or an else if statement right after and it's going to check if month is equal to 1 2 3 so on and so forth all the way till 12. so if a veteran programmer sees you do something like this where you write a bunch of consecutive if statements they're probably going to call you a noob and there's a better option and that's by using this thing called a switch this is how you write one we're going to type switch then we need an expression or we can stick a variable within here so we want to check the month and then we need a set of curly brackets in place of if statements we use cases and we're going to check if our value is equal to some case and the first thing we'll want to check if our case is equal to 1 so we're going to type in case 1 colon and then on the next line we're going to type c out it is january and then this step is important too we're going to type break so if this case matches what we want to evaluate or check we're going to do this and then break out of the switch and we don't need to check anything else so we're going to do the same thing for case 2. so case 2 then we want our program to do this display it is february and then break all right but you get the idea i'm going to repeat this process for cases 3 through 12 so i'll just fast forward this video all right it's done so we have cases one through twelve one corresponding to each month of the year so let's test out this program enter the month well currently it is april so i'm going to type four and hit enter so 4 enter and it says it is april let's try a different month i'm going to type in 12 hit enter and it says it is december so we know the switch is working what happens if we type in a number that doesn't match any of these cases well let's see what happens so it says enter the month i'm going to type in the word pizza so nothing appears to happen so what we can do is that we can place a default case what do we want our switch to do by default in case there are no matching cases so we're going to write default at the end and what do we want our program to do so first make sure you write default and then add a colon at the end and let's simply display a message that says you did not enter a number between 1 and 12. enter the month it is pizza month you did not enter a number between 1 and 12. so it's similar to an else statement where it's like a last resort if there are no matching cases we'll have our program do this by default so how this works is that the switch expression is evaluated at once and selects one of these many code blocks depending if there's a matching case or not if there's no matching case we can execute whatever is within the default code block now with these break keywords these are important to have after each case and if you were to take these out we would continue on to the next case as if the next case was also true so let's try this program again and take out a few of these breaks and see what happens enter the month it is april so i'm going to type in the number four and hit enter and our result is it is april it is may it is june it is july it is august so if you're missing a break within a case it's going to continue on to the next case as if that next case was also true and it's not going to stop until it reaches a break and these cases don't necessarily have to be numbers let's try another example using char values characters i have an idea let's write a program that will ask for a letter grade like in school the letters a through f and we will give a response based on what letter grade that we answer with so to begin let's create a variable that will store a single character a char value and we will say answer and we don't need to assign this a value right away next we will say c out this will function as a prompt what letter grade did you get and perhaps i'll just put a through f in here and then we will say cn answer let's create a switch so switch then this part's the expression we'll just put our answer within here and we will check this answer against many different cases in our first case instead of one we're going to type in a within single quotes so if our answer matches this case where it's a the character a what do we want our program to do perhaps a simple message such as you did perfect and then we want to break all right let's repeat the same process for the other letter grades so case b what do we want our program to do if this case matches you did good and then break and it's just a matter of repeating the same process for each of these letter grades so case c c out you did okay and break case d c out you did not do so good it's still passing though and case f c out you failed and break we can add a default code block so let's do that we're going to type in default and we will display a message enter a letter grade a through f i guess we're missing e but who cares let's run this program and see if it actually works what letter grade did you get i got an a so i typed in a hit enter you did perfect all right we got an a everybody what letter grade did you get i got enough you failed have a nice day all right then let's type in something that's not a letter grade like the word pizza and we get that default message of enter a letter grade a through f so we know the switch is working but yeah you're not limited to just using numbers for your cases you can also use characters and other things too so just to summarize what we did here today a switch is more efficient than using a bunch of consecutive if statements we can take a value or expression and compare it to see if it's equal against many cases at once if there is a matching case we can activate the corresponding code block and have our program do something that we specify and then we can use the break keyword to break out of the switch and continue on with the rest of the program and we can also add a default code block if there are no matching cases we can have our program do something by default but yeah that's the basics if you want a copy of the examples that we worked on today i'll post it in the comments down below and pin it to the top and if you're looking for practice or a project to work on why don't you create your own switch and then post it in the comments down below but yeah that's the basics of how switches work in c plus plus how's it going everybody it's bro here i hope you're doing well and in this video i'm going to teach you some useful functions related to strings in c plus plus let's get into it this video is going to be more of a showcase of the different string functions that are available to us so we're going to be covering a lot of them if there's any that you want to see in particular look in the description for any timestamps and you can hop around if you want we're going to kick this off with the string.length function this returns the length of a string that you specify so let me show you how this works then we're going to create a string named perhaps first name and we're not going to assign this quite yet because we're going to accept some user input now we're going to see out a prompt enter your first name and then we're going to use get line c in comma first underscore name we can use the length function to determine how long our name is and we can actually store this as an integer variable or we don't have to but i'll show you a good use so length equals whatever string that we want to determine what the length is so first name dot length and then a set of parentheses so you'll notice with a lot of these functions that they will end with a set of parentheses so pay attention to that all right so we have our length now what well i'll show you a good use then we can check to see if somebody's name is too long like if they create a username we can determine if it's too long or not we can have them choose something else so perhaps if do this within parentheses length is greater than or equal to 12 then what we will do is display a message your name can't be over 12 characters long okay else c out welcome then we'll display your first name all right let's try this enter your first name i'll just type in bro welcome bro alright so that worked just fine but i just want to change something real quick because it's bothering me i just want to give a space when you enter your name in okay let's assume that we have a very long username so let's try this again then bro my username for other things is usually brometheus so let's use that prometheus um four two zero six 9 wtf bbq that looks like a good username to me your name can't be over 12 characters long so that's a good use of the length function oh and another thing with the length function and actually many functions is that you don't necessarily need to assign this to a variable we could just take this delete it and then place this within our if statement so this would actually work just the same within less steps even though it's a little more complex i would say but it takes up less lines of code so let's try it so bro one two three four five six seven eight nine ten let's move on to the empty function so you can use a strings empty function to determine if any characters were placed within that string and a good use of this is that if you prompt for a name and somebody just skips it by hitting enter we can actually determine if that string is actually empty then so let's try this then we'll put this within our if statement first underscore name and we're going to use the dot empty function and then we need a set of parentheses so if the first name is md it's going to execute our if statement if not it's going to use the else statement so we can create a prompt such as you didn't enter a name okay let's see if this actually works then enter your first name i'm just going to hit enter you didn't enter a name then let's try it again but put a name in so bro welcome bro the clear function will clear the contents of a string so it's useful if you need to reset something so in this little program i wrote we just have a basic welcome message that displays welcome and then your first name or username whatever you enter it'll say that your name has been cleared we'll actually clear it by typing in the string that we want to clear so first underscore name dot clear parentheses then a semicolon and then we will display that welcome message one more time then so let's see if this works enter your first name bro welcome bro your name has been cleared and then it says welcome and then nothing after that because our first name string is now cleared or emptied so that's useful if you need to like clear out a form or some user input just use the clear function the append function will allow you to add one string onto another so a good use for this is that imagine that you are prompted for a username and then we can actually append an email extension to the end of that to create an email address so let's create a string called email and this will hold the newly appended string so we will append our username so user name so this is the string that's getting appended to dot then append parentheses semicolon make sure it's outside the parentheses though all right so we can put a string in here to append to this username our first string so let's add a gmail.com email address and then we're just going to display this okay enter your username i'll type in bro your email is now bro gmail.com i'm pretty sure that's taken the at function will return a character at a given index that you specify so we're going to write a program that will extract the initials from a first name a middle name and a last name so let's create a char variable so this returns a character value so we'll call this char value perhaps letter one equals then your first underscore name then at then next we need to place a position of which letter we want to check or extract if you want the very first character or a letter just put in zero because it's a computer science thing like all arrays basically or strings start at index zero alright so then let's do the same thing for the middle name and the last name so let's call this letter two middle name letter three last name and then let's display this c out your initials are then we will display letter one letter two and letter three okay enter your first name bro and to your middle name i'm going to make up something max like maximum and last name washington your initials are bmw and if we change the index for this at function we would actually extract a different letter so let's see what our initials are now so bro max washington my initials are raw because it's the technically the second character within each of these string variables now so that's how you can check to see which character is at a certain position in a string and then this function will return that as a character that you can use for other things the substring function will return a portion of a string and it's called a sub string so an example of this let's create a program that you'll enter in a first name and then we'll take a small sample of that and create a nickname for you so let's create a string named nick name let's say that we want to take the first three letters of your name and create a substring out of it called nickname so we'll type in the string that we want to use so first underscore name dot sub str for sub string then a set of parenthesis semicolon okay so there's two things that we're going to want to fill out the first is the starting position so let's say that we want to start the substring at the first character of your first name we're going to put zero here let's say that we want to retrieve the first three letters perhaps so we'll put three here if you want four letters then just put four so let's stick with three then and then we're just going to display this nickname let's try this enter your first name let's type in my full username of brometheus and it says your nickname is bro hello bro okay so we can actually change where we want to take our sample for our substring let's say that we want to move this to position three and we want to extend this for five characters now so we're going to take another sample of our first name our string and we'll get a different substring then so prometheus i don't know what this is going to be your nickname is meth like crystal meth i guess the insert function is very similar to the append function but we can take a string and then place it anywhere within another string we just have to give the position in which we want to insert this other string so this actually doesn't return anything and this insert function doesn't actually return anything it currently edits the string that you're currently working on so let's take our username dot then insert set of parentheses so there's two fields to fill out the first is the position in which you want to insert something so let's say that we want to insert a string or a character at the very first position so we're going to put 0 comma then what we want to insert let's say that we want to insert the at symbol like we're replying to somebody so we'll just put this uh index at zero then and then we'll insert this at sign this string so let's try this enter your username bro code and it inserted a at symbol uh before our actual username at index zero so let's place this somewhere else how about at position three so let's try this again bro code and to place this at symbol at position three within our string so it's kind of like we pushed these two parts of our string to the side and it inserted this right in the middle this find function is very useful because you can search for something within another string and it will return the first instance of that substring that you specify so let's create a program where we have somebody enter in their first name or a username and then we can search for a certain character or a string and return where it found that certain character so this is kind of like you're on a webpage and then you can use that find feature to find some sort of text on the webpage and it's going to return where it found that first instance of that certain text that you specified so it's the same concept really so this will return a integer value and we'll call this position equals we want to search our first string uh that was called first underscore name dot find and what we want to find we'll find a substring our variable substring all right so let's run this so we can actually decide on what we want to search for with this enter your first name i'm just going to type in bro code which uh search for which character i want to find any c's in here capital cs it found this character at position three so zero one two three that's kinda cool right so this is the erase function we can erase a portion of a string one use for this so do you know with phone numbers how they usually begin with an area code well let's prompt for the user to enter in a phone number and then we'll erase the area code leaving whatever the rest is so we'll use the string phone number dot erase then we need to place a starting position a starting index let's say we want to erase the first four characters so it's going to start at position zero and we're going to erase four characters from the string okay and then we'll display it at the end okay enter your phone number i'm just going to make up something with an area code so perhaps 1 0 1 1 2 3 four five six seven okay our new phone number is one two three four five six seven and it erased the first four characters of our phone number so it erased that area code basically so we're about out of time there's still many more functions that we haven't covered but if we did we'd probably be here all day so instead i'll link a resource to basically a web page that contains more functions that you may or may not be interested in but there's still a lot more that we haven't covered these are just a few and then if you have some free time feel free to browse the resource i'll give you if you want to learn a few more i'll post the source code that we worked on in the comments down below and pin it to the top but yeah that's the basics of using string functions in c plus plus hey what's going on everybody it's bro here again and in this video we're going to be covering some useful math functions in c plus let's get into it i promise that this video won't be as boring as the last one on string functions it's probably going to be even worse no it won't be that bad but here's a few math functions that you might find useful for your programs first thing that we're going to want to do is include something at the top of our program so outside of your main method we're going to type this hashtag include then within angle brackets i'll go rhythm but make sure you spell it right just like that not all functions that we'll be covering will need this but a few will and i might forget to mention this later on in this video and it would be a pain in the butt to have to re-edit this and show you the part of the video where i include this so let's just get this out of the way and include this now so this video is going to be more of a showcase of different math functions that we can use in c plus plus and if you want to skip around in this video that's completely fine i'll include some time stamps in the description if there's any math functions that you'd be interested in the first math function will cover is the max function we can find the higher of two numbers and return the higher number so let's write a program that will ask the user to enter in two numbers and the program's going to return the higher number so let's create two double variables so these can store a decimal portion it doesn't need to be a whole integer when we write this program so double x and double y we're not going to assign these a value quite yet and then let's create a prompt perhaps this program will enter the max of two numbers all right and then we are going to create a prompt to enter in number one enter in number one and then c in x for our first number and we'll do the same thing for y but we're going to change a few things enter in number two and we're going to change x to y and we can place the result within another variable if we so choose like double z so if we want to find the max of two numbers we're going to use the max function so type in max and then a set of parentheses and we're going to put two numbers within here and we can just put the variables x and y and then let us display the result so we'll just c out z okay everything looks good let's run this false alarm i have to make a few edits so i forgot to end the line and i'm just going to add a space after this colon space all right it should look better now all right this program will enter the max of two numbers enter in number one how about the number 24 enter in number two you know it's better than 24 25 and the max number is 25 all right now with the minimum function it's going to be the same process as before but we're just going to change a few things we're going to change max to min within our little message here and we're going to switch max to min so this will find the minimum between two numbers so let's run the same process that we did last time 24 and 25 and the minimum is obviously 24 big surprise there right the power function man that's a pretty cool name power function it returns a number to the power of another number so in our message what we'll say is this program will return x to the power of y all right so enter in number one that's right entering number two now we're going to use the pow function that also kind of reminds me of like those old comic books like batman where they had like sound effects for all their kicks and attacks uh but i'm getting off track again okay let's run this pow pow bam all right this program will return x to the power of y enter in number one perhaps three enter in number two let's say four so the result is going to be eighty-one and that sounds right three to the power of four is eighty-one let's talk about square roots so the square root function will return the square root of a variable or number that you place within the function for this demonstration we will no longer need the y variable and we can get rid of this prompt all right so this program will return square root of a number all right so enter in the number okay so then what we're going to do here is just type in s q r t for square root and then a set of parenthesis and we're going to place the number or variable that we want to find the square root of within the parentheses here and then this variable z will store it and we can display this all right let's run this all right enter in the number to find the square root of perhaps 64. that's a nice number and the square root of 64 is 8. the absolute value function will return the absolute value of a number that's basically its magnitude think of it like how far away the number is from zero is going to return a positive number so let's change our message to this program will return the absolute value of a number enter in the number that's good all right what we're going to do here is change this sqrt to abs like abs six pack abs but it's short for absolute value all right let's run this this program will return the absolute value of a number enter in the number so let's say negative 42 so this will return the magnitude of the number basically how far it is away from zero and it's always a positive number so the answer is 42. the round function will round a number to the nearest whole integer so what we'll change here is this program will round a number to the nearest whole integer all right enter in the number and what we're going to do here is type in round parenthesis and then place our number within here so x all right this program will round a number to the nearest whole integer enter in the number let's type in the number 3.14 the first few digits of pi and the result is three so the nearest whole integer to 3.14 in regards to the decimal portion is 3. now what if we were to type in a different number like 3.5 it's actually going to round it up as long as the decimal portion is at least 0.5 so there's a few other round methods too you might be interested in the ceiling function will always round a number up to the nearest whole integer so this program will round a number up i don't feel like typing that much and what we're going to type in here for the ceiling function is it's shorthand c e i l for ceiling like you're touching the roof the ceiling so let's type in 3.14 again and see what the result is so it's always going to round it up if there's at at least a decimal portion if this was like 3.0 this would stay the same as 3 but it's always going to round it up and the opposite of this function is the floor function if there's a ceiling there's a floor then so this program will round a number down to the nearest whole integer and we're going to replace this c e i l with floor like the floor of your house and we'll run this okay so let's type in a number with a very high decimal portion like three point nine nine n n nine and this rounded down to the nearest whole integer which was three so those are just a few of the math functions available to you we only went over a few of the basics there's still way more out there i'll post a resource that you can go to if you're interested in more math functions but these were just the basics i'll also post the code that we worked on in the comments down below and pin it to the top if you would like a copy but yeah those are some basic math functions in z plus plus hey it's your bro hope you're doing well and in this video we're going to be discussing the and as well as the or logical operators in c plus plus let's get into it logical operators allow a program to make a decision based on multiple conditions so for example we can create an if statement with more than one condition and for this video we'll be talking about the and as well as the or logical operators let's begin with the and logical operator we can check to see if one condition and some other condition is true if both conditions are true as in one and the other one is true then the entire expression is true if just one of them is false then the entire expression is false so let's create a program where this might be useful then let's create a program that will ask for somebody's temperature and if it falls within a certain range we will display a message so let's create a double value so we can store a decimal portion depending on what temperature they type in and let's just create a variable named temp short for temperature and we'll create a prompt to ask for the user's temperature enter your temperature and perhaps we'll put this in celsius and then we'll want to assign this variable temp all right so then let's create an if statement and this will display a message if it falls within a certain range all right so let's begin with if our temperature is greater than or equal to 35 degrees celsius then we'll display a message such as your temperature is normal all right but what if somebody's temperature is like 200 degrees celsius that's obviously not normal so we would want this to fall within a certain range so we can use the and logical operator to check one more condition so we can say if temperature is greater than or equal to 35 and temp is perhaps less than or equal to 37.5 then we will display this message else we can do something else such as display a message like go to the hospital but i spelt it wrong okay let's run this enter your temperature i have a temperature of 150 degrees celsius and it says go to the hospital because that is obviously not normal so if we want this if statement to execute it needs to fall within this certain range this condition has to be true as well as this condition has to be true since we said our temperature is 150 degrees celsius this portion is true it evaluates as true but this portion evaluated as false because 150 is not less than or equal to 37.5 so in order for us to execute this if statement it has to have both portions of this expression become true so let's try it again but we'll have it fall within this range then so perhaps my temperature is 36 degrees celsius your temperature is normal and with these and logical operators you can add more than one for example we can put this portion within its own set of parentheses and we can check something else such as temp does not equal zero i guess it wouldn't really make sense for this example since we're already checking to see if temperature is less than 37.5 um but this is one way that you can check a total of three things or maybe even four things if you add another and logical operator but that's basically how the and logical operator works you can check to see if one condition and some other condition is true if just one of them is false then the entire if statement or entire expression is false then let's clear what we have and move on to the or logical operator so the or logical operator is somewhat similar to the analogical operator but it will check at least two conditions if one of the conditions is true and the other is false then the entire expression is true the only way that the entire expression will evaluate as false is if both conditions are false then so let's create another similar example it's going to be a program that will ask if you have a cough so let's write a perhaps let's declare a string variable named cough all right and we will ask for a prompt do you have a cough then we'll write within parentheses like the answer that we expect like yes or no all right and then we will assign the answer to our variable cough which can hold a string but i'm forgetting a semicolon okay then let's write an if statement if and i think the best way to handle this that uses an orological operator is that we can say if our string cough and we're going to use the at method and we're going to check to see if the character at position 0 index zero of our string cough is equal to the character perhaps y all right so that means somebody's responding with yes or maybe a yeah and then we will say see out you are sick and then let's write an else statement else you are probably fine all right so here's a scenario what if somebody types in a capital y for when they type in the word yes let's try it all right so maybe for this first run let's just type in yes just to be sure that this is working all right do you have a cough yes you are sick now let's type that in again with a capital y yes all caps you are probably fine but that's not what we expected so why don't we check to see if some other condition is true within our if statement and we can actually use the or logical operator which is two of these vertical bars and we will also check to see if cough at index zero equals the capital y a capital y so as long as one of these conditions is true then this entire expression is going to evaluate as true even if one of these conditions was false as long as one of them is true then this entire expression is going to be true then and we will execute whatever is within the code block of this if statement so let's try this one more time do you have a cough then we'll just type in all caps yes you are sick which is what we wanted then so then just like the and logical operator we can check to see if another condition is also true or not so you can just add another set of these vertical bars if there's one more thing you want to check so you're not limited to just two surprise we're going to be talking about one more topic and we've discussed it previously in another lesson and that is the not logical operator and basically what this operator does is that it will inverse an operand it will reverse the true or false outcome of an expression and i know we've talked about this previously but it would be a good point to touch base with this just because this is technically a logical operator and if there's anybody new to this channel and they didn't see the previous videos uh they'll actually be informed about this now so let's just touch base with this real quick so let's create a program that will ask for a user's name and depending on what they type in it will either display a welcome message or it will display a message such as you did not enter your name if somebody just skips the prompt so let's create a string variable named name all right and then we'll create a prompt to ask the user for a name so c out enter your name and then we will use the getline function and within here we'll type in c in name so this is in case somebody types in a first name and a last name it will actually assign the entire line of input all right so we'll use an if statement and then within the if statement we'll check to see if our string name is empty and we'll use the empty method that we learned about so we'll just type in name dot empty so if the name is empty they must have skipped it and we will display a message such as perhaps we'll yell at the user you did not enter your name brownie face alright so then if the name is not empty then they must have typed in something so we'll display a message like a welcome message that says welcome whatever the user's name is all right so let's try this and then after this run we'll try and use the not logical operator in action so enter your name i'm just going to hit enter to skip you did not enter your name all right with this program there's another way to write this and we can use the not logical operator so this will basically inverse the operand if it's false it's going to flip it to true if it's true it's going to flip it to false so if we place this not logical operator before this name dot empty method if the name is empty and this pops up is true this is going to flip it to false and the inverse of that too so if basically our name is not empty we can actually display this welcome message so if the name is not empty we will display our welcome message else the name must be empty then so we will have our message that says you did not enter your name let's try this and see if it works just like before so enter your name i'm just going to hit enter you did not enter your name let's try it one more time but let me fix some syntax here all right enter your name this time i will type in my name and just type in bro and it says welcome bro well those are three logical operators that you would find useful the and the or and the not logical operators i'll post the code that we worked on in the comments down below and pin it to the top if you want to take a look or you'd like a copy for yourself but yeah those are how logical operators work in c plus plus hey how's it going everybody it's bro here hope you're doing well and in this video we're going to be discussing while loops in c plus let's get into it so we're going to be talking about while loops all while loops are is that we can execute some code repeatedly while some condition remains true this is how you write a while loop we're just going to type in while then we need a set of parentheses and then a set of curly brackets now within the parentheses this is our condition as long as some condition within here is true we will execute whatever code is between these curly brackets over and over and over again but it's also important to add a way to exit the while loop otherwise you're stuck in what's called an infinite loop so here's an example of an infinite loop and this is what you don't want to do all right so we're going to set the condition while one is equal to one we'll display something maybe a message so c out help i'm trapped in an infinite loop and then let's add an end line after this all right well let's see what happens let's run the program all right as you can see it's just repeating this code that we have within the while loop over and over and over again and it's not stopping because we don't have any way to exit this while loop so it's important to add a way to exit your while loop so you can continue on with the rest of the program as fun as this is it's not practical now i'll demonstrate a good example of us using a while loop that's actually practical this time for example we could prompt someone to enter their name and we can keep on asking them if they don't enter in their name if they try and skip it so it's a great way to force the user to enter in something maybe some information because we won't let them continue with the rest of the program until they do something or enter that information in so let's create an example of that where we're prompting a user to enter their name in repeatedly until they actually do so let's create a string variable named name and we're not going to assign this right away so this is the syntax for a while loop again we're just going to type while we're going to put our condition within a set of parentheses and then all of the subset of code for the while loop is going to be between two curly brackets so what we're going to check within the while loop this will be the condition perhaps if name is empty so we're going to use the string empty function and it will check to see if our name is empty so if this condition remains true what are we going to do let's create a prompt such as please enter your name and we'll add an endline after this and then we will get line c in the user's name whatever they type in and then once we exit this while loop we'll display a welcome message so see out welcome then the user's name all right let's try this all right please enter your name i'm just going to hit enter to skip this part and it's having us enter our name again no no no all right so then it's just going to continue on and on until i actually enter in something so now if i want to exit this while loop i'll have to type in a name and i can't continue on with the rest of the program until i do so i'm just going to type in bro hit enter and we escaped the while loop and it displays our welcome message and it says welcome bro so that's a good example of us using a while loop that has some practical uses one thing to pay attention to with while loops is that if our condition is false from the beginning we will actually skip this while loop and completely ignore it and continue on with the rest of the program so an example of this let's say for our name let's assign this value and i'll just type in an empty space alright so what would happen if we assign this value and this condition within our while loop is false from the get-go from the beginning well this is what happens then we just skip it completely so in order to even enter the while loop the condition has to be true then if it's not true when we reach the while loop in our program it will skip it completely if it's false from the beginning now there's actually a variant of the while loop that solves this problem for us and it's called a do while loop we will always perform this subset of code at least once but then only continue if the condition is true then and i'll give you guys an example of a do while loop so let's clear everything we have here and maybe i'll just make a note that this is a do while loop there's different spellings of this but it's essentially the same thing um so a good example of this do you remember from old video games like oregon trail where you had to press q to quit the game well let's create an example of something like that involving a do a while loop so while we don't press the q button or q key or enter in queue we will continue to play this game that we make up so let's create a string variable named answer and we're not going to assign this right away now we're going to use the do while loop so we're just going to type in do then a set of curly brackets and then after this we're going to type in our while loop all right and then you have to finish that off with the semicolon because i'm forgetting that so we're going to put our condition between these parentheses at the very end so we will always execute this loop at least once and then at the end we're going to check to see if this condition is true if it is then we'll continue with this do while loop so let's create a fictional game then it's going to have really very poor graphics all right so you are playing the game it's just gonna display something really and let's end line let's make this fun pew pew and then i'll ask for a prompt if the user wants to exit uh perhaps i'll just type in or add a prompt that says maybe press to quit and then let's add a get line here so get line c in and then the answer and i'll just add one more line of text here just to kind of keep things organized and separated all right so we're going to execute this do while loop at least once and then depending on our answer this will determine if we're going to exit the do while loop so we'll need to write some sort of condition within the while loop so we can check to see if our answer is q or not and one way to do this is that we could say answer dot at index zero all right does not equal q alright so this will take whatever we type in and if it begins with the letter q then we will exit the while loop then as long as it doesn't equal q however if they type in capital q we'll also have to account for that too so we're going to use the and logical operator answer at index 0 does not equal uppercase q all right so let's run this oh then we should probably write a prompt to let the user know that they have exited the game so you have quit the game okay we should be good so we're going to perform this code block at least once and then depending on our answer that will determine if we exit this game or not so let's try this you are playing the game pew pew press q to quit um i don't want to quit so i'll just type in still playing and it says i am still playing the game um no i don't want to quit um you know what this game is kind of lame so i want to quit the game so i'm going to type in q to quit and it says you have quit the game so you can see that with the do while loop you'll always perform this code block at least once and then at the end we will check our condition then if the condition evaluates is true we will continue to repeat this do while loop over and over again so that's a variation of the while loop it's a do while loop and it's if you want to execute this code block at least once otherwise with the basic while loop you might just skip it completely if the condition is false from the beginning so that's this topic on while loops i'll post all the code that we worked on in the comments down below and pin it to the top and if you're looking for additional practice or a project to work on why don't you create your own example of a while loop and a do while loop and also post it in the comments down below but yeah that is this topic on how while loops work in c plus it's you bro hope you're doing well and in this video we're going to be discussing four loops in c plus plus let's get into it for loops are fairly similar to while loops which we covered in the last lesson and at first it might be difficult to understand the differences between the two so for loops are used to repeat some code only a limited amount of times compared to while loops where it can be repeated infinitely so the advantage of for loops is that we know how many times that a for loop is going to iterate before it even begins for example we could create a for loop that can count down from 10 and once we reach zero perhaps we can display a message such as happy new year but first let me give you the basic syntax of a for loop so this is the basic structure for a for loop we have the word for a set of parenthesis and then a code block for this for loop and we'll put whatever code we want within the code block and we'll have that repeat so many times now within the parentheses of the for loop there's going to be three statements and each of these is going to have a specific function and each statement is going to be separated with a semicolon so let's create a for loop sounds good right let's create a program that will begin by counting up to 10 and then display a message and then after that we'll do the reverse and count down from 10. so we'll need to add that structure that i showed in that last slide so we need the word for a set of parenthesis and then a set of curly brackets for the subset of code for the for loop now there were three statements to fill within the parentheses of the for loop the first is some sort of index or counter and you can declare a counter like variable in here you can name this whatever like index or maybe counter or maybe even x commonly you'll see people declare a variable named i as like index and then we can set this equal to a number perhaps just one so this is our first statement we're setting up a counter for this for loop to track how many times this for loop has repeated next we'll need some sort of condition when do we want this for loop to stop let's say that we want to count up to 10 so we'll continue this for loop as long as i is less than or equal to 10 that's our second statement and next we can increment or decrement our counter and we could just say i plus plus for shorthand so one key feature with for loops is that we know how many times that this for loop is going to repeat before we actually get this even started so if i is 1 and we're going to continue this for loop as long as i is less than or equal to 10 we're going to be repeating this for a loop 10 times compared to while loops we don't know how many times we'll be repeating a while loop that's as long as some condition is true so what do we want to do during each iteration of our for loop let's just display whatever our counter i is so c out i then end line and then after we exit the for loop we can display a message such as you can count to 10. so this program is simply just going to count to 10 and display this message um that's basically it but let's test it out then we'll make a more sophisticated program involving for loops okay so this went by fairly quickly because we weren't adding any pauses or anything but you can see that it repeated the for loop 10 times and it's just counting how many times it repeated and then once we exited the for loop it continued with the rest of the program and it displayed that message you can count to 10. this time we're going to count down from 10 so for our counter i let's set this equal to 10 and for our condition we'll continue this for loop as long as i is greater than or equal to zero now we're not going to increment i we're going to decrement it so we're going to type in i then minus minus so after each iteration of this for loop we're going to subtract one from our counter and then we're going to display whatever our counter currently is and then once we exit the for loop we will display a different custom message happy new year all right so same process as before let's run this all right so let me expand this window we started from 10 after each iteration of the for loop we're counting down by one once we hit zero then we exit the for loop and it displays our custom message and with this third statement where you can increment or decrement your counter you can also increment it or decrement decremented by a certain amount let's say that we want to count down by two well we could say i minus equals two so we will then count down by two starting from ten yep you can see that here we're counting down by two each time likewise you could do the opposite where you can uh add as well so if you want to count up by two you would just say i plus equal two and it's not limited to just one or two you can do like three four doesn't really matter but commonly people will increment or decrement by one and an alternative way of writing this is that you don't necessarily need this third statement within the parentheses you could add this increment or decrement counter at the end of your for loop and this would work just the same too but commonly you'll see people put it within the parentheses so yeah you can see that this worked just the same but there's an alternative way of writing these for loops too but commonly you'll see people have all three statements together for their for loops so the main difference between a while loop and a for loop is that a for loop will execute a limited amount of times and we already know how many times the for loop is going to iterate before the loop even begins and with while loops we do not know how many times the while loop will iterate necessarily it's as long as some condition remains true it could be infinite so here's an example of the differences between a for loop and a while loop so imagine we're in the military and our drill sergeant is instructing us to do push-ups and he says drop and give me 20. this is a perfect example of a for loop because we already know how many iterations how many push-ups we're going to be doing we're going to do 20 push-ups and then we're going to stop an example of a while loop would be a drill sergeant telling us that we need to do push-ups until he says we're done so we're going to start doing push-ups until the drill sergeant says we're done then and it could be forever who knows it could be 20 it could be less it could be more it's while some condition remains true basically so that's the main difference between a for loop and a while loop we already know how many we're going to do with while loops we don't really know let's create another program involving a for loop this program is going to ask for a word from a user and then it's going to use a for loop to spell out each individual character in the word that we give as user input so this is what we're going to do we'll create a string variable perhaps named word and then we will create a prompt to have the user enter in enter the word to spell then we'll use cn or getline whatever the word is we'll create a for loop so four a set of parenthesis then a set of curly brackets then we'll need to place the three statements within the parenthesis of the for loop and we'll have to add the three statements to this for loop the first is the index or counter so we're going to declare a variable named i and set this equal to zero and the reason that we're setting this to zero is because with strings the first position is always zero and then the next position is one then two then three so on and so forth next for our condition what we're going to type here is i is less than our word dot then we're going to use the length function so this is going to return the length of whatever word we type in so this might change every time we pick a new word to enter in and for the last statement we're going to increment our counter by one every time we iterate through this for loop now within the code block for the for loop what we're going to type is c out then we're going to type in the word our word variable dot and we're going to use the at function all right and then do you remember in the previous lesson on string functions if we wanted the first character of a string we would put zero within here if we wanted the second character we would put one what we're actually going to do for this for loop is place our counter i within here so during the first iteration i is going to equal zero so we're going to display whatever the first character is then after we complete one cycle of the for loop our counter i is going to increase by one so then when we reach this statement for the second time it's going to display the second letter after the next iteration it's going to increment by one again and then it'll display the third letter so it's going to display all the letters of a word that we type in and i think i'll just add a space between each letter there's just one thing i have to fix before we run this program i'm just going to add a colon add a space to our prompt okay let's test this enter the word to spell i'm going to type in the word pizza and our program will display each letter of the word that we type in so basically a for loop only iterates a limited amount of times and we know how many times it's going to iterate before the loop actually begins so that's basically this lesson on for loops if you would like a copy of the code that we worked on today i'll post it in the comments down below and pin it to the top if you would like additional practice or a project to work on why don't you create your own program that includes a for loop and post it in the comments down below but yeah that is how what for loops work in c plus plus hey it's you bro hope you're doing well and in this video we're going to be discussing nested loops in c plus plus let's get into it all that nested loops are is this concept of having one loop that's inside of another loop and these come in handy when we get to this topic on 2d arrays they're going to be heavily used these nested loops so the generic syntax of a nested loop you can either have a for loop or a while loop there's other types of loops out there um i'm not going to bother with filling this in so it's basically you have one loop that's inside of another loop so it's as if we're stuck within two loops now instead of just one there's an outer loop and there's an inner loop and it's still possible to have more levels to this than just two but let's keep it simple for now so when we enter this inner for loop we're going to be stuck cycling through this inner for loop a certain amount of times and once we're able to exit we can finish an iteration of the outer loop but we may have to continue cycling through this outer for loop depending on what kind of condition we have and once we begin a new iteration we get stuck back within this inner for loop again and have to keep on cycling through this so i think it would be best if i give you a demonstration of this so we're going to create a program where we can print out a rectangle of a certain symbol that the user chooses and we're going to give a symbol a width and a height to this rectangle and this is a good demonstration of how a nested loop works so let's begin then step one is that we're going to accept a character from the user and then we're going to print a single line depending on what the user types in for a width and then after we complete that program we'll upgrade it to printing out a rectangle like it has two different dimensions so let's create a char variable and we'll call this perhaps symbol and we will want a integer variable named perhaps width and then later on when we upgrade this program to a 2d rectangle we'll include length all right so we'll create a display and we will have a prompt that says enter the oops enter the symbol to use then we will see in whatever the symbol the user types in okay and we are going to c out enter the width and then we will see in whatever the width the user types in all right so we'll create a for loop and then later on we'll turn this into a nested loop so we'll need some sort of index or counter the standard programming practice is to have a integer variable named i and we'll set this equal to zero and then add a semicolon at the end so remember there's three statements within this for loop the first is declaring an index or counter of some sort second is the condition so we want this for loop to iterate depending on what the width is so we can just simply say we're going to continue this for loop as long as i is less than whatever our width is semicolon and lastly we want to increment our index so we can just type in i plus plus so what do we want to do during each iteration of this for loop we want to display so c out whatever our symbol is that the user types in okay so let's run this program to test it so this is only going to print a single line enter the symbol to use how about a money symbol enter the width how about five money symbols all right so we ended up with a single line and the width was 5 and this just prints a single line of characters now let's turn this into a program that will display a rectangle so we're going to add a height so integer height and we will create a prompt to enter the height and we're going to change width to height for this line here all right so what we're going to do now is that we're going to surround this for loop with another for loop and this outer for loop is going to be in charge of keeping track of the height of our rectangle so we're going to take these curly brackets and surround this for loop so now we have an outer for loop and an inner for loop so this is now a nested loop so this outer for loop is going to be in charge of the height of the rectangle so it's going to be a similar process to what we have with these three statements within the inner for loop um one thing though is that with standard programming practice with nested loops they usually use a index or counter called j and the reason that i think that they use this is because typically the default is i for an index and i think they use j as the next one just because it comes next in the alphabet that's my reasoning behind it so it's gonna be the same process as before and i equals zero semicolon and we're going to continue this outer for loop as long as i is less than our height semicolon and lastly we're going to increment our eye counter after we exit this inner for loop we would like to move down to the next line think of it like a typewriter so after we complete one line we would like to go down to the next line so a simple way to do that here is that we can just c out e n d l to move down to the next line as if it's like a typewriter or maybe a 3d printer we'll finish printing one line and then we will jump down to the next line and just continue the cycle until we can escape the outer for loop so let's run this program to try it so enter the symbol to use this time let's try the at sign enter the width perhaps 10 and the height maybe 12. all right so here's our rectangle so let's just check to see that this is accurate so width of 10 1 2 3 4 5 6 7 8 9 10 height of 12 1 2 3 4 5 six seven eight nine ten eleven twelve yeah that works just fine then so this is a fun little program you can do to create or print a rectangle with a symbol width and a height that you specify so that's basically how nested loops work it's just this general concept of having one loop that's inside of another loop and you're not limited to just having one inner loop and one outer loop for example we could take this a step further and put this all within another loop and it's kind of like we would have three different levels then we have an inner loop a middle loop and then an outer loop i can't really think of any practical applications for three different levels of loops but i'm sure they're out there though um really for this tutorial series we'll only be dealing with like two levels at the very most i would say uh but this could be useful for maybe printing some sort of like 2d layout or something and when we get to this topic on 2d arrays these nested loops are actually going to come in handy to display all these elements of like a chart basically if you need to take anything away from this basically just a nested loop is one loop that's inside of another we will cycle through the inner loop and once we exit the inner loop then we will complete one cycle of the outer loop and restart the process all over again oh and a quick note that i forgot to mention when originally filming this video is that with nested loops you're not required to have four loops for example you could have a set of while loops of course you know you would have to have different statements you couldn't have these three statements you would have to have like a single condition um but yeah you can mix it up you can have nested loops that are while loops or for loops or a combination of four loops and while loops it doesn't matter just for this example we used for loops for the inner loop and the outer loop if you're having some trouble understanding this concept of nested loops don't worry we'll be revisiting this topic again when we get to 2d arrays and we won't really be using these anytime too soon i would say uh so you have some time to actually play around with these but that's pretty much this lesson on nested loops if you had like a copy of this program i'll post all this code in the comments down below and pin it to the top if you're looking for additional practice or a project to work on why don't you create a program that involves a nested loop and post it in the comments down below because i would like to see it as well but yeah that is this topic on nested loops and c plus plus what's going on everybody it's bro here hope you're doing well and in this video we're going to be discussing the printf function in c plus plus let's get into it before we begin this lesson just so you know this lesson is optional we won't really be using the printf function too much but it's something you might want to consider for your own future programs so let's begin all the printf function is is that it acts as a control for displaying our output a certain way for example this might be useful for maybe some sort of cash register program and if you add a bunch of numbers together or use a coupon you might get a result like 100 and 25.5 cents so when you display this number you probably don't want three decimal places because well that doesn't really work for currency so we can use the printf function to truncate the last decimal or only display at two decimal places when we produce output otherwise if we're working with columns of data we can do things like line up all of our columns together and add a certain amount of spaces before our information is displayed so this could be useful if you're displaying like tabular data or columns of data too so we can do unique things like this involving the printf function so let's practice by creating some variables related to cars well because i like cars so let's create a few variables so maybe we'll make a make that's a string a model an integer named here a double value named price and we need a char value so perhaps maybe a variable named for sale so if you have your own favorite car in mind feel free to assign some of these variables with your favorite car i will assign some of these variables with my favorite car so come up with your own or copy me doesn't matter so for the make it's going to be ford the model is going to be mustang the year is going to be 20 21 the price oh make sure that this is not within quotes all right we're good then nine thousand nine hundred ninety nine dollars and zero zero zero cents and for sale this is a single character so we could put like y for yes or n for no um it doesn't necessarily have to be those two but i guess that would make sense all right so let's use the printf function to display some of this information so to use the printf function all you do is type in print f then you need a set of parentheses and let's add a semicolon to the end before i forget now we can add a format string within the parentheses so perhaps i will say your car is made by all right now we can insert a value or variable and we're going to insert it wherever we place a percent sign so this begins the format specifier and we can add a value or variable that we want to display but we have to separate it with a comma then we're going to put our value or variable here so to begin we're just going to display the word ford not the variable just the string literal all right so now with this format specifier this percent sign will begin this and this variable or value will be inserted right where this percent sign is and there's a certain combination of values and characters we can add after this percent sign and each of these is going to have a particular effect on how our output is displayed so here's a chart that i made let's take a look all right well here's a little cheat sheet that i made so we begin the format specifier with a percent sign and at the end we need to list the data type of what value or variable that we want to display so there's going to be a certain character we're going to add here to display certain information for example if we want to display a string we're going to put s here and if we're going to display a character this would be c and there's a few others and we'll cover those in just a moment so there's other optional fields that we can add that will edit the width the precision the length and there's certain flags too and we'll cover those in just a moment so to begin let's cover just the different format specifiers we can use for data types and then once we get the hang of that we'll cover flags with precision and length let's head back to our program so if we want to display a string literal where this format specifier is after the percent sign we just type in the letter s to display a string let's see what this looks like when we run it okay your car is made by ford let's try another example so what i'm going to do is just c out endl to move down to the next line you could put backslash n for newline but i don't want things to get too complicated in this area so i'm just going to add a new line after our printf function so what would happen if we did the same exact thing but instead of a string literal we used a string variable so this is what the result is going to be and i'm just going to copy and paste this but we're going to change this string literal to a variable so we can use the variable make and pay attention to see what happens here alright so our car is made by ford and this time we used a string variable and we actually got an unexpected result and let's run this one more time and this should actually change again here's the problem with string variables and the printf function so the printf function was designed to work with the c language but it doesn't have the built-in data type of string and if you were to use this you would get an unexpected result so one trick that i found is that there's a way around this if you want to display a string variable in your printf function in place of a string literal this is what you can do after the string variable just type in dot c underscore then use the str function so you need a set of parentheses here so what this does is that it returns a pointer to an array that contains a null terminated sequence of characters representing the current value of the string object so basically what that means is that this function will return whatever value is at this variable so these should work the same now if we were to run this so your car is made by ford your car is made by ford so just keep that in mind if you're using the printf function to display a string variable you'll want to use this function after the variable name otherwise if you're using a string literal you could just type that here another trick is that with the printf format string you can add multiple format specifiers if you want to include multiple values or variables so let's display the make and the model in one single string so printf your car is a maybe a colon all right so we want to display the make and the model but let's display the string literals first and then in the next example we'll do the variables so we want to display the make so percent sign s because it's a string now we also want to display the model so we can add a another format specifier here and we'll display another string so percent s now after the format string we're going to add two values so the first one it needs to be separated with a comma we're just going to type in a string literal ford now we need a another string to add so we're going to add another comma and then type in our model which is mustang and then let's add a semicolon at the end okay so then this will display two strings so we can add multiple format specifiers to a single formatted string so your car is a ford mustang so with these format specifiers it's as if we're inserting this value or variable wherever this format specifier is located and it's in order so the first format specifier is going to be assigned with the first value and the second one is going to be assigned with the second value so on and so forth in that pattern so if we had three format specifiers we would need a third value or variable here so let's try the same thing but we're going to assign the string variables in place of these so i'm just going to move down to the next line by saying c out endl and let's try the same thing but we're going to use the variables this time so printf your car is a colon space we need the first format specifier so percent s and the second so percent s again so we want the make first so make dot c underscore s t r then a set of parentheses now we want the model so model dot c underscore str and a set of parentheses and i am missing a semicolon at the end all right let's run this to see if it works your car is a ford mustang your car is a ford mustang all right so i think we got string format specifiers down then so and before we move on to the next example i'm just going to move down to the next line and i think i'm going to make this a little more organized so i'm just going to line these up okay that should be good let's display the year which is an integer variable so print f then we need a format string the year is so if we want to include a integer variable we need to use the percent sign format specifier then we're going to put the letter d here and then we'll need to insert a comma then the value or variable so if you're inserting an integer variable a double variable or a character variable basically anything but a string you actually don't need to use this function because with the c programming language these string variables really aren't compatible with the printf function but if you're using a primitive data type like a integer a floating point number or a chart value you don't need to use this function so we could just use this variable and this would work just fine then so the year is percent sign decimal the year is 20 21 so i'm going to move down to the next line just by typing c out endl let's display the price so printf the price is if we want to display a double or floating point number we're going to put percent sign f for floating point number comma then our price variable c out e ndl not necessary but i want to move down to the next line after this there all right so if we want to display a double variable or a floating point number we just use percent sign f for floating point number so the price is nine thousand nine hundred ninety nine dollars point zero zero zero zero zero zero so we're actually going to be formatting this um fairly soon now if we want to include a hr value it's going to be a similar process so print f then a formatted string is it for sale percent sign c to display a char value then a comma then the variable will go here so for underscore sale c out e and dl okay is it for sale yes so remember that if you're using a primitive data type or variable when using the printf function you don't need to use this c underscore str function but if you're using a string variable here for your printf function then you would need to use this to return whatever value is located at this variable well i think we have the different data types down with using the printf function let's cover a few of the flags the width fields and the precision fields that we can use to format our output for the next example let's try and display the price but we're going to limit the amount of decimal places that are displayed because that would accurately reflect a price instead of three decimal portions so if we want to display a floating point number or a double number of some sort we'll use the percent f format specifier so the price is so normally we would type in percent f then a comma then our price however if we want to limit the amount of decimal places that are displayed we're going to type this after the percent sign but before the f dot 2 to limit this to two decimal places so let's see what this looks like right now yeah the price is 99999.00 so zero cents uh but you know what i think i want a dollar sign so i'm going to add that before the uh format specifier so let's try that now yep 9999. and zero cents all right now we can have our output be preceded with blank spaces kind of like we're hitting tab so what we're going to type is printf and maybe i'll just add a note this time this precedes with blank spaces and maybe we'll display a string so percent s and let's add a string literal like whatever the make of the cart was and that was ford then colon c out iandl all right so we can specify or allocate a certain amount of space to actually display whatever value is here let's say that we want to give our string our formatted string 10 spaces to work with and actually display this information where our format specifier is so after the percent sign but before the data type we're going to type in however much space we want to give this value to display so we could say maybe 10 we can pick a different amount too so what happens is that if we were to run this we would actually have all of this blank space before displaying our value but let's count how many spaces are here so we have one two three four five six and we actually don't apparently have 10 that's because we're actually including the space that the value takes up to so all together we would have 1 2 3 4 5 6 7 8 9 10. so you're basically giving or assigning room to actually display your value so this would be really useful if you're displaying like a column of information and you want things to be lined up naturally so that would be a good use to proceed some information with some blank spaces and you can have your value be preceded with zeros so what i'm going to do is just copy this and we're going to make one change so after the percent sign in the format specifier but before the amount of space we want to have this proceed with we're just going to put zero and i'm just going to change this string here this proceeds with zeros all right yep here's all of the zeros and we should still have 10 spaces of room assigned to displaying this value so 1 2 3 4 5 6 7 8 9 10. i think lastly for this video i'm just going to show you guys one flag that you can use to left justify any value within any space allocated using the printf function so what i'm going to do is just copy this line that says this proceeds with blank spaces and we're just going to make one minor change so if we want to left justify this value because normally it is right justified so any extra space is assigned before the actual value is displayed if we want to left justify it we're just going to add a minus sign so percent minus the amount of room that we want to give this value to display and then the data type of the value so this we should probably change this message uh maybe we'll just type in left justify all right so let's see what this looks like right now so it says left justify then the make of our car but we have all of this blank space after but we can't really see it right now so let's add a another format specifier to display after this i'm just going to copy this so we're just going to add percent minus 10 s and then we'll add a make or a model for the car so we'll just add mustang so let's try this again so it should be a parrot now yeah that's fairly clear so now we have 10 spaces to work with 1 2 3 4 5 6 7 8 9 10 and the value is now left justified within the space that we gave to display our value and the same thing is going to be with the other string that we assigned here as well but it's not as apparent here but just pretend that all this room is here well that's the basics of the printf function i do apologize for this long video this was a fairly difficult topic to cover but if you would like to learn more i'll include a link in the description for the c plus website where you can learn more information about this there's still more flags and different values you can add to your format specifier for different effects but these are just a few of the basics and i'll include all of this code in the comments down below and pin it to the top if you would like a copy for yourself but yeah those are the basics of the printf function in c plus plus it's you bro hope you're doing well and in this video i'm going to show you guys how you can create your own custom functions in c plus so let's get into it well us programmers we like to reuse code when available and not have to repeat it unless it's necessary so you may encounter a situation where you find yourself repeating some code more times than what you would like so what if i told you there was a way where you can write some code only once and then you can call or reuse that code whenever you want well that's the benefit of a function and we're actually familiar with a few functions already in the last lesson we learned about the print f function another that comes to mind is the length function uh another is the at function and you may notice that these tend to end with a set of parentheses so with functions they perform some sort of task for us so what i'm going to teach you guys is how to create your own custom function to perform some task for you whenever you call it so i have an idea let's create a function that will display a greeting for us so this is the first step you'll want to do in order to create a function we're going to create this function before your main method and we're going to type in the word void and then we need to come up with a name for this function and your name should reflect the task that it performs if we're creating a welcome message we can simply name this function maybe welcome and there is a common naming convention with functions you'll want to be sure that the first letter is lowercase then we'll need a set of parentheses and then a semicolon so this is step one we have declared a function but step two is that we want to define what it does so we can do that just by adding a set of curly brackets after the parentheses but before the semicolon that's good enough so we're going to write whatever we want this function to do when it's called within the curly braces so we can perform some sort of maybe message to display so we can see out welcome to my [Music] program okay we have successfully created a function named welcome when we call it it's going to do this whatever is between the set of curly braces here now going back to our main method we can call this function whenever we want to perform this task and in this instance we're just displaying one line of code or one message so in order to call a function we're going to type in the name of the function followed by a set of parentheses and then a semicolon at the end so we will perform this task every time we call this function so let's try it yup welcome to my program just like we anticipated now we can keep on reusing this code too perhaps i want to display a second message well i'm just going to call the welcome function one more time so this will actually display this message twice then welcome to my program welcome to my program but i should probably add an endline after this just to make it look better okay so that's the basics of creating a function let's create another that displays a goodbye message so it's going to be the same process as before so void maybe we'll call this goodbye set of parentheses then a set of curly braces then a semicolon all right and then we will display a message that says goodbye endl all right so we have two functions now one named welcome and another named goodbye we can call either of these functions whenever we want in our program so for our example we'll call the welcome function and then we can call the goodbye function so goodbye parentheses semicolon let's try this yeah welcome to my program goodbye well i believe we have the basics down for declaring and defining a very simple function but let's take it a step further let's take it to level two now when you call a function you can also send that function some information this can be a value a variable or even an object for example let's say that we want to send a name to our welcome function and that welcome function can do something with that name for example we can display a name that we send it along with our greeting so if you want to send a function some information when you call that function you're going to place whatever values you want to send that function within the parentheses let's send our welcome function maybe a first name so i'm just going to type in my first name as a string literal within the parentheses when we call this function so whenever you send a function some information these are called arguments it's the information that you're sending a function think of it like male the male or the letter that you're going to be sending is also referred to as an argument all right so this is step one step two with sending an argument to a function is that the function needs to be set up to actually receive this argument and this is done by setting the parameters of the function so within the parentheses of your function definition what we're going to do is that we are going to list the parameters of what we want this function to receive if we want to receive a string that's going to function as a name we're going to write the data type of this argument followed by a unique name for this value so we can just name this name all right so you can see that the red squiggle went away so we can now send a string that will function as a name to our welcome function kind of like we're sending mail to this function and this function is going to take this name and it can do something with it so we could actually display our name along with the welcome message so i'm just going to add that here and this should work just fine then so let's try it welcome to my program bro now whenever you call a function and it has a set of parameters you need to send a matching set of arguments when you call this function for example if i were to take out this first name the string that i had here and attempted to call this function well there's not a matching set of parameters and arguments when we call this function so this would result in an error then so we can't actually do this unless we were to send some sort of string to this function so let's take this a step further let's accept some user input and this time we're going to send two arguments maybe a string and then maybe an edge so this will be an integer variable so we need to list the data type here and then we will call this edge so we can no longer just send a string we also need to send a integer as well so maybe i'll just type in 18. so make sure that each argument is separated with a comma so let's change our program around a little bit so i'm going to copy this and paste it and i will just type in you are age years old but there we go and now we have a matching set of arguments and parameters so now we can call this function and perform this task for us so let's try it welcome to my program bro you are 18 yo goodbye now we can send some variables and not just some hard values to a function and let's accept some user input from the user so we're going to declare a string variable named name and an integer variable named perhaps age and we will provide a prompt for the user to type these in enter your name then get line c in comma name just in case they type in a last name but i'm forgetting a semicolon there all right and we will do the same thing with age so c out enter your age endl semicolon cn age all right and now we can actually place variables here because they're going to store whatever values that we assign to them so we're going to place a name variable and the age variable all right this should work now let's try it enter your name bro washington enter your edge 107. welcome to my program bro washington you are 107 years old oh and one quick note if you're sending some variables over to a function they do not have to have the same name so for example i have name and age here that we're sending our function we can actually rename these when we receive them so for example i could change this to user name and user edge then you'd have to change it here too if you use them so username and user age and this would work just the same then so they don't have to have the same exact variable name if you send some variables over you can rename these all right let's take it a step further let's go to level four with functions at least i think we're on level four so we can have a function return something so normally we have the set to void this keyword is the return type and we're not currently returning anything when we call a function so i think it would be best if we started a whole new program and if you want a copy of this i'll post it in the comments down below so don't worry but i think it would be best if we were to start fresh then well i have an idea let's create a program that will accept two numbers from a user and we will take those two numbers send them to a function the function will add them together and return the result so let's begin that program here let's create two values and these can be uh double variables so double number one and double number two c out we'll create a prompt to enter in the first number enter in number one okay then cn number one and let's do the same thing with number two enter in number two and c in number two not three two we're good all right so let's create a function that is named add and we're going to send two double variables as arguments all right so now we need to create this function called add so before when we typed in void this is the return type so we want this function to return a result so we need to list the data type of what we're returning here so we're going to be returning a double value so instead of void we're going to type in double because we're returning a double value then we're going to list the name of the function and we need to set up the parameters so we're going to receive a double value so we list the data type here and we will call this maybe num1 then we need to set up a second parameter so this is going to be double num 2. let's define this function so a set of curly braces and then a semicolon so what do we want this function to do we're going to add these two numbers together we could call this double results we're going to declare and assign this variable right away so num1 plus num2 all right lastly we just need to return the result back to this method called this function call so at the end of your function we're going to return whatever we want we're going to return our variable result all right so now when we call this function it's going to add these two numbers together and it's going to return a result but we need to do something with that result so here i'm just going to create a new variable we can call this double result equals whatever the uh is returned here when we call this function and lastly let's just display our output our result so see out your result is result endl okay let's try this then enter in number one 3.5 enter a number two 7.1 your result is 10.6 yeah that seems right all right so that's what you do to return a value instead of using that void keyword you write down the data type of what you're returning exactly so just to reinforce this idea of returning a value if you call a function and it returns a value there's a data type when you define this function the data type when you define this function has to match the data type of the value that you're returning so if our variable result was maybe an integer we would want to change the return type when we defined this function to also be an integer or if this was a string value we would also change the data type in our definition to be a string as well so make sure that the value you're returning and the data type in the definition also match and my last tip for today is that if you are declaring a function you need to be sure to declare it before the main method because art programs work from the top and work their way down so if i were to move this declaration for this function and moved it to the end this would actually not work then we'll get an error and you can see that here so another trick is that for some reason if you do want your definition for your function to be after the main method you can actually simply just declare the function and not actually give a definition for it yet so you do need a definition before the main method so we'll write that here so that was double add and then we needed a set of parentheses and there were two parameters double num1 and double num2 and then we're not going to add a set of curly braces we're just going to add a semicolon so this would work then yeah you can see it here it looks like it's working just fine so with your own program if you really want the definition to be after the main method you can do that but you'll want to be sure you're at least declaring this function before the main method at least but otherwise it depends on how you want your program organized i tend to just declare and define my functions before the main method so do whatever works for you then but i thought i would just mention that so that's the basics of creating functions if you would like a copy of the code we worked on today i'll include all of this in the comments down below both the first example and the second example that we both worked on and if you're looking for additional practice or a project to work on why don't you create your own function and post it in the comments down below but yeah that is how you can create your own custom functions in c plus plus hey how's it going everybody it's bro here hope you're doing well and in this video we're going to be discussing overloaded functions in c plus plus and at the end of this video we're going to be using overloaded functions to bake some pizzas so let's get into it so basically speaking overloaded functions is this concept that you can reuse a function's name as long as it has different parameters set up and for this example i have two functions both named bake pizza and this is legal because each of these has different parameters this first function accepts one argument as string bread and the other has two parameters set up that accepts two arguments bread and a sauce so this is actually legal then but if these two parameters for each of these functions was the same this would result in errors then so let's try and compile this and run it so it says there were build errors would you like to continue no i don't so this obviously doesn't work but if we were to have different parameters set up this is actually legal then and you can see that it's already compiling with no problems although it doesn't do anything yet so let's create a program where we will have different overload functions and we can pass in varying amounts of ingredients as arguments so let's begin fresh then let's create a few variables related to pizzas so we'll we need a bread of some sort and we'll assign this later the values to each of these variables we'll need a string sauce string cheese no pun intended string and a topping all right now let's create four different functions all named baked pizza so let's begin with a function that takes all four ingredients as arguments so this won't return anything we'll just say void bake pizza then for the parameters we're going to set up four different fields string bread comma string sauce string cheese and string topping and then maybe we'll just display all the ingredients so maybe we'll just see out here is your then we'll list all the ingredients for this pizza see out bread i'll probably add a space between these two see out sauce space c out cheese and lastly c out what was it a topping and then we'll just display the word uh pizza here at the end so pizza okay so that should be good then now we just need to uh copy this and then we're going to create another function with the same name but a different set of parameters like this one won't have any toppings at all so we'll just take that out and we'll take that out here as well all right then we'll copy this paste it again and this time we will take out the cheese then and seriously who orders pizza without cheese all right and then just plain bread i guess which is kind of silly all right so we have four overloaded functions with uh for this function named baked pizza then so now we can actually pass in some ingredients but we'll probably want to assign these so for a bread maybe we'll say thick crust and for a sauce i don't really know many sauce names maybe marinara actually red sauce is probably a more accurate term uh mozzarella i really don't know how to spell this and then toppings what about pepperoni okay so we have a bunch of ingredients set up now we can call the bake pizza function and send basically any uh of these values here as arguments so let's say we only want bread sauce and cheese well this will actually pass all of these as arguments then and it will perform whatever code is within each of these functions then so let's try it now so let me expand this a little bit here is your thick crust marinara mozzarella pizza now we can pass in the topping but spell it right here is your thick crust marinara mozzarella pepperoni pizza and then we could do just the bread too yep here is your thick crust pizza i just realized that i had a space before the word pizza and i didn't feel like refilming this whole video just to go back and correct that so basically with overloaded functions you can reuse the same function name as long as each of these functions has different parameters set up so this is fairly convenient because you can just call one function and pass in varying arguments depending on what you need so for our baked pizza example we can send whatever ingredients we want and we don't need multiple baked pizza functions like we don't need baked pizza one baked pizza two big pizza three so on and so forth we can just say bake pizza and then pass in the arguments that we want but yeah that's the basics of overloaded functions if you would like a copy of this code i'll post it in the comments down below if you have a program that you're working on that involves overloaded functions feel free to post that in the comments if you want to share it but yeah that's the basics of overloaded functions in c plus plus hey what's going on everybody it's your bro here and in this video i'm going to teach you guys how to create your own random numbers in c plus so let's get started hey everyone so we're going to be discussing generating random numbers in c plus plus but i do have a disclaimer these are not true random numbers but something called pseudo random numbers which are very close so using pseudorandom numbers would be fine if you're making a simple game in c plus if you need to simply roll a dice or deal some cards but i wouldn't use these for any like high level program that involves some security but this would be fine just for a simple game so let's begin now at the top of your program this is what we're going to include we're going to include two things so include s t d l i b dot h and this is for the s brand as well as the rand function the other thing that we're going to include is time dot h and this allows us to work with the current time all right so the first step to creating a pseudorandom number is that we are going to initialize a random seed based on the current time this is what we can do to do that type in s rand then a set of parenthesis and within the parenthesis type in time another set of parentheses and the word null so this is going to generate a random seed for us and it's going to change based on the current time there are other techniques of generating random seeds for pseudorandom numbers but this is one that you'll commonly see so that's why i'm introducing this method instead of others all right now we want to assign a random number to a variable so we'll declare a variable such as number and we're going to set this to rand parentheses all right and this next part is we're going to type in modulus and the range of numbers that we want to generate so let's pretend that we want to roll a six-sided dice we're going to type in modulus then 6 alright so we're going to get a random number between 0 and 5 but it's a total of 6 numbers so you always count zero when you're generating a random number so if you want the numbers one through six one thing you can do is just add one so this will generate a random number between one and six for us so let's actually display this just to test it so we're going to see out whatever our number is so let's run this okay we got one let's try it again five and one last time and five again all right so we know that it's working then so if you want to change this to a different number you can so for example i play a lot of dungeons and dragons so we use polyhedral dice that might have like 20 sides or 12 sides so if you want to roll a 20 side dice i would just put 20 here and then plus 1 so then i'll get the numbers 1 through 20. so let's try this now as if we're rolling for initiative so we just crit failed we got a one let's try it again we got an 11 and we got a 20. so i have an exercise that we can work on let's generate three random numbers and display them kind of like a slot machine but without all the fancy pictures so what i'm going to do is just copy this statement and paste this a few times and i'm going to change these variables to number one number two and number three and maybe let's change the range from one through ten and then i'll just display these so number one maybe i'll add a space between here number two and add a space and number three okay let's try this we got seven three and ten three nine and seven three six and two so this will generate three random numbers between the ranges of one and ten and if you know how to work with gui components or some sort of visual you could easily just replace those numbers with some sort of picture or other like graphical image if you do want to simulate like a slot machine of some sort but this would be a good place to start to at least get the random numbers going so just a quick summary that when you use this random function you can use modulus and the range of numbers that you want to generate if you want 100 numbers you could change this to 100 but it's going to be the numbers 0 through 99 that you get so you can add like one to get one through 100 otherwise you can always add a higher number to this for some reason like you can add 10 then all numbers that you generate would have 10 added to them then at that point so it depends on the program you're really working on too so you can use modulus the range numbers that you want then if you want to add something to that number you can do so well that's the basics of generating random numbers in c plus plus but i'm going to walk you guys through how to create a guessing game if you're interested so let's delete all this and start over i'll include all of this in the comments down below so don't feel like you missed out on it so before we get started you'll want to be sure to include these two things std lib.h and time.h if you haven't done so already now we'll need to declare a few variables we'll make an integer that will be our number that we generate an integer that will be our response and an integer that will serve as some sort of score and we're going to initialize this right away and set it to zero now we'll need to initialize a random seed based on the current time so s random then time parenthesis null alright and for our number we're going to set this equal to rand parentheses modulus and then if we want a random number between 1 and 10 we can just put 10 here plus 1. otherwise we can make this extreme and say like 100 so we have to guess a random number between 1 and 100 but let's start with 10 for now and keep it simple all right the next step we're going to put all of this within a do while loop so do then a set of curly brackets and while than our condition so the condition is that we're going to continue guessing as long as our number does not equal whatever our response is so there's a few lines that we're going to fill out within the do while loop the first thing that we're going to do is prompt the user to enter in your first guess enter your guess 1 through 10. all right then we will see in whatever the response is then we're going to use a few different if statements so if our response is greater than the random number what we're going to do here is c out that was too high okay so then else if the response is less than the number we will see out that was too low all right then lastly we're going to put an else here because if the response is not greater than the number and the response is not less than the number then the response must be equal to the number so then we will type in or type c out that was correct and i think i'm just missing a semicolon here and after the else statement we're going to increment our score counter by one then i also need a semicolon after the while loop here as well all right so then after we escape or exit the do a while loop we'll want to display the results so i'm just going to c out number is the number was and then see out the amount of guesses that it took us so we'll see out our score and i think i'm going to add a new line before this just to give us some space you know what i'm going to add a new line character here by guesses and after each of these c out statements as well because i forgot to do that okay let's run this now okay enter your guess one through ten i'll guess five because it's near the middle that was too high let's try maybe three that was still too high one that was too low so i'm guessing it's two then all right that was correct the number was two and it took us four guesses one two three four let's try it one more time just to be sure it's working so into your guess let's try six that was too high maybe three that was too low how about five that was too high and four that was correct the number is four and it took four guesses all right so that is a very basic guessing game so that's the basics on random numbers in c plus plus if you would like a copy of all this code i'll post it in the comments down below and pin it to the top but yeah that's the basics of pseudo random numbers in c plus hey what's going on everybody it's your bro here and in this video i'm going to teach you guys how pointers work in c plus so let's get into it so basically whenever we declare a variable that variable has an address in your computer's memory where this value of that variable is going to be stored so pointers are necessary for dynamic memory allocation and that can't be performed without them so we'll be using pointers for a project when we cover the topic on arrays and how they can be useful so let's begin by creating a few variables and i'll show you how this works i already have one string variable named name and i assign this a value of bro let's create another maybe an integer variable named age and i will assign this a value of maybe 100 i am 100 years old and let's display these using a c out statement so we'll see out name and then add an end line after and c out age and we'll also add an end line after this as well so this as you would expect will display the word bro and the number 100 so now there's this ampersand symbol that you can add to a variable and this is called the reference operator also known as an address of operator i've heard people call it both things so this will return a variable's address in memory as in the street address in ram where it's currently located so let's run this and see what happens now all right so we get these two funky numbers the first one is for our first variable and the second one is for our second variable edge so think of this like your own street address for wherever you live for example like one two three fake street except this number is the address for wherever this value is stored well this number is kind of the same thing but instead of a street address it's the street address in your computer's memory where you can find this value it's the location in ram of this variable and one thing that you may notice that every time we compile and run this program the address may change so these addresses are really only temporary now you don't even need to assign these variables a value just by declaring these variables you're already creating an address of where these variables are going to store a value so let's run this again but we're not going to assign a value so you can see that these variables even though they do not store a value they're only declared we still have space for them to actually store a value at these addresses in our computer's memory now the next step is that we can store an address in a separate variable and that is what's called a pointer a pointer is a variable that stores the address of another variable and to create a pointer we need to declare a pointer just like we do with other variables or constants so this is what you do to create a pointer so let's clear out what we have now and start fresh so let's create a string variable named name you know what i guess we could have kept this line but too late already deleted it and an integer variable named age and i will set this back to 100. now these are the steps to create a pointer now the first step is that if we're going to create a pointer for a variable we first need to list the data type of the variable that we're going to create a pointer of if we're going to create a pointer of this variable name that's a string we're going to declare this as a string the second step is that we're going to list an asterisk and this is known as a d reference operator and it returns the location value of a variable now we need a unique name for this pointer a common naming convention is that you take the name of the variable and you add a p before it and you take the first character of that variable and make it uppercase other names would still work that's just one naming convention so this is basically the pointer for our name variable and we're going to set this equal to ampersand name which is the address of our name variable and pname is now a pointer for this variable so let's do the same thing with our edge then following the same rules so we need to list the data type of this other variable which is an int for integer an asterisk so we're using the d reference operator then let's create a name for this pointer so i'm just going to type in edge add p before it and change the first letter to capital so ph for pointer age and we'll set this equal to the ampersand and then age all right so we have made two pointers and let's actually display this just to prove that i'm not a liar so we're going to c out p name then maybe e and d l and c out p edge endl as well so what you get here is that this these pointers are going to display the addresses of these variables then yeah that seems like it works then so one thing to keep in mind is that if you display the dereference operator along with these pointers uh it actually is going to display the value at these addresses so we got our values bro and 100 and that kind of defeats the purpose so if you're working with these pointers um or you're displaying them make sure that you don't also include the dereference operators so let's create one more pointer just for practice i'll create a string variable maybe we'll call this food equals and then list your favorite food here for me that's pizza all right so we have a variable named food i want to create a pointer for this so we need to type in the data type so string use the d reference operator then we're going to create a new name for this pointer p food equals ampersand then the name of our variable so food and let's display this so c out p food endl and here is the address that is contained within our pointer p food for our variable food so these pointers may not seem too useful right now but at least we know how to create them they're used in dynamic memory application and when we get to the topic of arrays we're going to be creating a project where we will want some dynamic memory because it's really useful to have so that's the basics of pointers hopefully this video gave you a few pointers as to how pointers work if you would like a copy of all this code that we worked on i'll post it in the comments down below and if you're looking for additional practice then in the comments section post how about three pointers that you created but yeah that is how pointers work in c plus hey how's it going everybody it's bro here hope you're doing well and in this video i'm going to teach you guys about arrays in c plus plus and at the end of this video we're going to work on a project where we can assign some names of cars and then place them all within an array and then we're going to take this array display all the values into some sort of virtual garage kind of like this pretty cool right let's get into it [Music] all an array is is that it's a variable that can store multiple values so for example i have a string variable named car and it's storing a single string with the word corvette now we can actually turn this into an array by following a few simple steps so let's say that we want to add multiple car names to this variable well we'll want to turn this into an array then so the first step is that next to the variable name add a set of straight brackets and then all values that we want to assign to this array we're going to surround with a set of curly braces all right so this is our first value if we want to add a second value we're going to separate each value with a comma and then we can add maybe another car name so think of this maybe like a parking lot or a garage we're adding multiple cars to this array and they're each going to take up one spot so for the second car name i think i will assign a tesla and the third how about a ferrari all right and then with the name of the array you should pick a name that reflects the values that it's storing so before we had a single variable named car so i think i'll turn this into cars because it's plural now so there we go we have just made an array now there's one limitation with arrays all the values that are assigned to this array have to be of the same data type so for example i couldn't mix an array with different data types like for example let's get rid of this ferrari and just add in the number 2021 like a year well this would actually not work because we're mixing different data types this is an integer and these are strings so we have to be sure that we're only adding values of the same data type although we could add the string representation of the number 2021 this would technically work um maybe we could change this to years and we'll make this an array of integer values so let's start adding some numbers so like maybe 2020 2021 and 2022 so this would work the same as well with the rays you have to make sure that all values are of the same data type now let's head back to our previous example of our array named cars now pay attention to what happens if i attempt to display this array named cars alright so take a look at this we get this address and you probably learned about this on the lesson on pointers this is where this array is storing these values at this address so if you want to display one of these values that's located in this array you actually have to label the element so what you want to do is that when you want to display one of these values type in the name of the array then that set of straight brackets and then you're going to put the element number of the value that you want to retrieve and if you want to retrieve let's say this first value you're actually going to type zero it's a computer science thing arrays always start at zero and the next position would be one and then two and then you continue in that pattern so if we want to display this word corvette or retrieve whatever is at this element element number zero we're going to type in the name of the array followed by a set of straight brackets and then the element number which in this case is zero so let's see what happens now and it displays our first value which is corvette so now let's say we want to display this second value we're going to enter in element number one within the set of straight brackets let's run this just to test it yep that's right we have a tesla in spot number two also known as element number one and then let's do the same thing with our last element here so that is index number two or element number two and that has a ferrari now what happens if we attempt to retrieve an element that doesn't exist within our array so let's say three so we don't have an element three we have element zero one and two and this results in a problem so an exception was thrown it'll say something such as index out of bounds and if we added another element to this array we actually wouldn't have that problem then so i think i'll add an audi this time and then let's display this yep oh and another thing with these arrays you can always update these values so let's say that we want to change that last element element number three we're going to say cars straight brackets three equals and then we can pick a new value so maybe i'll place a i don't know i like mustangs let's put a mustang in there and then let's display this again so cars 3 equals mustang yep it's right there let's say that you want to display all the values in this array let me show you what you don't want to do so what you don't want to do is simply just to see out all of these manually by hand so for example i'll just copy and paste this c out a few times so if we want to display the first element that is element number zero the second one is one two and then three all right so this would actually display all of them um but it's probably not the best programming practice to actually display all these let me show you a better method to display all the elements of an array using a for loop then so let's start over fresh but we'll keep our array of cars so the first thing that we're going to do is create a for loop so we're going to type four then a set of parentheses and then let's add a set of curly braces now there's three different statements that we're going to fill out within the parentheses of the for loop we'll need some sort of index so we'll say into i and set this equal to zero then a semicolon to move on to the next statement the second statement in our for loop is for our condition how many times do we want this for loop to repeat and cycle well we want this for loop to repeat for as many elements there are in our array of cars so one way to write this is that our condition is going to be i is less than size then a set of parentheses and within the parentheses we're going to type in our array name of cars and then we're going to add a semicolon to move on to the next statement so this is dynamic so if we were to increase the amount of elements that are within our array we wouldn't need to manually adjust how many times this for loop is going to repeat when we compile this program this number might update depending on whatever the current amount of elements is within our array so we could freely add or remove elements without worrying about our for loop working or not this is going to adjust so for the third statement we're just going to increment our counter so we can just type in i plus plus so now this for loop is going to repeat for as many elements there are in our array so let's say that we want to display each of these elements now so before what we did is that we just typed in c out and then the name of our array straight brackets and then we put a number in here so if we wanted the first element that was element number zero this would display the first element then the second one would be one two three so we would continue on like that so what we're going to do within this for loop is actually put our index of i so i is currently set to zero so when we reach this part of the for loop it's going to read cars at index i as in zero and the second time that we cycle through this for loop i is going to increment with this statement and i is now one and it's going to continue after each cycle so i'm just going to add an end line and let's try this out now all right here is our array of cars then that we made let's try another example of this just so that we really have the fundamentals down of arrays so i'm going to create another array of a different data type so for cars i'm just going to comment this out for now and let's create a new array maybe years so let's make this of the integer data type we're going to come up with a unique name for this array that reflects the values that it stores so years then a set of straight brackets equals and then we're going to add a bunch of values between this set of curly braces and i'm just going to make up some years so maybe 2021 22 2000 1999 1842 all right and then for our for loop we're just going to change the word cars to years so here and here and let's try this yeah that looks about right we have five elements within this array this array called years and they're all displayed here well that's the very basics of arrays i'll post this code in the comments down below if you would like to take it a step further i'm going to show you some more advanced stuff involving arrays what we're going to do is dynamically allocate an array after the compilation phase by using a pointer don't worry i'll walk you through it so we will be able to ask the user for the size of an array and then have the user assign values to it so we're actually going to make a project we'll ask the user how many cars they have and we're going to park each of their cars into an array think of it like a garage and each element has a parking spot number so if you want to continue let's take it to level two all right well if you're ready for some more advanced stuff we're going to create a program that's going to ask the user how many cars they have they're going to enter in some names of these cars and we're going to put them all within an array that's going to function like a garage and at the end we're actually going to display all the cars that are within our garage all the elements in our array the first thing that we're going to do is to create an integer variable and let's call this number we're not going to assign this a value quite yet we're only going to declare it because we're going to have the user type in a number for the number of cars that they have so we're going to see out how many cars do you have and then let's see in their response and we'll store that within our variable number this number is going to reflect how many cars they have and this is going to function as the size of our array now this is the tricky part we're going to create a pointer for our array because we don't know how much space we need to allocate before the program actually compiles so these are the steps we're going to create a variable of strings so we're going to type string then we need to use the dereference operator and we're going to create a pointer so a common naming convention is to type in p lowercase and the name of the variable or the array and the first letter is uppercase so if this array is going to be named cars we can just type in cars here with a capital c equals new then the data type that we're working with so it's an array of strings so we're going to type in string a set of straight brackets okay so then within the straight brackets this is the amount of space that we want to give this array we don't know how much space we're going to allocate it's going to depend on this number so if you were to type in 10 this would allocate 10 spaces 5 would give 5 spaces we're going to put in our variable number within here and then add a semicolon at the end so this is our pointer p cars for our array all right pcars is a pointer that's storing the address of our array and when we assign values to this we're sending values to whatever address is stored within our pointer now we're going to create a for loop that's going to repeatedly ask the user for each name of each card they have for as many times as the number that they typed in so let's create a for loop so four a set of parenthesis and then a set of curly braces there's three statements to fill out within the for loop we'll need some sort of counter or index we can declare that within here and i and let's set this equal to zero semicolon the next statement is our condition we could easily just say we'll continue this for loop as long as i is less than whatever the number the user types in semicolon and then we're going to increment our counter by one within the for loop we're going to create a prompt for the user to enter in the name of each card that they have so let's see out some sort of message enter car number and then we will put the value or the index of i here now one problem though right now during our first iteration of the for loop i is going to be zero and if some user doesn't know any programming and they probably won't they'll be confused when it says enter car number zero like what does that mean so what we can do to actually offset the display for our index is just say i plus one all right so this won't actually affect the value of one it's going to affect how it's displayed though to the user so i is still going to be zero but when it's displayed it's going to be displayed as one so if somebody's using this program and it says enter car number one that actually makes sense before we had enter car number zero where that probably wouldn't we understand what it means but the user might not so we're going to say enter car number i plus one and then let's add a semicolon and then we're going to see in actually let's get line because some car names might have some spaces between them so get line c in and comma and then what we're going to type in here is the pointer that we made so p cars at a specific index so then we need a set of straight brackets and then we're going to put our index or counter i within here and then just add a semicolon at the end now with this for loop it's going to have the user enter in a value for each car that they have and it's going to assign this value at wherever this array is located at wherever the pointer is pointing to which is where our array is located now let's create some sort of graphical display to actually display all this information so maybe we'll just make a see out message such as here is your garage and then maybe i'll create a new line after this maybe before this as well just to make it look nice um maybe let's add like some sort of line here too then i'll create a new line okay i think that's fine and for this last step all we need to do is to create another for loop that will display all the elements of this array this time and honestly we can just copy what we have here and simply paste it and make a few changes all we need to do is to change the contents of this for loop we can leave all of this the same then so let's create some sort of display for each line so maybe c out maybe let's type in parking spot number you don't have to do this exactly as i do you can create your own sort of display parking spot number maybe i or you could do i plus one i think i'll do i plus one you could have a parking spot number zero if it's like a vip parking spot or something do what you want to do uh so parking spot number one and then i think i'll add a space and then i will display the element of each array so if you want to do that we're just going to type in p cars straight brackets and then our index then semicolon oh before we actually do run this there is a few housekeeping things i'm going to add real quick um i forgot to end the line after this and i'm also going to add a space after the number to make it look better i'm kind of doing this on the fly so the syntax isn't going to be perfect all right we should be good then um so there's one thing you're going to notice when we run this and it's going to be fairly evident at first all right so how many cars do you have let's say four and then hit enter alright so you'll notice that it says enter car number one and then immediately it goes to enter car number two and this is the reason why it actually skipped the first prompt for car number one and that's because when we typed in four and hit enter well cn picked up the four but when we hit enter that's still within the buffer and we need to get rid of that because then when we used the get line function here it actually took the new line character and entered it into the first line of our array so what we can do is actually clear the buffer and what we'll do is after we type in c in number we're just going to type in c in ignore and then just add a semicolon so then that should clear the buffer then so that's just one thing you might run into and i wanted to point that out okay now we should be good to go all right how many cars do you have i have four cars enter car number one let's say i have a lamborghini lamb or i don't know how to spell lamborghini i'll just type in lambo uh car number two maybe a tesla number three maybe an audi and number four perhaps a jeep all right here is your garage and here are the four cars that we have they're all within our garage and that is basically how you can create a program to dynamically ask for the size of an array and assign values to it to display if you would like a copy of this program i'll post all of this code in the comments down below and if you're looking for additional practice then why don't you create a program that involves an array and post it in the comments down below if you'd like to share it but yeah that's everything you need to know to get started with arrays in c plus plus hey how's it going everybody it's yubro here hope you're doing well and in this video i'm going to be teaching you guys about two dimensional arrays in c plus plus and at the end of this video we're going to be working on a project where we can create a virtual computer keyboard it currently doesn't do anything all it does is look pretty but i'll teach you guys how to create this using a two-dimensional array so let's get started think of a basic array as one-dimensional as in it only contains a single row of elements while a two-dimensional array can contain many rows of elements think of it as an array of arrays so on your screen right now i have a basic one-dimensional array well we can turn this into a multi-dimensional array by adding more arrays to it but there's a few changes that we have to make so if this is going to be a two-dimensional array we need two sets of straight brackets right next to the array name and we're going to need to place the amount of rows and columns that we have in our multi-dimensional array into each of these straight brackets so this is going to end up looking like some sort of grid or matrix instead of just a single row so we are going to create multiple rows and separate each with a comma and surround our entire set of rows and columns with another set of curly braces so all i'm going to do is just add another row so we need another set of curly braces and i'll put some more numbers in here so 4 5 6 7 i'm just going in order we'll separate each array with a comma then we'll add another array so we need another set of curly braces and i'll type in 8 9 10 11. alright now what we need to do is surround all these rows with its own set of curly braces so we'll put that here and here all right so this might be difficult to see what i like to do with 2d arrays is just to put each of these rows on a new line just so it's easier to see and work with okay so what you see now is that this is starting to look like some sort of grid which is kind of the point of a two-dimensional array so there's rows and columns to this with the first set of straight brackets we're going to place the amount of rows that we have which is one two three so we're going to place three here and this set of straight brackets is for the columns and we have one two three four so we'll place four here and that ladies and gentlemen is a two dimensional array now if you ever want to determine how many elements that you have in your 2d array just multiply the amount of rows by the amount of columns that you have so in this example we have three rows times four columns so that's 12 elements so here we have 12 but remember that we start with zero though so it's going to be the numbers 0 through 11. let's say that you want to work with or display one of these elements in the two-dimensional array you'll need to retrieve that specific element so it's going to be very similar to a one-dimensional array but there's one extra step so let's say that we just want to display one of these elements so we'll just c out then we need to type in the name of the array so for my example the array name is numbers but we need two sets of straight brackets we're going to place the row number here and the column number here of what we want to retrieve so let's say that we want to retrieve this number six so we need to find out which row that it's in and remember that we start with zero so this is row number zero and this is row number one so we're going to place one here and then let's count the columns so this is column number zero column number one column number two so we'll place two here because we want this six and that's pretty much it let's run this just uh prove that i'm not a liar yep here's our number six all right here's an exercise for you what do we place here for the rows and columns if we want to retrieve this number eleven take a moment to figure it out yep that's right we're going to place two here and then we're going to place the number three let's test this out yep there's our number 11. so let's say that you want to display all the elements of this array well one easy way to do that is to use a nested loop we'll have a outer loop that's in charge of the row that we're on and an inner loop that's in charge of the columns so for the statements for the outer loop this is what we're right so usually you can write into i and set this equal to zero right we could do that but i think for this demonstration i'm actually going to rename i to row all right and then we're going to continue this while our row is less than the amount of rows that we have which is three and then we're just going to increment our row by one and it's going to be basically the same thing for the columns so we can say int column equals zero we'll continue this column as long as column is less than the amount of columns that we have which is four and then we're going to increment our column by one so within the inner for loop what we'll type is c out and we're just going to display our array numbers at the index of row as well as the index of column and then inside the outer loop we'll just want to move down to the next line we can easily just do that with c out endl and before we run this i'm just going to add a space after each of these elements just so everything's lined up nicely okay let's run this yeah and here is our two-dimensional array it got kind of funky with uh these two-digit numbers but you can still see that it's a grid of some sort a matrix of rows and columns that contains values so that's the basics of two-dimensional arrays if you would like to continue we're going to work on a project where we create a virtual keyboard kind of like the keyboard that's in front of your computer right now assuming you're not on a mobile device so if you want to continue let's work on that project well welcome back so for this project we're going to create a virtual computer keyboard maybe not the entire thing but we'll just include maybe the numbers and the letters and some of the other special characters and then afterwards if you want to go crazy and add like an escape button a backspace button enter shift you could do so but we'll just keep it simple for now then you can go crazy afterwards so let's begin by making a 2d array of maybe characters so we'll make this of the charred data type and let's call this 2d array maybe keyboard and then we need two sets of straight brackets and we're going to set this equal to then all of the keys that we want to add to this keyboard so i'm just going to stick with the numbers one through zero well one through nine and zero and then the first three rows of letters so that's q-w-e-r-t-y so on and so forth all the way until the last row of letters so i'm actually going to fast-forward this video because it's going to take a lot of time to actually enter in all these individual characters so feel free to pause the video otherwise i should have all of this code completed and posted in the comments down below so you can always copy this portion of the 2d array if you don't feel like typing this by hand so i'm just going to fast forward the video until the point in time where i already type out all of these individual characters and then i'll walk you through it a few moments later all right well welcome back again so here is our two-dimensional array of characters the first row is the numbers then we have q-w-e-r-t-y blah blah blah blah the second row is a-s-d-f blah blah blah then z-x-c-v and then we had some extra space so i just added a few special characters here all right so then with the straight brackets we need to list the amount of rows and columns that we have so we have four rows we're going to place the number four within the first set of straight brackets and we have 10 columns so we're going to place 10 here so this is now our 2d array now let's say that we want to display this we're going to use a nested loop to do this kind of like what we did before so we need an outer loop for the rows and an inner loop for the columns so what we'll type in here is maybe int then let's go with rows again so into row then we'll continue this as long as row is less than the amount of rows that we have so we can put four here all right and then we'll increment our row and we'll do the same thing but with columns so int column equals zero then column is less than 10. all right and then column plus plus and then we are going to display each of these characters so we will see out our keyboard at whatever our row is as well as our column and then we'll just add a space between each of these characters when we display them all right and then we just need to move down to the next line so we'll place that within the outer for loop so we'll just c out endl okay we should be good then let's run this yep and here is our virtual keyboard so someday if you ever learn about graphics for c plus plus you could easily replace all of these individual characters with labeled buttons and then you can click on each of these buttons to actually enter in a character or letter so that would be really useful then one day so basically a 2d array can be used to display like a grid or matrix of information whether it's like a table a chart a grid a 2d array is pretty useful because you can organize it in rows and columns so that's basically how a 2d array works if you would like a copy of all the code that we worked on i'll post it in the comments down below and if you're looking for additional practice why don't you post a program that you worked on that involves a 2d array but yeah that is how 2d arrays work in c plus hey how's it going everybody it's bro here hope you're doing well and in this video we're going to get into object-oriented programming in c plus so let's get into it hey everyone so for this part of the series we're going to get into object-oriented programming so we need to distinguish first the differences between classes and objects so look around you right now wherever you're sitting or standing so you are currently surrounded by different objects for example i have a mic in front of me i have a mouse i have a cell phone and i have a playstation 4 controller next to me so in programming we can actually create objects that mimic these things and it's kind of like it's a new data type so i could create for example a playstation 4 controller that has some qualities and it can perform something otherwise i can create a cell phone object and it can do certain things so we can actually create a class that serves as a blueprint for the different characteristics of an object and what kinds of actions that they can perform so let's create a new class and we'll make some human objects so we're going to create a new class before our main method so to create a new class just type in class and you need a name for this class so if we're going to create human objects we can call this class human the second step is that we'll want the members of this class to be publicly accessible because normally we don't have access to whatever's in this class outside of this class so we're just going to type the word public and this is a access modifier now we can actually assign some variables that all humans should have so what characteristics of humans come to mind so i'm thinking humans typically have names at least that i'm aware of uh let's make that singular though they have an age and maybe a weight so we'll have a string variable named name integer named age and a double variable named wait so these are kind of like nouns they're variables that are acting as the characteristics of what all humans should have now humans can also have like functions so what kinds of actions can humans perform so for example one that comes to mind is eat so we can create a eat function for humans so maybe if we call this function we'll just say this person is eating then we'll just end the line uh let's add a few others here so maybe a drink function this person is drinking and sleeping so avoid sleep this person is sleeping all right so we have finished making this class a class is a blueprint for what characteristics and functions that an object should have now we can actually instantiate an object that's a fancy term for creating an object so if we want to create our first human we'll type in the name of the class so it's human and then we need a unique name for this specific human i'll creatively call this one human one now with our object human one it's kind of like the variable name except that within human one there is a name an age and a weight variable and we'll still need to assign these some values and when we instantiate this object this is kind of like the data type you know with variables we usually list the data type before the variable name well it's kind of the same principle here we're listing the data type of what object we want we want a human not a car not a bicycle a human and we're going to call it humanone so we can actually assign these variables that all humans have so if we want to assign human ones some values to its variables we'll type in the name of our object so human1 dot and let's give this human a name and let's give him a name of rick all right so then this human now has the name rick we can also give him an age so human1 dot age and let's say he's 65 years old there we go i don't think we'll need to assign a weight i think you get the idea so let's actually display these just to test it so we'll see out human ones dot h and then end line and we will do the same thing for name human one dot name so let's try this yep rick is 65 years old now we can create another human so let's create human too so we need to list the data type so it's a human basically and then we'll call this one human too and let's give human2 a name and an age so human to dot name equals morty and let's give human to an age perhaps 16. all right so let's display human 2's age and name but i'm going to flip these because they're bothering me because they're out of order okay let's try this now so human two's name is morty and he has an age of 16 years old now we can actually perform one of the functions within a class so let's have rick use the drink function that he has access to so if you want to use a function that belongs to a class or an object just type in the name of the object that you want to perform that function so we can say human one and we want human one to use its drink function so let's try this now yep this person is drinking so let's create one more human so we list the data type human and we will call this specific human human three and we want to give him a name and an age so we'll type in the name of human three dot name is equal to let's call him jerry and we'll give jerry an age so human three dot age equals perhaps 45. all right now we have three humans rick morty and jerry and they all have a eat function drink function and sleep function uh but there's one thing if we try this though uh since these functions all have basically the same syntax we can't differentiate like who's performing which function so i'll give you an example let's have humanone which is rick use his drink function we'll have human2 that is morty use his sleep function and then we'll have human 3 use its eat function so if we were to display these it's all going to look like the same thing we can't differentiate who's calling this specific function so what we can actually do is that with these strings within our functions we can actually replace this person with the actual name of the object that is calling this function so i'm going to take out this person and what we're going to place here but let me just add these insertion operators so we're going to replace this person with this dash greater than sign and then name so this is going to be replaced with the name variable of whatever object is calling this function so we're going to take these this name and place it here and let's try it now there rick is drinking morty is sleeping and jerry is eating likewise you can do this with other variables too like you can say this age or this weight but you know this name works for this example all right so i think that's the basics of object-oriented programming an object is an instance of a class an object usually has some sort of attributes like values and variables that act like nouns that describe the object itself and they can usually perform some sort of action so for example our humans they have a name age and weight they can eat drink and sleep and a class is like the blueprint of what attributes and functions that all humans or whatever object you're dealing with can perform so that's the very basics of object oriented programming in c plus i'll post all of this code in the comments down below if you'd like a copy for yourself if you're looking for additional practice why don't you create your own class and then post it in the comments down below as well if you'd like to share it but yeah that's the basics of object oriented programming in c plus hey how's it going everybody it's your bro here hope you're doing well and in this video we're going to be discussing constructors in c plus so let's get into it [Music] hey everyone so we're going to be discussing constructors a constructor is a special function that is automatically called when an object is instantiated i needed to write that down because i wouldn't remember the definition so it's useful for assigning arguments to variables whenever we instantiate an object so based on the last lesson with object oriented programming we created three humans well we have two now we lost jerry so we have rick and we have morty so you can see that it's a lot of work to manually assign all these values to variables but one thing we can do is that we can create a constructor to take care of that for us so a constructor is automatically called anytime you instantiate an object of that class so if we want to create a new constructor for our human class this is what we're going to type so after all these variable declarations what we're going to type is the name of our class so we're going to type human then a set of parentheses and then a set of curly braces so this is almost identical to a function but we don't need to list a return type now we can actually set up parameters for this constructor so for example we could pass in some of these values that we're assigning to these variables as arguments now we don't have to do this by hand so we can actually send in the name an age and a weight so let's do this here next to each of these object names so we're going to set up some parentheses and then we're going to pass in some arguments so we're going to send in the name rick and the age of 65. and then a weight of 80 kilograms so let's do the same thing for morty then so we're going to change the name send in the age and a weight in kilograms all right so we no longer actually need these because we're going to take care of this in the constructor so within the constructor we need to set up parameters so we're going to need to set up a string variable that's going to function as a name and integer that's going to function as an age and a double value that will function as a weight all right you can see that all the red underline went away now now we can actually assign some of these values to each of these individual objects then so this is what we're going to type now this is what we're going to type the keyword this dash greater than sign name equals name so this name is whatever we receive as an argument so we're going to follow a similar pattern for the age and the weight so this age equals age and this weight equals weight so these are the values that we're actually receiving and we're assigning these to the variables of the object that is currently using this constructor so if we're instantiating the human1 object we're assigning all of these values to the variables of human one and if human 2 is using this constructor we're assigning all of these values to human2's variables so basically replace this word with the variable of whatever object is actually using this constructor so that's a good way of thinking of this so this allows us to actually reuse these statements before we had to like type these out by hand and it's not too bad if we only have two objects but imagine if we had like hundreds we'd be here all day then so we can simply just keep on reusing this code then now let's display all of human one's variables just to prove that i'm not a liar so we'll see out humanone's name then we'll end line and let's do the same thing for his age and his weight so humanone dot age and human one dot wait i think i'll add maybe years old after the age and kilograms after the weight so kgs all right let's try it yep rick is 65 years old and he weighs 80 kilograms now let's replace this with human two yep morty is 16 years old and is 60 kilograms so now if we ever create a new object we can just pass in some arguments if we want to assign some values right away so if we want to create human 3 and human 3 is jerry and he will be 45 years old and he will weigh 85 kilograms so whenever we instantiate a new object we can just pass in some values to assign to this object's variables right away then so let's display all of human three's variables at this point and we'll try it out yep jerry is 45 years old and weighs 85 kilograms okay guys there's one thing i should mention before you go so with c plus plus it is possible to place a constructor outside of its given class and you can do this with something called a scope resolution operator it's basically these two colon symbols so within a class you can simply have just the constructor declaration like this and then we can place the rest of the code outside of the class so what you might see is the name of the class followed by the scope resolution operator followed by the name of the class again and then like the rest of the code here so this would also be valid and let's actually try this so you can see that it works just fine so that's really the basics of constructors it's a special function that is automatically called when object is instantiated and a good use for this is assigning arguments to variables right away so instead of assigning all these values by hand we can just pass them to a constructor and the constructor can take care of it for us so that's basically what a constructor is so if you'd like a copy of all the code that we worked on i'll post this in the comments down below and your homework for today is to post a constructor in the comments but yeah that is how constructors work in c plus hey how's it going everybody it's bro here hope you're doing well and in this video we're going to be discussing overloaded constructors in c plus and at the end of this video we're going to be creating a project where we're going to be ordering some pizzas so let's get into it hey everyone so this video is dedicated to overloaded constructors and just as a quick reminder a constructor is defined as a special function that is automatically called when an object is instantiated and this is useful for assigning arguments to variables right away when we create an object of some sort so a good example of this would be pizza objects pizzas have varying amounts of ingredients so i have for example four different pizza constructors here and this is legal you can reuse the same constructor name as long as there are different sets of parameters set up so for the first constructor i have four string parameters set up a bread a sauce a cheese and a topping the third just has three a bread a sauce and a cheese then two a bread and a sauce and lastly just a bread so we can create pizza objects and we will use a matching constructor when we instantiate an object depending on how many ingredients we send our constructor so let's actually work on a program where we're going to construct some pizzas and order them so to begin let's create a pizza class so class pizza but make sure you spell it right because i didn't and then we'll want to make these members public so we can access them from our main method so we'll create the first pizza constructor honestly i shouldn't have deleted everything but too late i already did so we'll pass in a string bread string sauce string cheese and string topping and then what we'll do is just assign these values right away so this bread equals whatever bread we receive and we'll do the same thing for sauce cheese and topping all right now we'll actually want to declare these variables so we're going to do that before our constructors so string bread but we're not going to assign them string sauce string cheese and string topping all right so this is our first constructor but i misspelled sauce there so this is our first constructor we can actually create another constructor with different parameters so i'm just going to copy this and paste it we're going to get rid of our topping and then we'll get rid of this line too we'll copy this and make another constructor so this one's just going to be bread and sauce and the last one is just bread okay we have our constructors made up so let's create an order function maybe we'll call this order pizza and this doesn't return anything so we'll put void here all right let me think of something okay i thought of an idea we're going to display a message that says here is your then list all of the ingredients that are not empty and then just add the word pizza at the end kind of like somebody's delivering it to us or we're delivering it to somebody so within our order pizza function here we will just see out here is your and then the next step is we're going to check to see if our bread is not empty so if bread dot empty function but we're going to put the not logical operator here so if our bread is not empty what we'll do is see out whatever our bread variable is and then maybe we'll just add a dash after it now we're going to do the same thing for sauce then cheese and then what was it topping at the end yeah so if sauce is not empty we'll display whatever the sauce is same thing for cheese and lastly topping all right and then we'll just add the word pizza at the end so see out pizza but maybe i should turn this last one into a space then end line okay we're good to go then let's construct some pizza objects now to instantiate an object we first need to list the class as the data type so this is going to be a pizza object and you can see that we have it right here for the class name so next we need a name for this pizza let's call this maybe first underscore pizza and then we can pass in some ingredients as arguments so let's pass in a type of bread so maybe thick crust and let's pass in a sauce i'm not sure what the standard sauce pizza uses it's some sort of red sauce it's a mystery to many and then what about mozzarella i'm probably misspelling that i always do and then you know what that's it let's just stick with these three ingredients and then we'll call the order pizza function so we'll type in the name of our pizza first underscore pizza and then we will order pizza and let's run this okay here is your thick crust red sauce mozzarella pizza now let's create a second pizza with different ingredients now so pizza i'll call this second pizza and i want flat bread and i don't know alfredo and that's it and then we will order our second pizza so second underscore pizza dot order pizza function here is your flat bread alfredo pizza now that's one of the benefits of using overloaded constructors is that they allow us to instantiate different objects with varying arguments and this works great for constructing pizza objects because pizzas can have varying different types or amounts of ingredients depending on the pizza so for the first pizza this has three arguments and the second one has two now if we were to take these out and we only had this one constructor that needed four ingredients well this actually wouldn't work anymore because if we were to construct a pizza we need at least four ingredients but if we put these back in we can have between one to four ingredients but you know you can have more than this too you could have a pizza constructor with five ingredients six ingredients so on and so forth well that's the basics of overloaded constructors so if you would like a copy of all the code here i'll post this in the comments down below if you're looking for additional practice then your assignment is to create a set of overloaded constructors and then post it in the comments but yeah that is the basics of overloaded constructors in c plus hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 206,302
Rating: undefined out of 5
Keywords: C++ tutorial for beginners, tutorial for beginners C++, C++ tutorial, tutorial C++, C++, tutorial, C++ full course, full course C++, yt:cc=on
Id: uhFpPlMsLzY
Channel Id: undefined
Length: 274min 24sec (16464 seconds)
Published: Tue Mar 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.