Getting Started with C++ on Windows: Part II

(1/1)

Richard Marks:
Getting Started with C++ on Windows: Part II

Welcome to the first lesson.

I assume that you have already downloaded and installed a C++ compiler. If you have not, click here to read Part I.

In Dev-C++, we need to create a new project.

Click the File menu, and click on New -> Project.Click on the Console Application and enter Lesson1 for the project name.Click the OK button.
In the window that opens, you need to create a new folder to hold your projects.
I recommend calling the folder Projects.
Double click on the Projects folder once you have created it, and then click the Save button.

You should see this in the window now:
Code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}


I want you to delete all of that code and type this:

Code:

#include <cstdio>
#include <cstdlib>
#include <cstring>

int main(int argc, char* argv[])
{
printf("Hello World!\n");
fgetc(stdin);
return 0;
}


Don't worry, I will explain all of that later. For now, just know that you need to memorize that code. ;)
Now save your file in your Projects folder as lesson1.cpp

Click the File menu, then Save All to make sure that your project is saved.

Press CTRL + F9 to Compile the lesson1.cpp file.
You should not have any errors. If you do, then you did not type in the code exactly as I said to.

Press CTRL + F10 to Run the program that you just wrote.
You should see a black DOS Prompt window open with the words "Hello World!" written on it.
Press ENTER to close the window, and you should return to the Dev-C++ window.

Congratulations on writing your first program!

Now, let me explain what all the mumbo-jumbo that you just wrote is.

Firstly we have the Header File Inclusion Directives.
Code:

#include <cstdio>
#include <cstdlib>
#include <cstring>


What this does is gives your program access to the special functionality that each header file provides.
I will not go into any details on what each of the header files are for, since it will just confuse you right now.
Just know that those are a few that you will always use in your programs.
Memorize the 3 lines.

Next we have what is called the Main Function.
All programs must have a Main Function.
Memorize this:
Code:

int main(int argc, char* argv[])
{
return 0;
}


Anything that your program does will be done inside the braces of your main function.

Our program that we wrote as you saw when you ran it, just writes out "Hello World!" on the screen and then waits for you to end it by pressing ENTER.

We use two functions to do this.

The printf function is used to write text on the screen.
There are a lot of neat things that you can do with the printf function, but I will not go into those things until later.

The fgetc function is used to read a single character from a FILE.
We pass a special FILE called stdin which means Standard Input or in our case, the KEYBOARD.
So, what we are doing is reading a single character (key press) from the keyboard.
This is a simple way to make our program wait to close.

That is all you need to know for now.
Make sure that you understand everything in this lesson before you move on to lesson 2.

Stay tuned...

Navigation

[0] Message Index