RockGunZ
Would you like to react to this message? Create an account in a few clicks or log in to continue.



 
HomeHome  PortalPortal  SearchSearch  Latest imagesLatest images  RegisterRegister  Log inLog in  
Please visit the newest forums by clicking Here

 

 Soul's C++ Tutorial.

Go down 
AuthorMessage
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:14 pm

Hello i am Soul i will be willing to to show you how to do C++ because i realize we should have some useful people to help us keep this community better! Thanks. [color:aea6="Lime"]DEDICATED FROM THE ORIGINAL TUTOR IS FROM CHAOTIC GIVE HIM SOME CREDIT THANKS!"]

[SIZE="2"]I decided to write up some C++ tutorials for those here who are interested in learning C++.
I do not claim to be a professional at C++, I just want to share my knowledge with you. If you don't agree with things I say in here, leave a post.
I will be updating this thread as much as I have time to with new tutorials.


Thanks to Soul Aka Pedo for formatting this tutorial so it is easier to navigate through and looks better.[/SIZE]

[color:aea6="White"][SIZE="5"]Contents[/SIZE][/CENTER]
[color:aea6="Lime"]
[SIZE="4"]Chapter 1: C++ Fundamentals (Win32 Console Application)[1.0]

[SIZE="3"] - [color:aea6="Lime"][1.1]Lesson 1: Setting up C++
- [color:aea6="Lime"][1.2]Lesson 2: Non-executable Code
- [color:aea6="Lime"][1.3]Lesson 3: Output Commands
- [color:aea6="Lime"][1.4]Lesson 4: Variables
- [color:aea6="Lime"][1.5]Lesson 5: Input Commands
- [color:aea6="Lime"][1.6]Lesson 6: Operators
- [color:aea6="Lime"][1.7]Lesson 7: Loops
- [color:aea6="Lime"][1.8]Lesson 8: If Statements
- [color:aea6="Lime"][1.9]Lesson 9: Functions
- [color:aea6="Lime"][1.10]Lesson 10: Arrays
- [color:aea6="Lime"][1.11]Lesson 11: Multi-dimensional Arrays
- [color:aea6="Lime"][1.12]Lesson 12: Loops Continued
- [color:aea6="Lime"][1.13]Lesson 13: Pointers[/SIZE]
[/SIZE]
[color:aea6="Lime"]
[SIZE="4"]Chapter 2: Object Oriented Programming (Win32 Console Application)[2.0][/SIZE]

[SIZE="3"] - [color:aea6="Lime"][2.1]Lesson 1: Classes
- [color:aea6="Lime"][2.2]Lesson 2: Inheritance
- [color:aea6="Lime"][2.3]Lesson 3: Polymorphism
- [color:aea6="Lime"][2.4]Lesson 4: Making a Console Game
[/SIZE]

More to come when I get to further lessons. For now, we will start with these.

[color:aea6="White"][SIZE="5"]Resources[/SIZE]



You will need a C++ compiler. There are many available, both free and of cost. I will be using Visual C++ 2008 as the compiler for these tutorials, but it should work fine in any compiler.
Visual C++ 2008 Express Edition Installer

For now that is all you will need.

[color:aea6="White"]
[SIZE="5"]Common Errors and Fixes[/SIZE]


[color:aea6="Lime"]1. error C2065: 'x': undeclared identifier - You have either made a typo, or forgot to declare a variable or something along those lines, double click on the error to be taken to the correct line. Fix the error by correcting the spelling or declare the variable before using it.
[color:aea6="Lime"]
2. fatal error C1083: Cannot open include file: 'x': No such file or directory - You have either made a typo, or are missing the file. If you made a typo, correct it. If you are missing a file, contact me on MSN (chaos-bladezx [at] hotmail [dot] com).

[color:aea6="Lime"]3. error C2143: syntax error : missing ';' before 'x' - You forgot to add a semi-colon at the end of one of your functions. Double click the error to go to the line at fault.

More to come as requested/suggested/needed.

Chapter 1: C++ Fundamentals (Win32 Console Application)[color:aea6="Lime"][1.0]


[color:aea6="Red"][SIZE="6"]Lesson 1: Setting up C++ [/SIZE][color:aea6="Lime"][1.1]
[color:aea6="White"]
[SIZE="5"]Setting up a Project[/SIZE]

