Gw Temp

Menu

Tutorial - 'A beginners C++ Tutorial Part 3' by Maximo

An item about Programming Languages posted on

Blurb

Third part of Maximo's C++ Tutorial, this time covering variables.

Body

Now, we move on to something that you might think sounds tough, but once you get the hang of it, it really isn't! They're called variables. Don't be scared. I know you want to close the window thinking "OH NO. I HATE ALGEBRA". But really, don't be worried. Think of tutorials, not as an unknown number, but a box to store data. Every box has a name, no matter what. And the first type of variables that you're going to learn are integers. Yes, you might think "THAT IS ALGEBRA", and it slightly is, but don't take out any math books, please. First, you want to declare your variable. Well, declare it's name at least. Here's the code for how you would make a "box" without any data:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{

int newVariable;

system("PAUSE");
return 0;
}

Guess what? You just made an empty variable! Would you like to add something to the "Box"? Let's do it! On the second line of the main function, put in these words:

newVariable = 10;

So all the code would say:

#include
#include

using namespace std;

int main(int argc, char *argv[])
{

int newVariable;
newVariable = 10;
system("PAUSE");
return 0;
}

Nice job! You just made a variable box called newVariable equal to the value of 10. Now, you want to see your variable, right? Well, right below the line that says:

newVariable = 10;

Right the line:

cout << newVariable << endl;

NOTE: C++ is CaSe SeNsItIvE. Unlike html, make sure you save yourself your sanity and don't make it newvariable when it should be newVariable.

NOTE: In C++, don't put the quotes around the variable inside of the cout function. Otherwise it'll just right the variable's name and not it's value.

Ready to compile and make your program? Press F9 or go to Compile->Compile & Run. You should see it say:

10
Press any key to continue...

If it says that, congratulations, but if you made a compiler error, remember how to check and undo those? Look back to my older tutorial to see how. Now, you've seen the way that those tutorials work, in basic terms, are you ready to do math with numbers?

Remember that with math in C++, numbers can only go so high. To be on the safe side, don't make any quotient, sum, difference, product, or just NUMBER less than -2,000,000 or more than 2,000,000. BUT if you're only using positive numbers, you can make it go up to 4,000,000. Yes, I know, you probably can go past that in your head, but this is a computer without a brain we're talking about. The code for this is:

cout << 10 + 10 << endl;

Add that into the main function. See what it comes out with when it's compiled. The same goes for subtraction:

cout << 10 - 10 << endl;

And multiplication, but before you do multiplication, you must know one thing. The multiply simbol is * not X. So:

cout << 10 * 10 << endl;

Division's sign is /:

cout << 10 / 10 << endl;

Nice job. Meet me back here next time, and we'll go off from here. :)

~Maximo