Let's start our tutorial with our first program. Type this in your C++ compiler:
#include<iostream.h> main() { cout<<"Hello, World!"; return(0); }
Then, use the compile feature in your program, it should make a "exe" file out of what you wrote. Run the "exe" program you created and it should display "Hello, World!" on a black Ms-Dos screen.
If the screen flickered when you ran the program, type this code instead:
#include<iostream.h> #include<conio.h> main() { cout<<"Hello, World!"; getch(); return(0); }
It should then work fine.
Now, let's dissect the program we created in the first tutorial. At the start, you wrote "#include<iostream.h>". With every compiler comes a library of header files which help you create programs. iostream is a library which helps you with some input/output aspects of your program.Then you wrote "main()" which declares the start of the main program, then the brackets " { } " indicate the stat/end of the program. Then you wrote "cout<<" which is a output operator in the iostream.h header file (get the use of the headers now? ) which lets you output data to the screen. Then you enclosed "Hello, world!" in quotes and put it after "cout<<" , which outputs "Hello, World!" to the screen. Start to get the hang of it? Then comes "return(0);" which ends your program. We'll discuss the use of "return(0);" later.
If you wrote the second version of the program,it's because you needed the function "getch();" , which means "get character" because your program finished his duty quickly and exited the program, while "getch();" makes the program wait for the user to press a key (character). This is a frequent problem depending on your compiler.
|