Throughout chapter 1 we will set up our projects the same way every time, and here is how.
First, click File > New > Project
Choose Win32 Console Application as the project type and name the project "Lesson1"
In the project wizard, click next
Check the "empty project" box

Your project is now set up. In the "Solution Explorer" on the left you will see 3 folders, Header files, Resource files and Source files.
For now we will only need to worry about source files, these are the files that contain code.

Right click on Source Files > Add > New Item...
Choose C++ File(.cpp)
Name this file "Main.cpp"

This will be the Main source file and will be existent throughout these lessons.

[color:aea6="White"][SIZE="5"]Including Files[/SIZE]

In C++, we must include header files into our projects if we wish to use certain commands. For this lesson we will include a file called iostream. This file allows us access to certain input/output commands.
To include a file we use the keyword "#include" followed by the name of the file encased in less than/greater than symbols.

Code:
#include <iostream>

Now that we have access to these commands we must acknowledge that they are part of the std namespace. This means that to access these functions we must add the prefix "std::" at the beginning of every command in this commandset. However, this will become laborous and so we will use the following line of code which will stop us from having to do this.

Code:
using namespace std;

In C++, we usually add a semi-colon to the end of a line to let the compiler know we are finished performing a certain task. This must be done after "using namespace std" but not after certain keywords such as "#include".

[color:aea6="White"][SIZE="5"]The Main Program Function[/SIZE]

This part is considered by most beginners to be complicated, so make sure you read carefully.

In a C++ Console Application we must use a function called main, unlike other function, we do not call this function, but just define it. This function is called "main" and will consist of much of the main program code.

I will show you what this function looks like, and then go over each part.

Code:
void main()
{
   return;
}

For now we wont worry too much about this, but the keyword "void" means that the function will not return a value. If you don't understand this, dont worry. You will find out later in the functions lesson. "main" is the name of the function, and must always be called "main". The parenthesise '(' and ')' would usually contain parameters for a function, however, "main" does not require this in our case so we will leave it empty. More about parameters in the function lessons.

'{' and '}' are curly braces and contain the code that belong to this function. Anything between these will be a part of "main". Again, don't worry too much about this as of now.
"return;" tells the compiler we are finished with the application.
If you are using Visual C++ X then when the application finishes it will automatically say "Press any key to continue..." before closing.
If you are using a different compiler that does not do that, add the line "system("PAUSE");" before "return;" to force it to do this.

Code:
void main()
{
   system("PAUSE");

   return;
}

This is how we set up for a console application. Next lesson we will learn how to output text to the screen.

Your final code should be:

Code:
#include <iostream>
using namespace std;

void main()
{
   return;
}
~Thanks more lesson will come!
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:15 pm

[color:ee33="Red"]Lesson 2: Non-executable Code[color:ee33="Lime"][1.2]

This is code that is not executed when you build your application. It is useful for making comments to help you remember what a piece of code does, or to explain what your code does if you show it to someone else.
To make a comment, there are two ways.

'//' lets the compiler know that everything else on that line is a comment. The following are acceptable
Code:
//draw text to the screen

cout << "Hello" << endl; //say hello to the user

However, if we had a paragraph of text and we wanted it commented, or we only wanted part of a line of text commented we use '/*' and '*/'
The following are acceptable
Code:
/*this is a comment
this comment takes up many lines
this comment is big*/

cout << /*say hello*/ "Hello" << endl;

Although cout << /*say hello*/ "Hello" << endl; is acceptable, I wouldn't suggest filling your code with comments like that. Comments are better at the end of a line using '//' as shown above.
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:15 pm

[color:6b3f="Red"]Lesson 3: Output Commands[color:6b3f="Lime"][1.3]

Now that we have set up our project, we need to get some text up in our console window.
The command which we use for this is "cout". This command outputs the streamed information to the screen.
This code goes inside main, before the program returns.

Code:
cout << "Hello World" << endl;

The '<<' operator streams the following data into cout to be drawn into the console window.
"Hello World" is a string of text which we stream into cout, and then we stream the command "endl" to provide a line break. An alternative to "endl" is "\n"

Code:
cout << "Hello World\n";

