Home
Help
Search
Calendar
Login
Register
Please
login
or
register
.
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News:
Click here to join us on IRC (#charas on irc.freenode.net)!
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
General programming
»
C++
« previous
next »
Print
Pages: [
1
]
Author
Topic: C++ (Read 2469 times)
maxine
Average scripter
Zealot
Posts: 736
¨Dude i am 16 with 2 balls and hair
C++
«
on:
November 19, 2006, 11:56:25 AM »
As you might have seen, i posted the other starting up tutorial just for for the user to see that everything runs smooth this far. The code we went through were something like this:
#include
using namespace std;
int main(){
cout << "Hello World!";
getchar();
return 0;
}
Beginners might scratch their heads... Yes. I know Pyro went through this before, BUT i were the one who tought you one thing and another
. Anyway, we should now cover what everything does.
#include
..... All lines that starts with # is a preprocessor directive. This runs before the compiler goes through its work and substitutes text based on various directives. Here we have the #include which tells the preprocessor to include the content of another file. In this case we included the library
iostream
. This library is a standard library and automatically comes with your IDE. The iostream contains code to help us do display output. For now lets say these include files is there to make stuff easier.
using namespace std; Now if i cover up what this does it might confuse most people so if you really bad would like to know what this does, then say so after my tutorial and i will cover up that aswell. But for now just have it there on ALL of your source codes.
int main() ... This is our main function, its in here where all the code goes. Not all but the general. There is something called functions which grabs information from another place of the file or simple from another file. Anyway to reach this information you have to call it from the Main(). Functions will be a further lesson.
You can see i have brackets { } .. Opening brackets and closing brackets connects to each other. One opening bracket has one closing brackets. Now in the code the opening bracket appears right after int main() like this int main(){.. The closing bracket is at the bottom of our program. This makes the compiler go through everything thats between these brackets. I will just write a short code where more brackets are involved and what actually happens when there are more of them:
#include
using namespace std;
int main(){
cout << "Hello World!";
for(int i = 0; i <10; i++){
cout << i;
}
getchar();
return 0;
}
Now here you see that i have edited some lines of code. I dont want you to worry about these lines, just the two brackets ive edited.
#include
using namespace std;
int main(){ // this bracket
cout << "Hello World!";
for(int i = 0; i <10; i++){
cout << i;
}
getchar();
return 0;
} // connect with this bracket
And.....
#include
using namespace std;
int main(){
cout << "Hello World!";
for(int i = 0; i <10; i++){ // this bracket
cout << i;
} // connects with this bracket
getchar();
return 0;
}
Now when the compiler runs to this line "for(int i = 0; i <10; i++){"
It executes all whats inside those 2 brackets and when its done whats inside the brackets it continues down passed its closing bracket '}' and runs the getchar(); and return 0;... It might be alittle confusing. Anyway lets continue with what we have inside our main().
cout << "Hello World!"; ... This puts Hello World! to our black screen. cout << is pretty self explained c..out. It puts out whatever's between the " " signs and allways ends with a semi-bracket ';'. Try mixing and writing something else between there just to make things alittle more familiar.
getchar(); waits for the user to press enter. If this line wasn't there the program would stop as fast as it started. Pretty basic.
return 0; simply closes down the program saying everything went smooth.
Back to the library we included. If we wouldn't have included the file iostream. cout << would have been std::cout. I told you the libraries made stuff much more simple. Now getchar() would be like this-> std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); if we wouldn't have our special little helper.
Next time we cover up variables and alittle more code. Now this just looks like flabbering and i will understand if its alittle hard to understand. But when we advance to funnier codes it might be alittle easier to stay on track.
Logged
Print
Pages: [
1
]
« previous
next »
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
General programming
»
C++