By the end of this tutorial you should have created a program that looks like this (maybe 'hi' was friendlier):
This tutorial uses visual studio 2008; you can use the free 'express' version of visual studio/visual C++ obtainable here:
First we need to create a new blank project, if you know how to do this skip to step 2.
1. First go File -> New -> Project...
Select the 'Project type' as Win32 and choose the Win32 Console Application template.
Fill in the relevant information and click 'OK'
Fill in the relevant information and click 'OK'
Click next on the 'Application Wizard' and then tick the 'empty project' check box before clicking finish.
2.Now we need to add a new source file to the project, this is where your 'source code' will go.
Go to the solution explorer on the left hand side of the screen.
Right click 'Source Files', then Add -> New Item...
Select the 'code' category and create a new C++ File; we've called ours main.cpp, it will automatically be set as the initial code to be run.
Now we can add our code:
3. The first bit of code we want to type in is:
int main () {}
int main () {}
This is a global integer 'main'; the project is executed inside this main function.
The next bit of code we need is:
return 0;
return 0;
This sets the value of main to 0 to tell us the program has exited correctly.
Remember to add the ';' this shows the end of a statement the program will not run without it.
Remember to add the ';' this shows the end of a statement the program will not run without it.
You can now run the program using F5 key; the program should build and in the output you should get:
This exit code 0 means the program has terminated successfully.
Now we want to add:
#include <iostream>
As this is an include we do it above everything else before 'int main'
#include <iostream>
As this is an include we do it above everything else before 'int main'
This includes the 'iostream' header file, it's included in visual studio and adds input and output functions.
Now we should add to our body of code before the return:
std::cout << "hi";
std::cout << "hi";
This tells the program to output hi to the screen.
Your code should now look like:
Run your program you should have no errors, but the program will end before you can do anything.
To stop this we want to use the 'Sleep' function.
This is included in the windows header file, add that with:
#include <windows.h>
Then add to the body after the text:
Sleep(5000);
The sleep function is in milliseconds so 5000 is for 5 seconds.
#include <windows.h>
Then add to the body after the text:
Sleep(5000);
The sleep function is in milliseconds so 5000 is for 5 seconds.
The program will not do anything for 5 seconds so it won't terminate until then.
Your code should now look like:
Build and run this (F5) and you'll get the nice program looking like the top of this post.
No comments:
Post a Comment