For now, this is the only output command we will be using, and we will discuss it's use more in further tutorials.

Your final code should be:

Code:
#include <iostream>
using namespace std;

void main()
{
   cout << "Hello World" << endl;

   return;
}
More will come this may take time D:
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:19 pm

[color:e411="Red"]Lesson 4: Variables[color:e411="Lime"][1.4]

Variables are things which can store values, these values can be read and altered.
Variables are declared as followed.

variabletype variablename;

Or they can be declared with a value.

variabletype variablename = value;

[color:e411="White"]Variable Types

int - this variable type holds whole numbers
float - this variable type holds decimal numbers
string - this variable type holds strings of text, we must include the header file "string" to use this

There are many more variable types, but we will stick with these 3 for now.

Ok so first we will include the header file "string". Directly underneath "#include <iostream>" we add:

Code:
#include <string>

[color:e411="White"]Naming Convention

In C++, naming conventions are used to avoid mixups between commands, variables, functions etc. They are also used to keep things organized.

The naming convention for variables are as followed.

The variable name is in all lower case except the first letter of the words. The first letter of the first word is in lower case, but the first letter of every following word is in upper case.

The following would all be correct:

variable
myVariable
mySuperLongVariableName

[color:e411="White"]Declaring Variables

When declaring variables, where you declare them is important.
If you declare the variables before main they will be global, meaning they are accessable from anywhere else in the source file.
If you declare the variables inside main, they will be local, meaning they are accessable only inside main.

We will be declaring our variables inside main for now.

We will declare 3 variables, one int, one float and one string.
We will call them myInt, myFloat and myString.
myInt will contain the value 3
myFloat will contain the value 7.2
myString will contain the value "mytext"

To do this we use the following code

Code:
int myInt = 3;
float myFloat = 7.2;
string myString = "mytext";

We can now use our output command "cout" to display the value of these. We will display this after "Hello World".

Code:
cout << myInt << " " << myFloat << " " << myString << endl;

This will stream each of the values of our variables into cout, seperated by a space each time.
[color:e411="White"]
Declaring Multiple Variables

If we wanted to make 5 different integers, we have 2 methods of doing this.

The first method is rather obvious which is:

Code:
int myInt1;
int myInt2;
int myInt3;
int myInt4;
int myInt5;

But we also have the option of declaring variables of the same type all at once by seperating the names with a comma.

Code:
int myInt1, myInt2, myInt3, myInt4, myInt5;

This applies for all variable types, not just int.
[color:e411="White"]
Exercise

If you feel you are not ready to move on, or you want to test yourself, I would suggest trying this exercise.

Task: Use "cout" to display the following sentence, each word or number being a variable of the correct type. I will put the answer to this exercise on the next lesson.
Sentence: 3 Monkeys had 3.5 Bananas each

Your final code should be:

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
   int myInt = 3;
   float myFloat = 7.2;
   string myString = "mytext";

   cout << myInt << " " << myFloat << " " << myString << endl;

   return;
}
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:20 pm

[color:d564="Red"]Lesson 5: Input Commands[color:d564="Lime"][1.5]

To get input from the user of our application we must use an input command. For this we will use "cin".
"cin" will get input and stream this data into a variable of our choice.

For this we will ask the user for their name and welcome them to our application.

Unlike "cout", "cin" uses the greater than signs as it's stream operator '>>'.

Code:
string userName;
cout << "Please enter your name: ";
cin >> userName;
cout << "Welcome to my application " << userName << "." << endl;

Firstly, this creates a string variable called userName.
Second, it asks the user to enter their name.
Next it gets input from the user and stores it in userName.
Last it welcomes them to the application.

[color:d564="White"]Answers to last exercise

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
   string word1, word2, word3, word4;
   int myInt;
   float myFloat;

   word1 = "Monkeys";
   word2 = "had";
   word3 = "Bananas";
   word4 = "each";

   myInt = 3;
   myFloat = 3.5;

   cout << myInt << " " << word1 << " " << word2 << " " << myFloat << " " << word3 << " " << word4 << endl;

   return;
}
[color:d564="White"]
Exercise

Task: Get the user's name, age, height and weight. Use these values to fill in gaps in a custom story you made up, and tell this story to the user.

