View Full Version: Variables and YOU

CheatSync.net Forums > C++ Tutorials > Variables and YOU


Title: Variables and YOU
Description: A How-To guide on variables.


Durka Durka Mahn - September 19, 2007 08:52 PM (GMT)
The word 'variable' in C++ basically means memory space.

There are many types of variables, but today, we will simply be learning the int, the double, and the char variable types.

First off, I will show you how to define a variable.

To begin, you need to choose a data type that you want to work with:

Int - Integer numbers. Any number that ISN'T a decimal. Can be negative, even 0. Can't be a character.

Double - Real numbers. Can be ANY number, including decimals, but CAN NOT be a character, like ints.

Char - Characters. This can be any number, any letter, and any other symbols on the keyboard.

Now, depending on WHAT we want to do, you will use different types of variables to get around what you are doing.

First, I will show you the int variable type.

To declare an int, well..you type int, like so:

int x;

This will declare a new variable space named 'x' that can hold any integer number.

Now, you can also declare its value when you declare the variable name, like this:

int x = 5;

Now, imagine that the computer has set a memory space named 'x' and the value at that memory space is 5.

We can now use x in cout statements!!

CODE
int x = 5;
cout << "The value in variable x is..." << x;


This will output:
QUOTE
The value in variable x is...5


Now, let's move on to doubles.

Doubles can be any number. It can be a decimal, it can be 0, it can be negative, too.

To start off, we will declare a double.

double r;

This will name a memory space on the computer and call it r.

Now, we want to store something into our double, don't we? Well, we store a value just like we do with ints.

double r = 1.67;

Now, we can even use it an cout statements, just like with ints!
CODE
double r = 1.67;
cout << "The value in variable r is..." << r;


This will output:
QUOTE
The value in variable r is...1.67


Cool, huh?

Now, for the last part, let's talk characters!

To declare one, we will do the same as before, but with char instead.

char name;

Now, the difference between the two other variables is that character variables require quotes (" ") in order to declare its value. Also, we will need to declare the amount of characters we will be using. I usually use 200 on all variables.

Here is an example:

char name[200]="DDM";

This will set up a new character memory space with 200 slots. Each character we input will take up one of those slots.

Now, here is how we would output it:

CODE
char name[200]="DDM";
cout << "My name is " << name;


This is our output from the code:
QUOTE
My name is DDM


Hope the guide helped!

If you have any questions, problems, or comments, please reply below!

Vii - September 19, 2007 09:15 PM (GMT)
awesome tutorial. I'll give it a go.


Durka Durka Mahn - September 19, 2007 09:23 PM (GMT)
Thanks :D

Waterbottle - September 20, 2007 06:50 PM (GMT)
QUOTE (Durka Durka Mahn @ Sep 19 2007, 10:52 PM)
Int - Integer numbers. Any number that ISN'T a decimal. Can be negative, even 0. Can't be a character.

What do you mean it can't be a character? You can do int a = 'b'; just fine,seeing as writing 'b' is the same as writing 98 . Maybe I just don't understand you. I didn't bother reading every thing, so yeah.
Also it can't be any number, it must be inbetween -(232/2) and 232/2. ( assuming it's signed )


QUOTE

Double - Real numbers. Can be ANY number, including decimals, but CAN NOT be a character, like ints.

It obviously can't be any number, even though it can store huge values, also it's important to note how it loses precision the higher it get, and the precision is never 100% perfect. http://www.cprogramming.com/tutorial/float...ting_point.html


QUOTE

Char - Characters. This can be any number, any letter, and any other symbols on the keyboard.

Any number? Even the unsigned version can't be a higher number than 255.

Durka Durka Mahn - September 20, 2007 07:02 PM (GMT)
QUOTE (Waterbottle @ Sep 20 2007, 06:50 PM)
QUOTE (Durka Durka Mahn @ Sep 19 2007, 10:52 PM)
Int - Integer numbers. Any number that ISN'T a decimal. Can be negative, even 0. Can't be a character.

What do you mean it can't be a character? You can do int a = 'b'; just fine,seeing as writing 'b' is the same as writing 98 . Maybe I just don't understand you. I didn't bother reading every thing, so yeah.
Also it can't be any number, it must be inbetween -(232/2) and 232/2. ( assuming it's signed )


QUOTE

Double - Real numbers. Can be ANY number, including decimals, but CAN NOT be a character, like ints.

It obviously can't be any number, even though it can store huge values, also it's important to note how it loses precision the higher it get, and the precision is never 100% perfect. http://www.cprogramming.com/tutorial/float...ting_point.html


QUOTE

Char - Characters. This can be any number, any letter, and any other symbols on the keyboard.

Any number? Even the unsigned version can't be a higher number than 255.

Char numbers as in "2655654756234623573475234625846794672345346"

That will be stored in characters, not number values..

And yeah I didn't really think to mention the ASCII chart with ints...Not really needed just yet. This is just a beginner tutorial.




Hosted for free by InvisionFree