C++ provides the ability for the user to interact with the program. You can send messages to the console and the user can enter data which is then read from the console.
- [Narrator] C++ provides the ability for the user to interact with the program using read and write commands that allow us to read and write data to the console. We can send messages to the console and the user can enter data, which is then read from the console. All right, let's create a project, and let's take a look at how we can read and write data. I'm going to do file, new project. I'm going to do a console application, and let's just call it read and write.
And I'm going to put it in my C++ essential training folder, and I'm going to call it read and write. All right, at the very top of my program, I want to be able to read and write strings in addition to numbers, so I'm going to go ahead and add the string library. I'm going to do pound sign include string, then I'm going to go ahead and do using name space std. By including the IO stream header, we can use the C in and C out commands along with the extraction and insertion operators.
This makes our programs much more flexible. Let's write a program that prompts the user to enter some information, read that data into variables, and then I'll print out a message. So let's go ahead and create some variables. One, we're going to ask the user to enter their name, and enter the year they were born. We'll use that information to calculate their age this coming year and we'll print out a message. So, let's start by creating a string for their name. Then I need an integer variable to hold the year.
Now in order for the user to know what we want them to enter we have to prompt them or we have to put a message out to the console, so they know what to do. So in that case, we use the C out command, which is console out, and then we use the insertion symbol, which is two less than signs, and then we can put our message. So I'm going to say enter your name. And below that, so when they enter the name, now I have to read it in.
So in order to read it back in again, now I'm going to use the C in command to read from the console. I'm going to use the extraction symbol to take the data out of the console, and put it into, in this case, a variable name called name. Now I'm going to do the same thing and ask them to enter in the year they were born. And I'll read that in. And I just called it year.
All right, now we have all the information we need. Now let's go ahead and take that information and create a sentence that tells them how old they're going to be this year. So again I'm going to use a C out to print it out, and I'm going to print out their name first. If I want to print out multiple values, whether they're literal strings, or variables, I can do that by repeating the insertion operator. So now I have name, and then another insertion operator, and then a message that says you will be, and then another insertion operator, and I'm going to take the current year minus the year they entered for their birth year, and a final message say years old this year.
And I'll add an end l just to make it look nice. All right, and then we have our return zero. So, let's go ahead and run this, and we'll see what happens. So I'm going to compile it first. Make sure I don't have any errors. And I'm going to call it read and write. All right, zero errors, let's run it. And the firs thing I'll do is enter my name. Then I'm going to enter the year I was born. Let's just say I was born in 1900. I look good for my age.
All right, and it says Peggy you will be 117 years old this year. Okay, it looks like it's working. So this works great, but there are times where you might want to read in more than one word at a time. Maybe if I wanted to ask the person for their full name. Well, let's do that. Let's just change this to say enter your full name, and I'm going to read that into my name variable, and I'm still going to read in the, the year they were born, and I'll print it out. So let me go ahead and run this again. I'm going to do compile and run at once, and my full name is Peggy Fisher, and I'll hit enter.
Well, wait a minute, it didn't ask me for the year. Well, it did, but I didn't get a chance to put it in. So what happens is when you read in using the C in command it reads until it hits the first white space character. Now a white space character is a space, a tab, or even an end line symbol. So in this case when it hit the space after Peggy it stopped and it only put Peggy into the name, and then it put Fisher into the year, which is not going to work when it goes to calculate how old I am. So, the IO stream library does give us the ability to read in a string, or a line, of information.
So let's change our program to accommodate this. So instead of C in name, I have to change this statement here. So I'm going to comment this out, and right below it I'm going to add a new command called get line. It's all lower case, so I'm going to say, get line and then in parentheses I need to tell it where to get the information. So it should get it from the console. So I'm going to use C in and then a comma, and now I want to tell it where to put the information that it does find, and I want to put it into the name variable.
Okay, now what'll happen is it'll ask me for my name. It'll read that line of information until it hits an end line symbol. So when I hit enter, it will allow it to go to the next line and ask me for the year. So let's try this. Okay, and I'm still going to do Peggy Fisher, and this time, there we go, now I can enter my year. So let's pretend that I was born in 1985. It looks like I'm going to be 32 years old this year. Okay, that works really good, and the get line will work for us.
There is one caveat in that if you try to use C in to read in a number followed by the get line to read in a string, what'll happen is it's not going to work the way we expect. So it's a little confusing to explain, so let me show you an example. So what I want to do is I want to reverse this to say enter the year first. So I'm just going to move things around a little bit. I'm going to say okay, first I want to do the C out, enter the year you were born, and then I'll read that in, and what I'm going to do is I'm going to say enter the year you were born, then I'm going to say comma, and your full name.
So they get the message all at once, so now I don't need this C out statement here. So now I'm still going to use the get line. I'm just going to move it up here. So what's going to happen is it's going to read in the year into the year variable and then it should get my full name using the get line statement. Now, let me run it, and we'll see what happens. So I'm going to do compile and run. All right, so it wants the year I was born. So I'll do 1985 again, and I'll hit enter. Notice it didn't stop to allow me to enter my name again.
So that's a problem. So what's happening here is that when we use the C in command, and if C in is used to read a number, when I entered 1985 I hit the enter key. The enter key is actually putting an end line symbol into the buffer, and again, it's a white space character that we don't actually see. So it's in the buffer, but it did not put it into the variable called year, so it's still in the buffer. It's just waiting to be read. So when the program got to line 12, and it said get line, go to the C in buffer, and go until you hit an end line symbol, it says oh, here's one, and it put the end line symbol into name so it thought it already had the name.
All right, so there is very easy way to fix this. What you want to do is any time you're using C in to read in a number, and if you want to try and read in a string next, you have to add a statement called C in.ignore. There's lots of ways to do this, but this is probably the easiest way. This command on line 12 will ignore that end line character and allow the get line to wait for me to enter my full name. All right, let's compile and run it again. So I'll put in the year, again, and now it's waiting for me to enter in my full name this time.
So now I can enter in my name, and Peggy Fisher you will be 32 years old this year. So that is now working again.
- Compiling and running C++ programs
- Creating variables in C++
- Choosing the correct data type to represent variables
- Creating assignment expressions
- Changing data types in C++
- Comparing values in C++
- Using logical operators
- Comparing strings in C++
- Defining and calling a function
- Using arrays in C++