Your final code should be:

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
   string userName;

   cout << "Please enter your name: ";

   cin >> userName;

   cout << "Welcome to my application " << userName << "." << endl;

   return;
}
-Phew- Updated more will arrive.
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:20 pm

[color:f964="Red"][SIZE="6"]Lesson 6: Operators[/SIZE][color:f964="Lime"][1.6]

Operators are used to perform calculations or certain tasks on a set of data or variables. In this lesson I will cover the operators we will be using in further tutorials, but I may go over more later.

The '=' operator: assigns a value to a variable (e.g. x = 1)
The '+' operator: adds values together (e.g. 1 + 1)
The '-' operator: subtracts values (e.g. 1 - 1)
The '*' operator: multiplies values together (e.g. 1 * 2)
The '/' operator: divides values (e.g. 2 / 2)

[color:f964="White"]More operators

The '+=' operator: adds the following value to the variable (e.g. x += 1, this will add 1 to the value of x)
The '-=' operator: subtracts the following value from the variable (e.g. x -= 1)
The '*=' operator: multiplies the variable by the following value (e.g. x *= 2)
The '/=' operator: divides the variable by the following value (e.g. x /= 2)
The '++' operator: adds 1 to the variable (e.g. x++ or ++x)

[color:f964="White"]Logic operators

The '>' operator: if the value on the left of the operator is greater than the value on the right, then the condition is true
The '<' operator: if the left value is less than the right value, then the condition is true
The '>=' operator: if the left value is greater than or equal to the right value, then the condition is true
The '<=' operator: if the left value is less than or equal to the right value, then the condition is true
The '==' operator: if the left value is equal to the right value, the condition is true
The '!=' operator: if the left value is not equal to the right value, the condition is true

That is all for now. No exercises for these as they will be used in later lessons and exercises.

[color:f964="White"]Answers to previous exercise

Any variation to the following story, as long as values are taken from user and substituted into the story.

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
   string name;
   int age;
   float weight, height;

   cout << "Enter your name: " << endl;
   cin >> name;
   cout << "Enter your age: " << endl;
   cin >> age;
   cout << "Enter your weight: " << endl;
   cin >> weight;
   cout << "Enter your height: " << endl;
   cin >> height;

   cout << "There once was an adventurer called " << name << " who was " << age << " years old." << endl;
   cout << name << " loved to slay beasts. " << name << "'s height of " << height << "m" << endl;
   cout << "and " << name << "'s weight of " << weight << "kg was always a good help to this hobby." << endl;

   return;
}
Updated thanks!
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:21 pm

[color:5b4a="Red"][SIZE="6"]Lesson 7: Loops[/SIZE][color:5b4a="Lime"][1.7]

In C++, loops are blocks of code that are repeated over and over until they are not wanted anymore. These are used readily throughout programming and you will need to know how to use them.

In this lesson I will cover the while loop.

As the name suggest, this type of loop will repeat while a certain condition is met. As soon as this condition is not met, the loop ends and continues on with the next part of the code.
A while loop is like so:

while(condition)
{
//code here
}

For example, we may have a game character, and we want to run the game while the character is alive (health above 0) and when it dies, the game ends.
For this we could do the following:

Code:
void main()
{
   float health = 1000;
   
   while(health > 0)
   {
      health -= 0.1;
   }

   return;
}

The program will continue running, subtracting health from the player until it dies. Then it will continue with the remaining code, which in this case is "return" which will end the program.

More loops will come in further lessons.

[color:5b4a="White"]Exercise

Task: Make a loop that will add 1 to the health every loop until the health is above 500000, once it has done that, display a message to the user to let them know they are fully healed, then end the program.
Computer processors are fast, so they will probably be healed in less than a second.

Your final code should be:

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
   float health = 1000;
   
   while(health > 0)
   {
      health -= 0.1;
   }

   return;
}

Remember, computer processors are extremely fast and it will only take part of a second for it to loop through enough to get the health down to 0 in our example.
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:21 pm

[color:c7d2="Red"][SIZE="6"]Lesson 8: If Statements[/SIZE][color:c7d2="Lime"][1.8]

Many times in programming we will want to check if a certain condition is true before we execute a block of code.
This is how an if statement appears.

if(condition)
{
//code here
}

