C++ PROGRAMMING INTRO
Alrighty, I'm gonna break down a program I wrote, and explain a few details of C++ . Hopefully, when you're done reading this, you'll be able to make you're own little programs.
Before you even start learning, you're going to a compiler and linker. An all-in-one program is the best. I would suggest DEV C++ . Unless you wanna spend a couple hundred of dollars on Microsoft Visual Studio. DEV C++ can be found at Bloodshed.net. To start a new project, click Fil => New => Source File
You should get a blank text entry screen. Thats where the code is gonna go. After you input all of the code in your program, you're gonna go to Execute=>Compile. If you don't get any error messages(I say any, but you may get some that don't interrupt the program from being compiled), click Execute=>Run.
Now, first, the code
- Code:
-
#include
#include
#include
int main()
{
int integer1;
int integer2;
cout <<"Use this program to add two integers together\n";
system("PAUSE");
cout <<"Input number 1\n";
cin >> integer1;
cout <<"Input number 2\n";
cin >> integer2;
cout << "The answer is:" <<< "\n";
system("PAUSE");
return 0;
}
Now, this code results in the command prompt popping up. It tells me that "this program is used to add two integers together" and that I should then press any key to continue. After that, It asks me to enter the first number. I enter that and it asks me to enter the second number. When I press enter this time, It will say "The Answer is:" and display the answer. It'll say press any key to continue, and when you hit a key, the program will close.
Simple right? Actually, yes. I'll break it down
- Code:
-
#include
#include
#include
These are declaring certain syntax forms you'll be using. In Laymens terms, the computer needs to know where to find some of the things you are saying.
- Code:
-
int main()
This is your standard int main(). no capitals, and only one space between int and main. This is declaring that you're about to write out a function for your program.
- Code:
-
{
This is declaring that you're starting to write in the function here.
- Code:
-
}
Easily enough, this means you're done writing the function of the program.
- Code:
-
int integer1;
int integer2;
This is where we Declare the variables that we'll be using in the program. "integer1" represents the first number we will be inputting, and likewise "integer2" represents the second number. It is important, organizationally, to declare the variables at the top of the program. It makes everything a lot easier. At the end of these statements are a semi colon. you will almost always put a semi colon on the end of your statements, though a few exceptions are the "#include" statements at the top, as well as "int main()".
- Code:
-
cout <<"Use this program to add two integers together\n";
cout is read like "see-out". cout will always have the braces pointed in. I dunno why, it's just how it is. It's basically saying that this is what is displayed on your screen. The text in the quotations is displayed, minus the "\n". the "\n" tells the program to create a new line so it doesn't look all clustered. as would be expected, this statement ends with a semi colon.
- Code:
-
system("PAUSE");
This statement tells the program to stop for a moment so you can read it. it will display the "Press any key to continue..." text. It's a fairly common statement and ends with a semi colon.
- Code:
-
cout <<"Input number 1\n";
After pressing any key to continue, this will be displayed as text- "Input number 1". The "\n", if you remember, has the program create a new line. and the statement ends with a semi colon
- Code:
-
cin >> integer1;
This statement ask for our input from our keyboard. The "cin" is pronounced "See-in". cin statements always has away facing brackets. Again, I dunno why. After the brackets is "integer1". look familiar? it should. It's the same variable that we declared earlier on. This line is basically asking for us to type in what the variable represents. Now, because we used "int", it has to be a number. because it is a math equation, it would be pointless for it to be anything but a number. However, should you need to declare a variable that would require the input of text, you would replace "int" with "char". Also, as a side note the "int" declartion is for integers, or counting numbers, only. if you input decimals, they will be rounded immediately. If you need to input decimals, replace "int" with "float". this statement ends with a semi colon, just like all of them.
- Code:
-
cout <<"Input number 2\n";
cin >> integer2;
These are the same as the past two we went over, except your using the second variable that you declared.
- Code:
-
cout << "The answer is:" <<< "\n";
This is by far the most complicated line in our program. It starts with cout, and brackets. Like the earlier examples of cout, it has text inside quotations. however, it then has another set of brackets. why? To continue what is being outputed. after the brackets it says "integer1 integer2". This is telling the computer that it is to take the number that we said represents the first variable (integer1) and add it to ( ) the number we said represents the second variable (integer2). after that, we have more brackets to continue the output. why? Because we need a newline, hence the "\n". Note that the \n was in quotations. It ends with a semi colon.
- Code:
-
system("PAUSE");
We have another system pause....but why? Well, because without it, the program would shut down so fast after you inputed the value for the second variable that you wouldn't be able to read it.
- Code:
-
return 0;
Return zero tells the program to end.
- Code:
-
{
This tells our compiler that we are done with this function.
Alright! first program succesfully broken down!
Now, in your compiler, it will be displayed a little differently. some things will be different colors, and bolded. Heres what our program would look like in Dev C++
- Quote :
#include
#include
#include
int main()
{
int integer1;
int integer2;
cout <<"Use this program to add two integers together\n";
system("PAUSE");
cout <<"Input number 1\n";
cin >> integer1;
cout <<"Input number 2\n";
cin >> integer2;
cout << "The answer is:" <<< "\n";
system("PAUSE");
return 0;
}
Oh, and finally, what the program looks like, after being run in command prompt