Monday, April 11, 2011

I should have started here

OK so i got ahead of myself on day one. Everyone just wants to start in coding write away but this is bad and soemthing that I have to teach myself not to do so today I'm going to discuss how to start a new project in the Visual Studio IDE. I'm using Visual Studio 2010 Professional but this will apply to the express 2010 and also older versions of the IDE as well.
1. Open up VS(Visual Studio) from the Windows start menu. (duh!!)
3. Choose new project from the main page and make the language highlighted says C++.
4. Choose Win32 console application and give the rpoject a name (I called mine hello world).
5. Choose OK and then click next on the next screen. Choose to make it a blank project under other options.
6. We're almost ready to start coding. On the pane on the left of the screen right click on source files (this folder will hold all of the source code files you weite). Choose Add -> new file. Then choose C++ Source file and call the file hello.cc or .cpp or whatever want as long as it ends in .cc or .cpp.
7. The white screen you see in the middle of the IDE is the text editor where you will actually write the code at.
8. Now for the code!!!! put the following in the text editor:
#include
using namespace std;

int main()
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
9. Before we compile this code and see the magic I would like to explain it. #include is telling the C++ compiler to get code from another file called iostream (the compiler takes care of this don't worry about it) and compile it for use in this program. using namespace std; tells the compiler to use the namespace std which I will explain later.
int main() is the start of a function. It tells the compiler to execute the code between the following { and } together. every C++ program starts with a main function. the int at the beginning tells the compiler that it will return an integer value when it succesfully completes.
the next line cout << "Hello world!" << endl; tells the computer to output the text between "" which in this case is Hello world!. endl tells the compiler to move to the next line.
Then in the next line you will see cin.get();. This actually tells the program to pause until it receives input from the standard input(keyboard in this case). When we execute the program, we will see the program pause after it outputs text to the screen. If we didn't have this line the program would run very quickly and you would not have time to see the program execute.
The last line is return 0; and it tells the program to return the number 0 when it properly executes.
10. OK so now we're ready to actually compile and run the program. Press F5 on the keyboard and you will see the VS IDE run through some stuff in the lower part of the screen and then you'll see the words you type in appear in a DOS command window. Press any key on your keyboard and then the enter key and it will complete it's execution.

Saturday, April 9, 2011

Day 1 code

OK so I decided to do this in C++ since I'm much more likely to use C++ in day to day programming.
Here is the code from day one:
/****************************
* Metamomrphix *
* C++ in a year Day 1 *
****************************/
#include
using namespace std;

int main()
{
int limit = 1000;
for (int x = 0; x <= limit; x++)
{
cout << x << endl;
}
cout << "this ends day one. give input to quit" << endl;
cin.get();

return 0;
}

for an explanation the top part before the #include is just a comment section wich can be used to provide information about code.
the #include means to compile the code included in the iostream.h header file. This increases your codes abilities with adding only one line of code. The iostream.h file contains the functions necessary to produce output and will be included in most of the C++ apps that you write. The namespace basically says where the file is located sort of. I learned C++ before namespaces were used so I never really studied them much I just put that line of code in.
int main()
{
......
}
is a function which is a section of code that is executed and can be used in any place in your program. This is a small program so it only has one function wich is the main function that every C++ app must have.
Next you see int limit = 1000;
This is defining a variable that is an integer called limit and initiating it to 1000 Variables are declared in C++ with datatype ;. Or you can also assign a value at the same time you declare it like I did in the example or you could declare the variable on its own line and not assigning a value to it yet. I could have done this by using int limit; and then instantiated it later on in the function by using the line limit = 100;
Next I usewhat is called a for loop to loop through a portion of code. This is the most commonly used loop you will come accross and use in C++. The syntax is for ( datatype name = starting value; name comparison operator limit; name+ value. You see I used the line of code for ( int x; x <= 100; x+). In english this means at the start of the loop a variable of integer type called x is = 0, go through the loop while x is less than or equal to 100, and each time the loop is iterated or gone through increase x by one (x++). Then each time through the loop the code between { and is executed.
cout << << meas to output whatever is between << and <<. In this case I put in the variable x which will be a number between 0 and 100 as per the conditions in my for loop. endl simply means to stop printing on the current line and print on the next line.
You can also output strings or characters between the << and << by putting the text in quotation marks as I did in this line cout << "this ends day one. give input to quit" << endl;. Much more can be done with output in +, but this is day one so we'll call this lesson good after two more quick lines of code.
cin.get() will simply tell the program to pause at this point until input is received from the standard inptu device which is the keyboard in this case. Try putting // before this line of code wich will comment this line out and prevent it from being executed. The program will run through very quick without pausing and you see just a quick glimpse of a dos box on the screen and it'll go away because we didn't tell it to pause.
The last line is very important. Every function in C++ must return a value. We declared the main function of type int so it must return an integer. Therefore upon successful completion the main function will return 0 and let the function and the program in this case end.
That's it until tomorrow.
God Bless,
Metamorphix

First post

Hello everyone who may or may not read this blog. My objective in this blog is to use to geatly increase my rogramming abilities.
I was tryign to think what would help me to learn programming more than any other language. As a result I decided to learn C. I already use C# fairly regularly, but felt that it was much to high level.
So Over the next year I will be working through the C language and will be posting ever day in regards to my experiences. I hpe tht anyone who read sthis finds it either interesting, educational, or maybe even entertaining as I go th5ough this journey to learn the C language. God Bless,
Kevin