Welcome back. I hope you were able to solve the challenge.
Let's go over it together, and I'll give you my solution to it.
This is a pretty simple challenge,
but it's important that you get it because we have to be able to compile, build,
link and run.
Obviously, we don't want any compiler errors, we don't want any compiler warnings.
So let's start here.
What I've done is I've created a challenge solution project based on the template.
And here's my main cpp. It's just a simple Hello World.
So this is the one I'm going to modify. All right.
So let me change a little bit of this. I'm going to get rid of that statement right here,
and we'll totally start from scratch. Now remember, we need to include
the iostream library. This is how we do input and output.
This is how we do our cins and our couts.
So first thing I want to do is I want to prompt the user.
I want to say std::cout,
and there's my insertion operator. I'm just going to simply say enter
your favorite number
between,
let's say, 100.
And we'll put a colon there
and a semicolon on the end. That will prompt the user.
Okay. Now we want to be able to read that number from the user.
Remember, when we read something,
we need to store it somewhere. So we need to store that in a variable.
In this case, it's going to be a whole number so we're going to use an integer. So I'm just going to say int
favorite number, just like we did with that first program we wrote.
So that's my favorite number, perfect.
Now I can read into that variable, so I can say std
from cin.
I want to extract so I'm using the operator going the other way
into favorite number.
Okay. So at this point, I've prompted the user.
They've typed in a number.
I've read that number, and I've stored that number in
this favorite number variable right here.
The last thing to do is to print out those two statements. So first, I'm going to say
cout,
and I'll say "Amazing.
That's my favorite number too."
And we'll print a new line here so we can go to the next line.
We'll say endl, followed by a semicolon.
And this is the only difference now between this program and the one that we wrote previously.
We want to be able to display the number. Say no really.
Whatever you entered was or is my favorite number.
So in this case, let's do that again. We'll do std,
and we'll do again cout, and we're going to write to that stream,
and I'll say "No really!"
And I'll put a comma here followed by a space
because we want this to be spaced out nicely.
And I'll show you what the problems may be if you don't do that in a second.
And at this point, we're going to insert our favorite number right in here.
And we'll do another insertion operator.
Remember, we can stream all these things together we can do as many as we want.
And we'll say space is my favorite number.
And we'll finish it off with a new line.
Let's try that. We'll go up to build,
run and build and execute.
We've got a clean compile and it's running.
Enter your favorite number between 1 and 100. Let's say it's 10.
Amazing, that's my favorite number too.
No really. 10 is my favorite number,
perfect. That's exactly what we wanted to solve.
Now the reason that I put in these little spaces here is because let's say I don't
put that space in there and I don't put the space in here
just like that. And let's run this again now. You can see that
the output is not going to look right.
So in this case let's say I typed in 25
and you can see amazing that's my favorite number too, that's fine. But now look, no really
comma 25. No space between 25 and the little I for
and the lowercase I for is
my favorite number. So you can see that the output doesn't really look very nice.
So all we're doing is just formatting it.
We'll talk more about writing to streams and using manipulators so that we can really get
more fine grained control of what we want this to look like.
But for now we, could just put spaces right in here.
And we'll try that again. Build and run.
My favorite number is 10.
No really, 10 is my favorite number, perfect. So that's it.
And that's that's the solution to this challenge.
As I said it's a pretty simple challenge,
but there's a lot of pieces here that come together.
In the next few sections, we'll talk about the structure of the c++ program
as well as variables. We'll talk a lot about variables
how we declare them, how we define them, how we initialize them and how we use them
but you got a taste of it here.