This is very similar to a while loop, but it will only execute the code once, unless the if statement is inside a loop itself.

Here is an example of an if statement.

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
   float health = 1000;
   int mana = 100;
   
   while(health > 0)
   {
      health -= 0.1;

      if(mana == 100)
      {
         cout << "You are healed for 100 health." << endl;
         health += 100;
         mana -= 100;
      }
   }

   return;
}

What this does, is while the character is dieing of poison as with our while example, if the character has enough mana to heal him/herself, (s)he will heal him/herself for 100 health, but then the mana will be lost and they will not be healed next time around as the condition will not be met.

[color:c7d2="White"]Answers to previous exercise

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
   int health = 0;
   
   while(health <= 500000)
   {
      health += 1;
   }

   cout << "You are fully healed!" << endl;

   return;
}
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:21 pm

[color:7868="Red"][SIZE="6"]Lesson 9: Functions[/SIZE][color:7868="Lime"][1.9]

This lesson will be a bit bigger than the previous lessons, as functions can be complicated for beginners. Also, I will add more than 1 exercise at the end of the lesson to help out.

When dealing with functions, we define them at the start of the code, right before "main".
However, if you would prefer all your functions to be under main there is an alternative.

Example of a function before main:

Code:
#include <iostream>

int AddNumbers(int a, int b)
{
   return a + b;
}

void main()
{
   return;
}

Example of a function after main:

Code:
#include <iostream>

int AddNumbers(int a, int b);

void main()
{
   return;
}

int AddNumbers(int a, int b)
{
   return a + b;
}

In the second example, we must declare the function at the top of the code, so if we call it during main, the compiler will know it exists.
We must then define it with it's instructions again at the bottom of the code so the compiler will know what the function is supposed to do.

Where your functions are defined is up to you, it makes no difference in the end.

[color:7868="White"]Format

Functions are very similar to commands, but we make them ourselves. They serve a particular purpose in our code and can make tasks easier. They save us from having to write out the same block of code multiple times.

A function consists of a data type, a name, a set of parameters and a set of instructions.

variabletype functionname(parameters)
{
//code
}

[color:7868="White"]How Functions Work

I will write an example function and then go over each part of it. I will create a simple function that will add two given numbers together.

Code:
int AddIntegers(int a, int b)
{
   return a + b;
}

[color:7868="White"]Function Data Types

When we define a function, it is done very similarly to defining a variable. With a datatype, followed by a name.
The datatype in a function is important. It lets C++ know what type of data our function will return. Return simply means
it will pass back a value to the point in the code where the function was called. If our function is not going to return
a value, we use the data type "void". An example where we wouldn't return a value is shown later in this lesson.

In this example, the function is of type int. This means that when the function is finished it will return an integer to the point where the function was called.
The integer we return is the result of the added numbers.

[color:7868="White"]Naming Convention

When naming functions, all letters are lowercase, except the first letter of each word which is uppercase.

The following are acceptable:

Add
AddIntegers
AddThreeIntegers

The name of a function doesn't change how the function works, but it is important to name it well so you will remember your function
and more importantly, what it does.

AddIntegers is the name of the function in our example. It serves no purpose other than letting us know what we want our function to do.

[color:7868="White"]Parameters

Parameters are variables we get when the function is called. These variables are used while the function is performing it's task.
If we want parameters, we make them. We can make as many parameters as we need, but adding 100 parameters is a bad idea and we
don't do it. If we don't want parameters, we leave the parenthesise empty.

In our example, "int a" and "int b" are the parameters. They are integers we get when the function is called. We need them to determine
the sum of two numbers we give to the function. Without these, we have no values to add.

'{' and '}' simply hold the set of instructions the function will perform between them.

[color:7868="White"]Returning Values

When we are finished with our function, we must return a value. If our function is of type "void" we aren't required to return a value, so we
just type "return;" to let C++ know we are done.

If our function has a type, such as "int", we must return an integer. If it is of type "float" we must return a float.

In our example we use "return a + b;". What this does is add the two integers, a and b from the parameters, together, into 1 integer and returns it.

[color:7868="White"]Calling Functions

To call a function in our code we use the following code:

Code:
AddIntegers(1, 2);

However, this will do nothing as we are not telling it what to do with the returned value.

