Fundamentals of C++
The course is part of this learning path
This course provides you with a solid understanding of the fundamentals of C++. We will take a look at the components of the programming language and then put these into practice through a couple of projects that we will run through at the end of the course.
- Learn how to store different types of data in main memory
- Understand how to manipulate and perform operations on that data, including performing arithmetic on numbers
- Understand how programs make decisions
- Learn how you can write your programs to communicate with users
- Beginner coders, new to C++
- Developers looking to upskill by adding C++ to their CV
- Experienced C++ programmers who want to stay sharp!
- College students and anyone studying C++
This is a beginner-level course and so no prior knowledge of C++ is necessary.
In this lecture, we will solidify some of our skills for working with strings. If you aren't familiar with it, MadLibs is a great game and the different MadLibs are usually in books that are fun for pretty much all ages. They are great to keep kids busy while traveling as well. Someone will be asked to provide words of different kinds like; names, occupations, clothing, and even different parts of speech and grammar like adjectives; which describe nouns of course. So, like blue, fat, tired, smelly, small, anything like that or really any host of possible words. Then when the person has filled in or otherwise provided all the words that are requested, they are placed in blanks in a prewritten story and lots of funny and bizarre stories are created as a result. We'll be writing a simple clone of a single MadLibs game. We'll be prompting the user before they see the story for various words and parts of speech. You can collect all this data as strings. So, first off I'm going to show you what this looks like in Visual Studio. So, I have not exposed the code, but we can of course run the project by going to 'Debug, start without debugging.' So, here's our program running and it will very soon ask us to enter an adjective. So, we'll come up with one, we'll say 'Orange' and then enter a girl's name, well say Karen. Please enter another adjective around, enter an occupation Plumber. Please enter the number of a place. So, I'll just go with the city I'm in right now, Dearborn. Please enter the name of an item of clothing in plural. So, maybe 'Jeans,' that's plural and singular so that works. Please enter a hobby, dancing and then please enter yet another adjective, blue. Please enter another occupation, fighter pilot. Let's do that. Please enter a boy's name, we'll say Alan, enter a man's name, Fred. There once was a orange girl named Karen who was around plumber in the kingdom of Dearborn. She loved to wear jeans and dancing. She wanted to marry the blue fighter pilot named Allan, but her father king Fred forbid her from seeing him. So, as you can see this generates quite a wacky story that if we had seen ahead of time, we might have filled in with other types of interesting adjectives and words and such that would make it a little more normal. But since they're just being asked information ahead of time you can see how it's quite interesting and leads to some funny things like marrying the blue fighter pilot named Allan. So, it's quite funny. Now, if we close this out, I'm going to go back here and show you some of the information that you will need in order to finish the project. So, as you can see there are several variable options here that you'll need to keep track of. And then here's the story that you just saw, but with say adjective one would correspond with the variable adjective one. Don't capitalize them like this, this is just to help them stand out in the story so that you can see the story itself without me giving it away how I would do it. So, there's a couple of things that you might want to remember and one of them is to keep your work more compact. Remember that you can use the stream insertion operator to separate many liberals and variables even on the same line, working with the same Seattle. You can even break them across multiple lines in fact. For now, I'd like you to pause the video and see if you can make this program work. Come back when you're done or if you think you need some help. Welcome back. I hope you were able to get the program working. This one was maybe a bit challenging. So, if you accomplished it, congratulations. If not, it's totally okay. We will work on this together and hopefully it will make sense. So, let's get started with the project which we will call MadLibs Clone. So, I'm going to go to 'Create a new project' and we're going to make it an 'empty project' with C++. And we're going to name this MadLibsClone and hit 'Create'. I'm going to create a source file here by going to 'Add new item' and we're of course going to make this something like main.cpp. I'm going to include iostream and I'm also going to include strings since I know we're going to be using strings. So, namespace std, int man and then the skeleton right there. So, we've got quite a few variables that we're going to need. And we need an adjective 1, we need a girls Name, no apostrophe is allowed here guys. Adjective 2, string occupation1, string placeName, string clothing, string hobby, string adjective3, string occupation2, string boysName, and string mansName. Let's double check. Make sure everything looks like it's spelled right. Good. So, we got quite a few variables that we're using. Now we're going to intersperse this with our story. So, I'm going to do this, There once was a, so I'm putting a 'Space' inside the string literal here notice. And then I'm going to put the Adjective1 and then literal again 'Space.' Girl named another 'Space.' We have to keep the spaces in mind then Girls name, right there, who was a, then 'Adjective2.' And I'm going to go to the next line. This may be not be something that you have done before, but you can actually break this onto the next line. Now, if you would have put a cynical in here and started this line with another c:out, that's totally fine. That's absolutely fine. It'll print the exact same way. It's not a big deal. So, adjective2 a 'Space' and then occupation1, 'Space', in the kingdom of, space and then place name. Okay. And then there was a period, there. A couple 'Spaces', she loved to wear and then clothing. Well I hope she love to wear clothing, but we'll see which kind. So after that we'll say and to and then hobby. So, if the person or and rather hobby. So, if the person put dancing or something like that, swimming, so hobby. Alright. So, then we're going to put another period. She wanted to marry the, we have adjective3, a 'Space' and then occupation2 and then 'Space' named 'Space.' And the next line so we've got name 'Space' and then we'll just say boys Name. Alright, but her father king and then we'll need the mansName, that's our last variable 'Space' forbid her from seeing him. And then we can finally put our endl at the bottom. So, before we run this obviously, we've got the variables and we've got all this here but we haven't prompt the user yet. So, now that we have our story in place and we have the variables in place, I'm going to know prompt the user for each of these. So, this is going to take a little bit but not too bad. So, we'll say Enter an adjective and then we're going to use cin or get line rather with cin and adjective1, will prompt them for the girls name, Enter a girl's name, right there. getline cin and then we'll have a girl's name and then we're going to say enter another adjective. Keep it a little bit fresher than what we're saying. cin and then adjective2, cout Enter an occupation. So getline, we're going to take it from cin and go to occupation1. And then we're going to say Enter the name of a place, good and then getline. We have cin and placeName. Make sure that's good. Now we're going to getline is spelled wrong in a couple of different places here, fix that. And we are going to now extend that and I'm going to put cout, enter the name of a piece of clothing (plural). Cause if you remember the actual story goes she loves something to the effect of she loved to wear. So, dresses, jeans, boots, shoes, belts, you know whatever someone puts in, it'll be better if it's plural. cin, clothing and say Enter the name of a hobby. Getline cin and hobby. So, once we've added the hobby, we then need to add another adjective so, say Enter yet another adjective and we will do that endl. And getline cin adjective3, almost there homestretch. So, Enter another occupation. Okay be nice if I could spell, wouldn't it? Getline cin and then occupation2. And then last two enter a boy's name and then there will be getline cin boysName and then finally Enter a man's name; which of course boy's name and man's name are really different but maybe someone would think of a more gruff name and it might add some interest to the story. But we do have them separate in the story. So, let's see what happens. So, now let's see what happens if we 'Debug', 'start without debugging', if there's no errors it will launch which it has. And now we have enter an adjective. So, let's say fat, enter a girl's name, Sally. Enter another adjective, orange, enter an occupation, let me see here, we'll say dancer. Enter the name of a place Los Angeles, enter the name of a piece of clothing plural, we'll say hats, enter the name of a hobby, we'll say skipping rope or jumping rope, enter yet another adjective we'll say, wide. Enter another occupation, plumber, enter a boy's name, Fred, and enter a man's name, Warren. So, there once was a fat girl named Sally who was a orange dancer in the Kingdom of Los Angeles. She loved to wear hats and skipping rope. She wanted to marry the wide plumber named Fred but her father king Warren forbid her from seeing him. So, this is a really interesting story and it'll be interesting to try it on your friends once you've got it programmed and have them go through it without seeing the story ahead of time. So, always know that you can close the source file and keep the project open. You can still run it from here. You can also use the generated executable but we'll talk about that later in the course. So, hopefully you were able to solve that to some extent, or similarly to the way I solved it on your own. I hope that that was a good experience for you. So, that was a lot, though, as a side challenge that we won't go over formally here, perhaps you can come up with more stories for practice. And again, see if you can interact with the user and print out these different MadLibs, quote unquote Maybe try them on your family and friends and see how it works. So, that's enough for this project. I'll see you in later lectures.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.