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.
Learning Objectives
- 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
Intended Audience
- 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++
Prerequisites
This is a beginner-level course and so no prior knowledge of C++ is necessary.
In this lecture, we will complete the content for this section before tackling a couple of projects. We have previously worked with a lot of data in the form of variables and values and also literal constants and symbolic constants throughout this section. However, the data was hardcoded, meaning we wrote the values directly in the source code. Although hardcoded data is something we do use, we also want to be able to obtain data from outside our applications, such as, from the user. The standard input device for most users is the keyboard and that is what we are going to obtain data with. So, let's create a new project called KeyboardInput. So, 'create new project', 'Empty Project', C++, KeyboardInput. As usual, we will create the skeleton project in main.cpp. So, include iostream using namespace standard int main return 0. So, we will use the cin object with the stream extraction operator for some of our input and we will also look at the get line function often used with strings. Let's start with asking the user to input their age. So, we'll start by declaring an integer variable for the age. For obtaining the user input, we will use this cin object which will have the stream extraction operator which looks like the stream insertion operator but has the arrows in the opposite direction, so that the user knows what we want, it is very common in a command line application to prompt the user for input first. So, we will explicitly ask the user to enter his or her age. Enter your age and then cin age. Once the user hits 'Enter' the data from the keyboard will be placed in age. Now, let's echo the age back to the user, that is, make it repeat whatever we input as age. Now, that this is done let's run the program 'start', 'without debugging' under debug and it says "Please enter your age". So, let's put 28. Now, you'll notice it's repeating the 28 underneath. So, I entered this and then it echoed or repeated the information for me underneath. So, that was your first real two-way interaction with the user. Congratulations. This is just the beginning of some amazing things you're going to be doing so stay diligent and practice. Now, let's add some code to ask the user for their full name before the age. So, let's put the code there. So, we need to have the include string and we will create a string here called fullName and we're going to prompt the user before the age, say "Please enter your name". And then, for this though, since it's a string we have to use something special if we want to grab the full string we want the fullName to include the first and last names so the given name and the family name. So, the reason we have to use get line is because with cin and the stream extraction operator, as soon as any kind of white space is encountered including just a simple space, the code considers that a terminator. In other words, a space indicates that the data is complete just before that space. With a full name, which includes a space, we need to use get line. The get line function will only terminate when it encounters a new line character. In other words, it will know to wait until the user hits enter. So, anything up until the enter key is pressed will be included in the data that is stored. We haven't covered functions except for the main function, for now I will simply say something and you can glean the meaning from context. We will certainly cover functions much more in depth later on, but the get line function takes two arguments separated by comma. So, getline cin, fullName and I'll explain that here. The first argument to get line is cin. That indicates the source of the data and the second argument in this case, fullName, indicates where the data is to go. You might call the second argument the sync of the data. So, we've got the appropriate code there, now let's echo the data back to the user. I'll do it near the bottom right here. I'll say fullName. So, let's run this and see what happens. So, we can put our full name here, John Baugh and let's put age of 26, I wish, and hit 'Enter' and you'll notice it is echoing John Baugh right here because of that and then 26 on the next line because of this. So, that's exactly what I expected. Now, I'm going to issue you a challenge and I want you to create a brand new project called Percentages. I'll write the basic instructions in a simple notepad text document to display on screen because this one maybe a little more difficult just to remember. So, here we go. Bring up notepad and this is the Percentages project and I want you to do the following; so, create a fullName string variable, create a location string variable, create an integer named initialScore, then I want you to prompt the user for their full name which should be stored in the fullName variable. Then, you want to prompt the user for their city followed by their state/province and country and store that in the location variable. Then, ask the user for an integer 0 to 100 to be stored in initialScore. At the very end, at the end, print out the following so what are we going to do? We're going to say "Hello," and then followed by fullName, "We heard you are from, and then whatever location is, then your original score is initialScore but with five points added, your score is, and then you're going to print out the initial score plus five points. So, what I want you to do is pause the video and see if you can accomplish this task. Did you get it? Let's create the Percentages project and solve it together to see if you get something similar to what I get. So, I'm going to do file close to close the current project, 'create a new project', 'empty', C++ and we're going to just call this Percentages and I think you're pretty familiar with what we need to do here. We need to make our main.cpp and we always start with our skeleton code and this one I know we're going to need the string library so I might as well just include that now. And we're going to include of course the entry point to our application which is main. So, let's see what all we need. We need to create a full name, location, and initial score. So, string fullName, string location and then int initialScore. So, that's the first part. Then, we need to prompt the user for their full name than for the city, state/province, and country and then prompt them for the score from 0 to 100 for initialScore. So, "What is your full name?" And we're going to use getline from cin to be stored into fullName, then we might say "What is your city, state/province, and country?". And we want getline again cin and then we'll put location and then finally we'll say, "What is your project score? 0-100". So, for this one since it's an integer, we'll just put, use the stream extraction operator to store it into initialScore. We have to make sure that we echo the information back to the user because that was part of the project. So, we'll say, "Hello", followed by the full name. "We heard you are from". So, let's start with that. We'll say, "Hello, " and then we'll say the fullName end1 and then we'll say, "We heard you were from " with a space location, right? And then the last part says, "Your original score is initialScore. But with five points added to your score is. So, you say, "Your initial score", initial score or original score is initial score and then we can say, "But with five points added, to your score is now " and then we'll take the initial score + 5 in parentheses. So, that will do some arithmetic for us directly in here. Put the parentheses around it just to make sure that it doesn't think we're trying to do any kind of string concatenation and just to make it clear to anyone reading this that this is what we want right here. So, let's run this and see what happens. So, 'Debug'. 'Start without debugging' and let's see here what is their full name, John Baugh, city, state, and province we'll say Dearborn, Michigan USA. What is your score? We'll say 85. Hello, John Baugh, we heard you were from Dearborn, Michigan USA, your initial score is 85 but with five points added, your score is now 90 and that is exactly what we expected. So, nice work everyone. Before we end this lecture though, I want you to do something interesting to see a little quirk that you're likely to encounter. I want you to put the prompt for the initialScore at the top of the other prompts along with the obtaining of the data. So, let's get this prompt here for the initialScore. I'm going to cut that and put that before the others. There's a reason for this. So, hopefully it'll make sense in a second. Now, let us run this and see if we get some unusual behaviors. So, I'm going to start this without debugging and says, what is your score? 85 I had entered. Now, you'll notice it says, What is your full name? And then, it immediately goes to what is your city, state/province, and country. Well, that's Dearborn, Michagan USA but you see that when it echoes the data back it says, Hello, and then it's empty. We heard you were from Dearborn, Michagan USA, your initial score is 85 blah blah blah, but the interesting part is it missed the name. So, what's actually going on here? Why did it skip that? Well, here's what's going on. The answer is that when you hit enter after entering a string, getline consumes the new line character. In other words, it removes it from the input stream. You can think of the input stream as like an actual stream where the data is flowing and anything that's left in it has to be or will be handled in most cases at some point later on. Since get line consumes the new line character, these are okay in general, but right after we prompted for an integer, the stream extraction operator does not consume the new line character. So, when the person hits enter here, enter is entered into the data stream and then it will prompt the user but right immediately as it gets to the get line, it will see in the cin that there is a new line character. So, it'll say, they must be done, so then it goes immediately to the next prompt. So, we have to be able to fix this in some way. So, any time you ask for a string, after you ask for an integer or any non-string, you have to manually consume this new line character. So, to do that, we're going to solve the problem with something called cin.get. So, what this does is it normally just gets a single character from the input stream and it will return that data but we're not interested in catching it, we just want to make sure that it consumes it. So, this should take care of it. Let's see what happens when we run this project. So, debug start without debugging. What is your project's score? 85. What is your full name? John Baugh. What is your city, state, and province? So, Dearborn, Michigan USA and now we've got our data back the way we expected it. So we have consumed the new line character. If that was a little confusing, don't worry, the more experience you get going through this course and practicing on your own, the more you might encounter situations like this. If you forget how to solve it, you can always come back to this introductory lecture on taking user input. Congratulations, if you've made it this far. Up next, we will be doing a couple other projects to help solidify your knowledge that you've gained already in this chapter. I'll see you there.
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.