Hello those who are new to C++! Today we are going to learn how to make the very simple "Hello World!" application.
A "Hello World!" application is an application that simply displays the text "Hello World!" in the Windows Console.
To get started, make sure you have Dev-C++ or any other compiler with the header files installed.
First, we will need to declare the headers for our program.
#include tells the computer to find a 'header file' and use it to make compiling easier for you!
<iostream> stands for "Input/Output Stream" and will tell the computer to load the iostream header.
Next, we will start off by making an 'int main' function. We will get into functions later on, but for now, you will use this for your main code. This is why we call it the 'main' function! :D
To do so, we must add this into our script:
Our code should now look like this:
| CODE |
#include <iostream>
int main () { }
|
Now, the 'int main' function will allow the computer to run the MAIN code for your program. Later on in more advanced tutorials, other functions will be used in order to save room and compute data easily.
For now, we will simply use 'int main'.
Now, if you were to compile this, nothing would happen, and the file would simply close!
So, to make text appear, we will need to add what is called a cout statement.
cout statement is a code that tells the computer to grab the string we tell it to and display it on the screen for the user to see.
So, we want to display "Hello World!".
To do this, we must use << operators.
We will add this code:
| CODE |
| cout << "Hello World!"; |
cout tells the computer to get a string and display it on the screen.
The << is simply an operator that is used with the cout statement.
"Hello World!" is a string, for it is text within quotations marks ("") and is what we are telling the computer to display.
The ; at the end is used to tell the computer to STOP using the cout statement and go to the next line of code. If you were to leave this out, you would get major errors!
Now, we will add the code from before into our code, like this:
| CODE |
#include <iostream>
int main () { cout << "Hello World!"; } |
Now, we are having a small, tiny problem. If you are to try and compile this, and run the program, the console will open up, display the text for a millisecond, then close the window, without you able to do anything!
So, how do we fix this problem, you may ask?
Well, we will simply use the getchar statement.
The getchar statement is a function that tells the computer to wait for user input.
We will use this code to make the text stay on screen until the user inputs something into the program:
getchar() tells the computer to run the function 'getchar' which is in a header file that we included before.
Again, the ; is needed to tell the computer to stop and run the next line.
Now, we will add the code above into the rest of our file.
| CODE |
#include <iostream>
int main () { cout << "Hello World!"; getchar(); } |
And now we're done!
You can compile and save the project, and feel free to post it on the Release forum.
Here is what the program will output:
Think you messed up DDM, I keep getting this...
| QUOTE |
6 tommy's first c++.cpp implicit declaration of function `int getchar(...)'
|
And my script looks like this
| QUOTE |
#include <iostream>
int main () { cout << "What the fuck hax, OMG PSPCube FTW!"; getchar(); } |
Wow..wtf. That's a really weird error.
That didn't happen to me.. Give me a few.
Add #include <cstdlib> at the very top with the other include...
That probably won't do anything..That's just a default header file.
I don't know what is wrong with his though.. :S
Can't hurt, also could be some installation problems.