Before I begin, here are some things I hope you have figured out:
-you have a C++ editor and compiler(get Dev C++ if you do not have one
www.bloodshed.net )
-you know where to type your code
-you know the basics of using your compiler and editor
-I am using Dev C++ 5 Beta 4.9.9.2 so I will not be familiar with questions pertaining to Visual C++ or other editors
-you know you are free to ask any questions
With that said, let's begin!
First, open Dev C++. Select new project. Pick the Console option. You should see your editing area changed with a few lines of code. That's normal. Let's start with a basic action-Displaying words in the console.
In order to have the console print words and numbers, you have to use the
cout action.
An example of this would be: cout << "I have begun my journey into C++" << endl;
Pretty basic, but where would that go?
Well, you'll notice that your program already has several codes already. Do you see the int main(int argc, char *argv[]) line? Good. Just under that,you will see the swirly bracket or {. That is the beginning of your program. If you want your program to do anything, it must be after that bracket but before the closing } at the end.
So lets try this:(Your program should look like this...Also, whatever I put"[b and /b]" around is what you need to type in)
#include <iostream>
#include <stdlib.h>
int main(int argc, char *argv[])
{
[b]cout << "I am beginning my journey into C++" << endl;[/b]
system("PAUSE");
return 0;
}
Let's break that down:
You typed in the cout action. Notice that it is lower case. C++ is case sensitive. cout must be lower case. So we have a cout action telling the console to print "I am beginning my journey into C++". Whenh you want cout to just print words, they must be enclosed with double quotes. If they are not, then you compiler will be looking for strings or ints or other things that match whatever you typed.
We ended our cout with another set of << but added endl after them. That just tells the program to start at a new line. Some also use the \n to make a new line. Such as cout << "Hello \n"; It is just a shorter way of moving to the next line.
We end the whole line of code with a semicolon or ;. You MUST MUST do this for just about every code. If you do not, then your compiler will give you an error. If just shows the program that that is the end of you line of code.
So, what are you waiting for? Goto Compile and Run and see what happens...
You should see a ms-dos window open with the words "I am beginning my journey into C++"
It also says press any key to continue. You do so and the program closes...Why is that?
Well, back in your code, you have the line:
system("PAUSE")
That is basically forcing the program to stop and wait for you to push any key to continue with the program.
Last, you have the return 0 code.
That just tells the program to return a value of 0 which ends the program.
Mess around with the cout action a bit until you are comfortable with it. Try typing more than one command of it and see what happens. Until next week, have fun with coding!
~Theforeshadower