Home | Mini-Bio | Game Progress |
Creating a Console Application |
|
To begin this tutorial, one must first realize what a console application is, and why we are starting with it. A console application is a DOS program. We are starting with it for the purpose of learning to use the compiler, and learning basic code. |
![]() |
Now, to begin, open your C++ compiler. Once it's loaded, close the Tip of the Day if it pops up. Now select File->New. Choose Console Application, name the project, change the destination folder if you wish, and click on OK. Make sure Empty Project is selected, and click Finish. Finally, click OK. |
|
Creating Output Code |
|
This created your first project! You should now have an empty project sitting on your screen. So now, what do you do with it? Let's check out the workspace window on the left. (If you do not see the workspace window, locate the workspace button to the right of the redo button). Click on FileView, and expand the workspace by clicking the plus sign. As you can see, there are no files in this workspace yet. Click on ClassView. If you try to expand the workspace here, you will find nothing! Why don't we create a file? |
![]() |
To add a file to this workspace, go to File->New, and select a C++ Source File. Make sure Add to Project is checked, name the file main, and click OK. You now have a file in your workspace! But ClassView is still empty... Why is that? |
The solution to this problem is a quick bit of coding. Type this into your new source file: #include <iostream>using namespace std; int main() { cout << "Hello world!\n"; return 0; } Press F7, or click the Build Button. The output window should pop up on the bottom. This window will tell you if any errors are present in your program. If you copied and pasted the code from this tutorial, you should have no errors. |
Let's look at the ouput of the program: ![]() If your output matches mine, congratualation! You've successfully programmed your first program. Now you get to learn exactly what you did. |
|
Let us examine the first line of the program: This line tells the compiler to include the library file called iostream. This line is known as a preprocessor, as indicated by beginning the line with a #. |
#include <iostream> |
This line tells the compiler to look in the namespace titled std. This namespace stores many of the input/output functions assocciated with console application programming. |
using namespace std; |
This line creates the main method. All console applications need this method to function properly. The code for the main method is between the curly braces ({ and }). |
int main() |
This line uses cout to display output to the screen. cout is found in the iostream library, and accessed through the std namespace. If you had not added the line using namespace std;, you would have had to qualify cout like this: std::coutThe double "less than" signs are the insertion operator. Whenever you see the insertion operator, you know that there is output being generated. All output is placed to the right of the insertion operators. To output text the way we did, you must use double quotes around all you wish to output. The last two characters within the double quotes is a special character unto itself. "\n" causes the program to move to the next line. It is one of many of these special characters we will be using within these tutorials. |
cout << "Hello world!\n"; |
This line tells the compiler that there are no errors in the code. The compiler will then flag any possible syntax errors or warnings at compile time. | return 0; |
Creating Input Code |
|
Let's add some more code to the program. Now we will learn how to get input from the keyboard. Add the red colored text to your program: #include <iostream>#include <string> #include <cstdlib> using namespace std; int main() { string name; cout << "Enter your name: "; getline(cin, name); cout << "Hello world! My name is " << name << endl; return 0; } |
![]() |
Let's examine the new code: We have two new library files we needed to include, string and cstdlib. string allows for the storing of entire words or sentences. cstdlib allows for us to get input with spaces through the getline method. |
#include <string> #include <cstdlib> |
This line declares a new string object called name. This is done in just the same way you declare a regular variable. We will discuss variable declaration later. |
string name; |
This line simply displays a message asking for input. |
cout << "Enter your name: "; |
This line gets input from the keyboard. This particular method allows for entering strings with spaces in them. It takes two arguments. The first tells the method to use the keyboard for input, the second tells where to put the input. |
getline(cin, name); |
The original output line has been modified to allow us to output the name string. The insertion operator separates the different things that are being outputted. endl is another way to move the cursor to the next line (same function as "\n"). |
cout << "Hello world! My name is " << name << endl; |
Thus ends the first tutorial |