If we wanted to store the sum of 5 and 12 into a variable called myInt we would use this:

Code:
int myInt = AddIntegers(5, 12);

In this example, we are telling C++ to store the returned value of AddIntegers into myInt, which in this case is 17.

And if we wanted to output the sum of 2 and 6 to the screen we would use:

Code:
cout << AddIntegers(2, 6) << endl;

In this example, we are telling C++ to draw the returned value of AddIntegers to the screen, which in this case is 8.

[color:7868="White"]More about the "main" function

Looking back at "main" you will notice the data type is "void". This simply means we will not be getting a returned value from the function, and return will simply end the function.
"main" does not require parameters in our examples so we don't need to worry about that.
The set of instructions for main is just all the code our program will execute before closing. Basically, the "main" function is our program.

[color:7868="White"]More on Functions

Taking the AddIntegers function further, we can make it draw the numbers to the screen itself instead of us having to do that manually.

Code:
void AddIntegers(int a, int b)
{
   cout << a + b << endl;

   return;
}

This time, we change the data type to "void" because we will not be returning a value, as the function handles the value itself.
The function again, takes our two parameters, adds them together, and draws them to the screen.
It then tells C++ that we are finished with "return".

Now when we want to draw this to screen instead of:

cout << AddIntegers(1, 2) << endl;

We can use:

AddIntegers(1, 2);

[color:7868="White"]Exercises

Task 1: Create a function that adds the strings "Hello" and "World" together with a space in between. HINT: Add the strings using the '+' operator, stores this new string in a variable, and displays the added strings to the screen.
Task 2: Create a function that will tell the user they are dead, as long as their health is 0 or less. You must return an integer from the function, 1 if the user is alive, 2 if the user is dead. HINT: Pass health to the function as a parameter to check. Use your function as the condition for the loop.
Task 3: Create a function that will get the users first and last name, combine them into one string and return it. Store the returned value into a variable. Draw this variable to screen.

If you get stuck on these exercises, go back a bit. If you still can't get it, message me or try continuing if you feel you will be ok.
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:22 pm

[color:65ea="Red"]Lesson 10: Arrays[color:65ea="Lime"][1.10]

[color:65ea="White"]What arrays are

Arrays are like tables of data. They store numerous amounts of information of ONE data type.
A 1D or One Dimensional array, is the array we will focus on now, which means it is an nx1 table.

Here is an illustration of a 5x1, 1d array.

Code:

---
|5|
|2|
|2|
|3|
|1|
---

This table represents a 5x1 array of "int" values.

[color:65ea="White"]How Arrays Work

To define an array in C++ we have multiple ways of doing it. But we will start with the easiest.

Code:
int myArray[5];
myArray[0] = 5;
myArray[1] = 2;
myArray[2] = 2;
myArray[3] = 3;
myArray[4] = 1;

Although we declared myArray with 5 values, we must take into account that "0" is the first value in an array. Therefore, the 5 values in our array go from [0] to [4].
Looking back at our illustration, this is what the table looks like.

Code:

---
|5|  0
|2|  1
|2|  2
|3|  3
|1|  4
---

Another way to make the same array above is like this:

Code:
int myArray[5] = {5, 2, 2, 3, 1};

This is much quicker and bears the same result.
These array values are exactly like variables and can be used in the same way.

Code:
cout << myArray[4] << endl;

The above code would draw a 4 to the screen.

[color:65ea="White"]Answers to previous exercise

Task 1:

Code:
#include <iostream>
#include <string>

using namespace std;

void AddStrings(string a, string b)
{
   string c = a + " " + b;
   cout << c << endl;

   return;
}

void main()
{
   AddStrings("Hello", "World");

   return;
}

Task 2:

Code:
#include <iostream>
#include <string>

using namespace std;

int CheckDead(int health)
{
   if(health <= 0)
   {
      cout << "You are dead" << endl;
      return 2;
   }

   return 1;
}

void main()
{
   int health = 1000;

   while(CheckDead(health) != 2)
   {
      health -= 1;
   }

   return;
}

Task 3:

Code:
#include <iostream>
#include <string>

using namespace std;

string GetName(string first, string last)
{
   return first + " " + last;
}

