Gw Temp

Menu

Tutorial - 'A Really Big C++ Tutorial' by Xenosaga2

An item about Programming Languages posted on

Blurb

A great and long tutorial on the C++ language

Body

Okay, this is my first ever tutorial. I'm going to try to explain everything that I know thus far about C++, and I might come back to add in some more tutorials later on.

Okay, I know that everyone's been doing the #include thing, and mine will be similar to that, but I will be explaining each thing beforehand.

Comments - If you wish to make a comment in a program, which is like a comment in RM2K, you can do 1 of 2 things. You may either put a // or a /* before the comment. You will need to close the comment with a */ if you are using the /*. This is really helpfull because you can comment out the rest of the program, or just a segment of it without having to put a // to the left of every line. The computer will analyze the // and will not do any of the commands on that line. The same goes for /*. They are just there to help you remind yourself of whatever you are doing. Here's an example:

// Main Program
{
Blah, Blah, Blah
}
// Subprogram
{
More Blah, Even More Blah
}
// End of Program.

#include - This makes it so that the filename given to the right of this command is included in the program. All content in that file can be used to your advantage depending on which files you are including. Here is another example:

#include
#include

// Main Program
{
Blah, Blah, Blah
}
// Subprogram
{
More Blah, Even More Blah
}
// End of Program.

The file should be automatically be included in the program file that you have created. is usually not included in the program when you create a new one, so make sure that you include it.

Return 0 - This command prompts a "press any key to continue" to appear wherever you put it in the program. When you press any key, the window closes. Here's another example:

#include

// Main Program
{
Blah, Blah, Blah;
return 0;
}

cout - Now, you can't just put Blah, Blah, Blah and expect it to display that message. You have to define that it's being outputted (I think that's the word for it.) so, you would type cout << "Blah, Blah, Blah"; Here is yet another exciting example:

#include

// Main Program
{
cout << "Blah, Blah, Blah";
return 0;
}

This would display the following:

Blah, Blah, Blah
press any key to continue

int main() - This is the heading of your program. The int part means that the heading is an integer (No decimals). You could change this to double if you wish (Number with a decimal). The main part is a variable built into the heading. If you wanted, you could make the program return main if you wished to. This would make the program return to the beginning. (Or to wherever you have the particular variable.) Here's the example I just explained:

#include

// Main Program
int main()
{
cout << "Blah, Blah, Blah";
return main;
}

This would produce:

Blah, Blah, Blah
Blah, Blah, Blah
Blah, Blah, Blah
Blah, Blah, Blah
Blah, Blah, Blah
Blah, Blah, Blah

And so forth.

cin - This is the input for the program. Using this will allow you to input a value when prompted. I would suggest using a cout message before that so that you may define the prompt. You will need a variable to store the number into. You can define a variable by adding in int or double, then the name of that variable. You will know what I'm talking about if you've used RM2K enough. An example of which would be:

#include

// Main Program
int flasknumber;
int main()
{
cout << "How many flasks of evil on the wall?";
cin >> flasknumber;
return 0;
}

When you put the variable in the source code in this fashion, you are declaring that function. It's like saying that the function is there.

endl - endl means "End Line". It tells the program that it needs to return once. But not like return 0; I mean like pressing the Enter key on your computer. The result for the cin example would actually be:

How many flasks of evil on the wall? __________
press any key to continue

Now, if you used endls like so:

#include
int flasknumber;
int main()
{
cout << "How many flasks of evil on the wall?" << endl;
cin >> flasknumber >> endl;
return 0;
}

Then the result would be:

How many flasks of evil on the wall?
___________
press any key to continue

<< or >> operands - You will need to use these when using cin or cout. You'll notice that the left brackets go after the cout (<<) and the right brackets go after the cin (>>) these must be used when you are using the cin and cout stuff. (Don't ask me why.)

Displaying variables - Now, let's say that you wanted the program to display the amount of flasks of evil that you put in. You would add a cout that was as follows:

#include
int flasknumber;
int main()
{
cout << "How many flasks of evil on the wall?" << endl;
cin >> flasknumber >> endl;
cout << flasknumber << endl;
return 0;
}

This would wind up displaying something like this:

How many flasks of evil on the wall?
__________

press any key to continue

Variable Operations - Here's where things get a bit more complicated. Let's say that you entered 1 in the cin part, and you might need to add on a few because the demons in your head will probably tell you to throw a few more at the wall. Hmmm... I think 10 might do. So if you wanted 10 more to be added to your current total, and then displayed on the black, icky screen of doom, you would do something like this heah:

#include
int flasknumber;
int main()
{
cout << "How many flasks of evil on the wall?" << endl;
cin >> flasknumber >> endl;
flasknumber=flasknumber+10;
cout << flasknumber << endl;
return 0;
}

This takes the flask number that you entered and adds on 10 to it. So the result should be:

How many flasks of evil on the wall?
1
11
press any key to continue

Now you can also use other operations in this same way. Some of the default commands in C++ are '+', '-', '*', '/' and Mod. With modulus, though, you have to be a bit cautious. You must not use modulus on a number that is int type. If you do, it will be trying to find the decimal that is supposed to be on the int number, which can't happen, becuase the decimal values in an int number are truncated. This means that every number past the decimal will be destroyed, and the number will become a whole number. (I.E. 1.5 becomes 1) Try using modulus on double type numbers instead.

Okay, I guess I'll stop typing for now. (Did I forget to add in something about the ;'s? Oh well. I'm too lazy to write about a symbol) I hope I have the option of coming in and adding in stuff later on. If I can't, oh well. And if you guys give me good ratings on this one, I might come back and do another one. So what do you think of my first tutorial?