DIR Return Create A Forum - Home
---------------------------------------------------------
The New DS computer forum!!
HTML https://dscomp.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: C++
*****************************************************
#Post#: 289--------------------------------------------------
Tutorial #4 - Intro to variables.
By: Hondaman Date: October 9, 2010, 5:45 am
---------------------------------------------------------
[center]Welcome to Tutorial No.4
In this tutorial you will learn how to use the int variable and
what variables are for are for.[/center]
[font=trebuchet ms]About Variables -[/font]
A variable is simply a place holder for something else.
This being you could make the word 'Awesome' be equal to '0'
Variables are in *(I THINK)* every game.
Without them the game just wouldn't work as it could, or should.
[font=trebuchet ms]Part 1 - Setting up a variable[/font]
Before we can output anything we need to asign a value to a
word, phrase or number
Varable names can be anything e.g.
Char1, 11011 or User_Inpt
This means you can have as many variables in a script as you can
think of combinations of numbers, letters or both.
But there is a few small rules you may want to follow.
You dont have to but I reccommend you do.
1 - Name variables with a name related to what they are for e.g.
User_name, Username, User_N etc... (Makes the code easyer to
understand)
2 - Dont make variable names too long e.g. User_N (this means
that you will come across less errors when typing the name of
the variable.)
Now to setup your variable.
Using you hello world script
[code]
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
system ("pause");
return 0;
}
[/code]
above 'cout << "Hello World!" << endl;'
add another line and type in 'int'
this should go bold. Int is short for integer and an integer is
a whole number.
after in type in your variable name, I'm going to use 'cheese'
and then a semi-colon.
so it should look like -
[code]
int cheese;
[/code]
This basicly means we are going to assign an integer to the word
cheese.
and now below 'int cheese;' type
[code]
cheese = 124;
[/code]
So now we have just made the word cheese equal to 124.
[font=trebuchet ms]Part 2 - Using the variable[/font]
To output this simply edit your
[code]
cout << "Hello World!" << endl;
[/code]
To -
[code]
cout << cheese << endl;
[/code]
This will output 124 from the variable.
And if you want to have the output as , 'Text , 124 , Text'
then all you have to do is type -
[code]
cout << "Hello World! " << cheese << " Is my favorite number."
[/code]
Remember! -
put a space after "Hello World! "
and befor " Is my favorite number"
this is because if you dont the program will output
[code]
Hello World!124Is my favorite number.
[/code]
Instead of
[code]
Hello World! 124 Is my favorite number.
[/code]
Thanks for reading this tutorial.
If there was anything you didn't understand or anything is wrong
PM me.
Hondaman.
[center]<< Previous
HTML http://www.dscompforums.tk/c/tutorial-3-extending-the-hello-world!-script/msg286/?topicseen#new<br
/> | Next >>
HTML http://www.dscompforums.tk/c/tutorial-5-getting-user-input/[/center]
*****************************************************