void main()
{
   string firstName;
   string lastName;
   string fullName;

   cout << "Enter first name: ";
   cin >> firstName;
   cout << "Enter last name: ";
   cin >> lastName;

   fullName = GetName(firstName, lastName);

   cout << fullName << endl;

   return;
}

If you managed to get all these exercises right, you are doing extremely well. If not don't worry. Look over these answers carefully and learn how it was done.

[color:65ea="White"]Exercise

Task: Create an array of strings with 3 elements. Fill this array with ANY first names, and draw these names to the screen.
Back to top Go down
Demyx
ForumModerator
Demyx


Posts : 371
Join date : 2009-12-19
Location : In my fucking pants come and tickle?

Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitimeSat Dec 19, 2009 11:22 pm

[color:9733="Red"]Lesson 11: Multi-dimensional Arrays

[color:9733="White"]What multi-dimensional arrays are

Like an array, multi-dimensional arrays are like a table of data, but they are more complex.
For a 2d array, we define it like so:

int myArray[3][2];

This will create a 3x2 table.

Here is an illustration

Code:

0 1 2
--------
|5|3|1| 0
|3|2|4| 1
--------

In this table we have 6 values. As with 1D arrays we start from 0. Each element from the first dimension, will have 2 elements from the second dimension.
This gives us:

myArray[0][0]
myArray[0][1]

myArray[1][0]
myArray[1][1]

myArray[2][0]
myArray[2][1]

[color:9733="White"]How is this useful?

Right now, they may not be as useful as you think, but next lesson you will see a way in which they can really be helpful.
So let's say we have 3 characters, and we wanted to store the type of sword and shield each character is holding.

For this, we can use a 2d string array.

string equipment[3][2];

We use a 3x2 array, because we have 3 characters, each with 2 names. As we said before, each element from the first dimension will have one of each element from the second dimension.

Code:
string equipment[3][2];
equipment[0][0] = "Wooden Sword";
equipment[0][1] = "Wooden Shield";
equipment[1][0] = "Iron Sword";
equipment[1][1] = "Iron Shield";
equipment[2][0] = "Steel Sword";
equipment[2][1] = "Steel Shield";

In this example, player 0 is holding a wooden sword/shield, player 1 is holding an iron sword/shield and player 2 is holding a steel sword/shield.

Like with 1D arrays, we can use the quick way.

Code:
string equipment[3][2] = {"Wooden Sword", "Wooden Shield", "Iron Sword", "Iron Shield", "Steel Sword", "Steel Shield"};

Not only can we use 2D arrays, but also 3D, which follow the same principles.

For example:

int myArray[2][2][2];

Is a 2x2x2 3D array.
It will have the following elements:

myArray[0][0][0];
myArray[0][0][1];
myArray[0][1][0];
myArray[0][1][1];
myArray[1][0][0];
myArray[1][0][1];
myArray[1][1][0];
myArray[1][1][1];

As you can see, each element in the first dimension has an element from the second dimension, and each element in the third dimension has an element from the third dimension.

[color:9733="White"]Answer to previous exercise

Code:
#include <iostream>
#include <string>
using namespace std;

void main()
{
   string myArray[3] = {"Bob", "Jim", "Mary"};

   cout << myArray[0] << endl;
   cout << myArray[1] << endl;
   cout << myArray[2] << endl;

   return;
}

[color:9733="White"]Exercise

Task: Create a 2D array that holds the age, amount of money and number of computers in household for 4 people.
Display the information for each person on one line each.

Sample output:

Name: Mary Age: 12 Money: $50 Computers in household: 4

[/FONT][/COLOR][/QUOTE]



[color:9733="Lime"]All the lesson has been updated I'll hope you enjoy the advices and tips thanks.
Back to top Go down
Sponsored content





Soul's C++ Tutorial. Empty
PostSubject: Re: Soul's C++ Tutorial.   Soul's C++ Tutorial. Icon_minitime

Back to top Go down
 
Soul's C++ Tutorial.
Back to top 
Page 1 of 1
 Similar topics
-
» Styx's leading tutorial
» Rock Gunz Reload Shot Tutorial

Permissions in this forum:You cannot reply to topics in this forum
RockGunZ :: Rock Gunz General Discussion :: Tutorials-
Jump to: