In this video, we'll write our very first program.
So what we want to do is we want to create a project.
So again, I'm in the section 4 workspace, and I'm going to right click on there
and say new, new project.
I'm going to select my default template again.
And I'm just going to call this first program.
And I'll select next and finish.
Now notice that project1 is the active project so I want to be sure that
first program is the active project. So I could just double click on, it
and I can open up that c++ file. That's it.
So this is the source code for the first program.
Now what I want to do is I want to erase this and I want you to really just type it in
that way you get used to typing these things in at the beginning that's probably the
best way to do it is just to follow along.
So let me tell you what this program is about.
This program is going to ask the user
to enter their favorite number, a number between 1 and a 100.
And then it's going to display.
Wow, that's amazing. That's my favorite number too. That's it. Okay.
So let's start. First thing we need to do is we need to do pound include
of iostream.
Now as I type, I'm going to press io then when I press s,
you can see that the code completion is helping me out is trying to figure out what I want.
And it's giving me its guess as to what I want.
And I want iostream, so I'm just going to select it and close it.
There is no semicolon here on this statement.
These pound directives, and we'll talk more about what these preprocessor directives are
in section 5, but they don't take semicolons at the end.
So I'm going to press enter. And now we want to create our main function.
Remember, the main function is the main entry point.
This is the place where execution begins.
So we're going to say int main left paren, right paren.
And then we're going to put a left curly
and the IDE will terminate that left curly with the right curly.
We'll talk more about style as we go, but
some programmers like to line up their braces like this,
others like to line them up like that.
Right now, it doesn't matter. Let's just line them up like this.
All right. So what do we need to do. We need to prompt the user to enter
their favorite number. So how do I do that? I do that with a cout statement.
So I'm going to say std::
cout,
and we'll do the insertion operator which inserts something into that stream.
Remember, cout is typically tied to your console, your terminal,
so the user can type things in.
And we're going to display,
enter your favorite
number between,
let's say, 1 and 100.
And I'll just put a colon there.
Now you'll notice that you've got a quote mark
and then a terminating quote mark. This is called a string.
This is called the string literal.
This will be displayed exactly as is.
Now this is a statement, so all statements end in a semicolon.
Remember, main returns an integer
and that integer is 0 if everything is good. So I'm going to return 0,
right. So if i run this program now, it's all -- it's going to say is enter your favorite number
between 100.
That's it. It's not going to grab the number from anywhere,
it's not going to display anything else.
We need to somehow read what the user types in on the keyboard.
So just like cout,
writes to the console cin reads from the console.
So what we're going to do here is we're going to say std
cm,
and now we're going to use the extraction operator because we want to extract information.
And it's the same as the insertion operator except going the other way.
But now what we're saying is we want to grab whatever the user types in and we want to store it somewhere.
We where do we store that?
Well, this is where variables come in. And we'll do
a lot of talking about variables in section 6.
I'll talk about how do you declare them, how you initialize them,
the rules for naming them and so forth.
But declaring variables is very straightforward. So let's do that here.
What we do is we tell the compiler. Remember, the compiler is really dumb.
It's not guessing it. It does exactly what we tell it to do.
So what I'm going to do is I'm going to tell it that I want a variable name
and that name is called favorite number that's where I want you to store what the user typed in.
And you also have to tell it what kind of data that is.
Is it a string? Is it some big huge structure?
No, in this case it's just a whole number an integer.
So we're going to say int
favorite_number
;
that's it. That's a variable declaration.
You've just told the compiler that this variable name right here favorite number is an integer.
Now I can read
information into that, so I can say std::
and again the std will explain later on, it's a namespace
but we definitely need it here stdcn
into favorite number.
Now again, I'm going to type fav
and the code completion will try to figure out what I want,
so it's going to use favorite number I can just press enter
semicolon on the end.
So notice what we've done so far, we've included iostream.
Why did we include iostream because we need the input output libraries.
We can't do IO without it. So we're including it.
Now the compiler understands
about input and output. It understands cout and cin.
They are defined in here.
Great, so I'm outputting enter your favorite number between 1 and 100.
Now the user is going to type something at the keyboard.
And whatever they type, we're going to store it into favorite number,
this guy right there.
Right now we just need to say
cout
and I can say something like amazing.
That's my favorite number too.
Here we can do an end line, so we
flush the buffers and print out to the next line.
That's it. So let's go through this again one more time.
We've got a main function. This is the entry point. This is where execution begins.
All c++ programs must have a main function.
All c++ programs must only have exactly one main function.
So when the program begins to execute, this is where it starts.
Here I've defined a variable called favorite number
and it's an integer, it can only hold whole numbers.
I'm outputting to the console.
Enter your favorite number between 1 and 100,
the user is not going to input something into the console a number.
And I'm going to read it and store it into that location into that variable, into that box.
Then I'm going to say cout, amazing. That's my favorite number too.
And I'm just going to print end line at the end that'll flush out the buffers
and move the cursor over to the next line.
Finally, I return zero.
Everything's good.
Now next thing to do is to build this program.
Program is not going to run up by itself. We need to compile it. We need to link it.
And then we need to execute it. That whole process of compiling
and linking is called building.
Now you'll notice that when you right click
on that source file right here main cpp,
you see it says compile.
Now if I press that,
it's going to compile our program.
And you can see here 0 errors, 0 warnings.
We'll talk about what happens if you get errors and warnings a little bit later.
Also notice that we're using the c++14 standard,
which is what we want to use.
That's what we did when we set up that default template if you recall.
What we need to do next is to build
the project by compiling and linking
that's going to create the exe file on windows
or the executable files on Linux and mac.
And we'll do that in the next video.