Complete Introductory Lesson on C++

.Judgement

Onyx user!
Reputation
0
First thing is first. You need to create your own folder so you can put all your C++ programming files that you are going to make in here. Goto where you would like to store your files like in the C: drive or on your desktop and right click your mouse and goto new then click on folder. Go ahead and name your folder anything you want like C++ or programming or any name.

Now open up your C++ compiler program. Once again I use Visual C++ 6.0 so the steps I will give on operating the compiler program will be on Visual C++ 6.0. However, any C++ compiler should work almost the same. I believe you can download an express version here.

This one.
Or:
This one.

Once you open your visual C++ program, goto file - new. Then goto the files tab and click on C++ source file. Then in the file name box, type in the name that you want your file to be called like Lesson1. Then in the location box, go ahead and find where you created your new folder and point it to that location.

Now we can begin programming. The very first line in all your programs will be this. #include <iostream> This is basicaly a header file that means input and output streaming. This is in order to use the command "cout" which is used to display text on your screen. The next line of command that you will use on most programs is int main() Don't worry about this as it just means that you will be able to view the results of your progam on your screen. The command "cout" is for when you want something to display on your screen and "<<" are called insert operations. ok, here is the very first program.
-------------------------
#include <iostream>
using namespace std;
int main()
{
cout << "This is my first C++ program";
}

------------------------

Your entire program after int main() must be between two braces {}. This is telling the compiler to do everything within the braces.
Also, after every command you give, you must place a semicolon after your command " ; "
Now every text that you want to be typed o your screen must also be between two quotation marks " "
I know this sounds complicated but believe me it will become alot easier once you get the hang of things

Now compile your program by pressing the compile or build button at the top or simply goto build on the top and click on compile lesson1. At the bottem of your screen you should see the message " 0 warnings and 0 errors" That means that you have successfully compiled your first program. Now you need to execute the program. Goto build again and then click on execute lesson1. Your screen should now read " This is my first c++ program"
Try creating your own small programs that displays any kind of text that you want on your screen.

{ = brace

cout = display message

<< = insert operator

// = one line of comments

/* more than one line of remarks and then */ to close your remarks

\n = make a new line

\t = tab

For example you can have a program like this
-------------------------
#include <iostream.h>
void main()
{
// this is how you make a comment

/* this is how you make
more than one line of comments */

cout<< "Hi there\n";
cout<< "How are you?\n";
}
---------------------------

The \n will create a new line for you so the screen will read

Hi there
How are you?

Without the "\n" then the screen would have shown
Hi thereHow are you?





Point #2

Variables =
1. letters to store data
2. Must be declared before you can use it.

Data Types = These are what you use to declare your vaiables.

Double - To store number that contain decimal places (23.4453, 7889.00784)(64 bit I believe)
Float - Also to store numbers that contain decimal places(2.54, 67.8) (32 bit I believe)
Int - To store whole numbers (integers) (1,2,3..)
Char - To store characters (A,B,C..)
Bool - Ture or false 0=false 1=true. (Almost no one rally uses this data type)


Math operations

= store
+ add
- subtract
* multiply
/ divide


ok now its time for another program. This program will declare two numbers (5,6) as intergers and also declare two numbers as results (sum, product).

int num1=5; This means that you are declaring "num1" variable to be an integer type and for it's value to be 5.

int sum; This means that you are declaing "sum" variable to be an interger no amtter what is palced inside of it.

sum= num1 + num2; This means that whatever the result of adding the two numbers that were stored into num1 and into num2 will be placed into the variable "sum"

----------------------------
#include <iostream>
using namespace std;
int main()
{
int num1=5;
int num2=6;
int sum;
int product;
sum = num1 + num2;
product = num1 * num2;
cout<< "Sum = "<<sum<<"\n";
cout<< "Product = "<<product<<"\n";
}
---------------------------
After compiling your program, you should get a message on your screen that says
Sum = 11
Product = 30

Now you guys should try to make a program that has three numbers (3.5,2.4,7.8) and find the sum, product, and average of all three numbers. As a hint, use "float" or "double" as your data type since now you are using decimals.



Point #3


Cin basically means to read from the keyboard. Notice how we used cout with the insert operation "<<" With cin, we use ">>" .

One note that I wanted to show everyone because it was already mentioned before, "endl" is the exact same thing as "\n" which means make a new line except endl doesn't need to be between quotations as "\n" does.
Also, remember before when I declared a variable, int x; or int num1; I then made a new line to declare another variable. You can declare all your variables in one line as long as they are all of the same data type and you seperate them with commas. For example,
int num1;
int num2;
int sum;
int product;

or you can just type

int num1,num2,sum,product;
------------------------------------
#include<iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter a Whole number:";
cin>>num1;// reads num1 from computer
cout<<"Enter another Whole number:";
cin>>num2;//reads num2 from keyboard
cout<<"First number:"<<num1<<endl;
cout<<"Second number:"<<num2<<endl;
}
----------------------------------
That program will display the two numbers that the user entered from the keyboard.

Increment operator ++ means to increase the variable by one. It can be typed as either x++ or ++x

Decrement operator -- means to decrease the variable by one. It can be typed as x-- or --x

------
int x=5;
x++; // increase x by one
cout<<x<<endl;
------
The computer would display 6

------
int y=15;
y--; // Decrease y by 1
cout<<y<<endl;
------
The computer would display 14

------
int x=5, y=10;
y=x; //each variable can store only one value
cout<<x<<endl<<y<<endl;
------

The computer would display 5 and 5. X first started off as a value of 5 and Y started as a value of 10. Then we told the computer that whatever is stored in X to put that same value in Y. That is why now Y=5 which is the same value of x.

------
int x=2;
int y=12;
y=x++; // copy X into Y first then increase x by one
------
The answer would be x=3 and y=2. Y is equal to whatever x is at that moment which is 2. Then x gets increased by one after that so X then equals 3.

------
int x=10;
int y=15;
x= ++y; //increase Y first then put that value into x
------
The answer would be x=16 and y=16. Since the two postive signs ++ are before the Y variable, you increase Y first before anything. Then X will equal whatever the new value of Y is which is 16. And since Y was increased to 16 before it was placed inside the X variable, now Y is equal to 16 also.
Try this one for yourselves

------
int x=2;
int y=12;
x= --y;
------


Loops - Loops repeat the same code more than one time. There are 3 loops that are used in C++ programming. Right now I am going to go over the most common loop which is the "for" loop.

For loops can be used if you know the number of loops (repetitions). The condition is checked before the loop starts.

for(int x=1; x<=3; x++)
cout<<x<<endl;

int x=1; This is the initialization of x. X is going to start as number 1. However, if you already have declared int x; earlier outside of the loop then all you would need to type is x=1;

x<=3; This is the condition. As long as x is less than or equal to 3, the loop will continue.

x++ This is the step. This means that x will increase by 1 until the condition is met or over with.

This is what would be dispalyed on your screen.
1
2
3
------
Now try this one

for(int x=2; x<4; x++)
cout<<x<<endl;

This is what would be displayed
2
3
----
Now 4 would not of been displayed because the condition is x<4. As long as x starts at 2 and is less then 4 then what will be printed is 2 and 3.
--------------
for(int x=5; x>10; x++)
cout<<x<<endl;

On this one, nothing will be printed because x starts at 5 and the condition says that x>10 which means that x has to be greater then 10. Since x starts at 5 and is NOT greater then 10, then nothing gets printed out on the screen.
---------------
for(int x=5; x>2; x--)
cout<<x<<endl;

This is what would be printed out
5
4
3
-----------------
for(int x=1; x<=5; x+=2)
cout<<x<<endl;

This is what would be printed out
1
3

Now remember earlier I mentioned about compound assignments.
x=x+2 can also be written as x+=2.
So that means to increse x by 2 every step. So x starts off as 1 then gets incresed by 2 so x would then be 3. 1+2=3. Then x gets incresed by 2 again so x would then be 5 but 5 doesn't get printed on the screen because the condition says that x has to be less then 5 x<5.

--------------
ok, try these 2 programs.

---------------------
#include<iostream>
using namespace std;
int main()
{
for(int x=1; x<10; x++)
cout<<x<<endl;
}
-----------------------
The output would be
1
2
3
4
5
6
7
8
9

Remeber, if you are using DevC++ or if your output window disapears fast, add this extra line of code before the last brace
system("pause");
-------------------------
Now try this program

------------------------
#include<iostream>
using namespace std;
int main()
{
for(int x=1; x<=10; x++)
cout<<"My name is Spidereon"<<endl;
}
----------------------
That will print out "My name is Spidereon" 10 times

Now if you want something to repeat forever, then you just take out the condition part and add an extra semicolon ";" at the end of the step
----------------------------
#include<iostream>
using namespace std;
int main()
{
for(int x=1; x++; )
cout<<"My name is Spidereon"<<endl;
}


arrays are a collection of a variable type.

int myarray[19];

that would make an array of integers with 20 integers, startign with myarray[0] all the way up to myarray[19]
that number (0-19 in our case) is often called the index

heres soem sample code:
#include <iostream>
using namespace std;
int main()
{
int myarray[9];

for(int i=0;i<9;i++)
{
cout << "\nEnter the number for index " << i << endl;
cin >> myarray;
cout << "\nmyarray index " << i << " is equal to " << myarray << endl;
}
}

so i'm going to cover some more stuff on classes. lets talk about contructors, destructors, and accesabilty levels.

contructors are called whenever you make an object of that class. for example:

#include <iostream>
using namespace std;

class test
{
public:
test();
};

int main()
{
test mytest;
}
test::test()
{
cout << "Contructor is called\n";
}

so the contructor is always the exact same name as teh class and it does not have a data type(void, int, ect.)
it is ALWAYS called whenever teh class is made. so in this case, when teh class is made, it couts a message taht says, "constructor is called"

now destructor is called when teh class is destoryed. if we were to add a destructor to our program, it would be called at teh end of the function int main()

a decontrutoc is the name of class preceded by the ~
in our case it would be ~test()

heres the code:

#include <iostream>
using namespace std;

class test
{
public:
test();
~ test();
};

int main()
{
test mytest;
}
test::test()
{
cout << "Contructor is called\n";
}
test::~test()
{
cout << "Destructor is called\n";
}

now public: and private: and protected: are the parts of classes taht determien what can access the functions, variable, ect.

if it is public, other functions can access the following code

if it is private, only the class can access the code

there is something called protected, but we'll get more into that as we cover inheritance.

typically, you keep the functions public, so taht they can be accessed and you keep teh variables private so they are protected from direct contact.


if you are confused by OOP, don't worry. things will become more clear as you keep playing with c++ and you keep learning.


classes are also known as object oriented programming.

so lets go over some terminology and understanding.

what is a class?

Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to build and design it. A car has engineering drawings and blueprints. The entire object has to first be initiated and designed. The pedal hides the complex mechanisms taht actually make the car go faster, just as the brake pedal hides the mechanisms that slow teh car. the steering wheel hides teh mechanisms taht turn the car and so on. this enables people with little or no knowledge of cars to drive a car easily.

Lets use our car example to introduce classes. Performing a task in a program requires a function. The function describes the mechanisms that actually perform its tasks. The function hides from its user the complex tasks that it performs. A class houses multiple funtions, just as a car's engineering drawings house the design of an accelerator pedal. A function belonging to a class is called a member function.

Remeber, you can not drive an engineering drawing of a car. someone has to build it. That is one reason why c++ is known as object oriented. You can build many cars from teh same engineering design the same way you can build many objects from the same class.

we will begin to program a very simple grade book.

heres our code:

#include <iostream>
using std::cout;
using std:: endl;

class GradeBook
{
public:
void displayMessage()
{
cout << "Welcome to the Grade Book!" << endl;
}
};

int main()
{
GradeBook myGradeBook;
myGradeBook.displayMessage();
return 0;
}

now lets go over the code.
we include the standard #include code and teh using std::cout and using std::endl
after that, we define our class with the class GradeBook command. this makes a class called GradeBook.
we use an opening brace. then there is a public: this means that any code that follows is public. public means it can be accessed by other classes and functions.
void displayMessage() is a function within a the class or a member function. the opening brace defines the class. teh function simply couts a message.
clossing braces follow. be sure to put a semicolon after teh classes closing brace.
then our main function begins.
it creates a GradeBook object called myGradeBook. it similar to how int myinteger would produce an integer called myinteger.
it then calls a member function within teh class called displayMessage();


so that is basically how classes are initialized and used. obviously, classes can become extremely complex. in later lessons, we will make classes taht are much more difficult. btu for now, i just want u to understand the CONCEPT behind classes and OOP.

Few Notes About Classes:

Classes are the difference between C and C++. The advantages of C++ over C include:
Encapulation - Data and functions are stored in one container
Inheritance - Child classes will share data and functions from parent classes
Polymorphism - Functions can be overloaded inside classes.

Constructor - A function that has the same name as its class. If the class name is PSP then the constructor name will be PSP()

- Will be called automatically when an object is created.
- Can accept parameters.
- Can not return
- Can be overloaded


Destructor - A function that has the same name as its class with a ~ before it. (I think ~ is called a tilde). If the class name is PSP then the destructor name will be ~PSP()

- Will be called automatically when the object is destroyed.
- Cannot accept parameters
- Cannot return
- Cannot be overloaded

Destructors are mainly used to free up the used memory.


"if" statements. I am going to quickly go over compound assignments and relational operations and also a couple of notes on "if" statements

Compound assignments is just something to shorten up your variable code.

x=x+5; ---> x+=5;
x=x/y; ---> x/=y;
x=x-10; ---> x-=10;
x=x*y; ---> x*=y;


Relational Operators - These are mostly used with "if" statements.

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal


Logical operators - Also used with "if" statements

&& and
|| or
! not

A couple of thing I wanted to mention about "if" statments. First, you do not have to use braces "{}" after your "if" command or "else" command if you only have one line of code following the "if" or "else" command. Another thing is that you do not have to use the "else" command with the "if" command if you do not want to either. You can simply use pure "if" statements with no "else" in your program if you don't need to. I am going to write down the code that Yesenia sent me because she was having trouble finguring out. She did a real good job on it but there was alot that she could have cut out of her code to make it work simplier. Here is her code
---------------------------------
#include <iostream>
using namespace std;
int main()
{
int Post= 0;
char Person = 'b' ;

cout << "How Many posts do you have in pspiso forums?: ";
cin >> Post;


if ((Post >=0) && (Post <= 4))
{
Person = 'n';
}
else
{
if ((Post >=5) && (Post <= 100))
Person = 'n';
else
{
if ((Post >=101) && (Post<= 500))
Person = 'n';
else
{
if ((Post >=501) && (Post <= 10099))
Person = 'o';
else
Person = 'x';

}
}
}
if (Person == 'n')
{
cout <<"Hi you are a N00b,want to know why? its because you have " << Post << " posts in pspiso forums";
}
else
{
if (Person == 'n')
cout <<"Hi!you are a newbie because you have " << Post << " posts in pspiso";
else
{
if (Person == 'a')
cout <<"Hi! You are a expert cause your " << Post << " in pspiso";
else
if (Person == 'o')
cout <<"Wow!!! you have many posts... good for you big_smile " << Post << " is alot of posts.. GOOD JOB ";
else
if (Person == 'x')
cout <<"um... do you think I am stuped? you are do not have " << Post << " in pspiso... hmm.. I expected better from you o_O";



system("pause"); //show output screen longer
}
-------------------------------------

Now here is a more simplified version of the code with no errors.

------------------------------------

#include <iostream>
using namespace std;
int main()
{
int Post;

cout << "How Many posts do you have in pspiso forums? ";
cin >> Post;
cout<<endl;

if ((Post >=0) && (Post <= 4))
cout <<"Hi you are a N00b!,want to know why? its because you have " << Post << " posts in pspiso forums"<<endl;

if ((Post >=5) && (Post <= 100))
cout <<"Hi! You are a newbie because you have " << Post << " posts in pspiso"<<endl;

if ((Post >=101) && (Post<= 500))
cout <<"Hi! You are an expertcause you have " << Post << " posts!"<<endl;

if ((Post >=501) && (Post <= 10099))
cout <<"Wow!!! you have many posts... good for you big_smile " << Post << " is alot of posts.. GOOD JOB "<<endl;

if (Post >=10100)
cout <<"um... do you think I am stupid? you are because you do not have " << Post << " in pspiso... hmm.. I expected better from you o_O"<<endl;


system("pause"); //show output screen longer
}
----------------------------------------


Here is a quick lesson in modulus.

% = Modulus - This basically means remaining.

4%2 = 0 2 goes into 4 2 times with a remainder of 0
5%2 = 1 2 goes into 5 2 times with a remainder of 1.

Modulus are used to check if an inputed number is even or odd. If modulus = 0 then it would be an even number. If modulus =1 then it would be an odd number. Modulus can also be used to create rows of numbers.

Here is a quick program to check if an inputed bumber is even or odd.
-----------------------------
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a whole number: ";
cin>>num;
if(num%2 == 0)
cout<<"Even"<<endl;
else
cout<<"Odd"<<endl;
}
-----------------------------------
As mentioned earlier, modulus can also be used to create rows of numbers of your own specifications.
This program will create a row of 5 numbers then skip to the next line for another 5 numbers and repeate until the condition of 15 numbers are met.

for(int x=1; x<=15; x++)

This is a simple for loop that will have our number begin at 1 and increase by one every loop until the condition of 15 is met.

{
cout<<x<<"\t";
if(x%5 == 0)
cout<<endl;
}
The loop will start by printing x which is 1 right now and then place a tab or space after that number. That is what "\t" means.
So then you will have 1 then a space after it. Then the loop will check if 1%5 == 0. If it does = 0 then it will skip to the next line. But 1%5 does not = 0. Rembember that only 5%5 can = 0 because 5 goes into 5 one time with 0 remaining.
So the loop will continue to print numbers with a tab after it until it reaches 5.
So you will have 1 2 3 4 5. Once the loop reaches 5, it will go ahead and start another line because of the cout<<endl; line right after the if statement.
Then you will have something like this
1 2 3 4 5
6 7 8 9 10

And again, once you reach 10, 10%5 = 2 with remainder 0 again so again it will skip to the next line and repeat until it reaches 15 which is the loop's condition.
So the output will loop like this.

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

------------------------
#include <iostream>
using namespace std;
int main()
{
for(int x=1; x<=15; x++)
{
cout<<x<<"\t";
if(x%5==0)
cout<<endl;// skip to next line
}
}
-----------------------------

Well as long as I have some time, I will continue the lessons especially for those who might view these C++ lessons later on in the future. Remember that if you want to really program homebrew for the psp and you don't want to just learn lua, you will need to learn C++ before attempting to create any advance programs for the psp.

Here is a quick lesson in factorials.
Factorial is a math function represented by a !. Factorial of a number is the number times its previous number times its previous number all the way down to 1. For example,
1! = 1
2! = 2*1 = 2
3! = 3*2*1 = 6
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120

here is a simple program to find the factorial of any number that the user inputs.
---------------------------------------
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
int factorial = 1;
for(int x= num; x>1; x--)
factorial=factorial * x;// you can also say factorial* = x
cout<<"Factorial of "<<num<<" Equals "<<factorial<<endl;
}
-----------------------------------------

int factorial = 1;

The reason why we start the variable factorial with a value of 1 is because 1 times anything equals the original value of the number.

for(int x= num; x>1; x--)
factorial= factorial*x;

The for loop starts as x equaling whatever number that the user has inputed. Lets pretend that the user inputed the number 3. So x starts off as 3. Then the condition is checked. As long as x is greater then 1 then the loop will continue. Since x starts as 3 for our example, that means that x is greater then 1. x-- simply means that once one cycle of the loop has finished, x will decrease by 1 for the next cycle. So right now we are pretending that the user inputed 3 and x=3. The next line in the for loop says:

factorial = factorial*x;

Remeber that we started the original value of the variable factorial to equal 1. So now the new value of the variable factorial will = 1*x. Since we are pretending that the user entered 3, then the new value of the variable factorial will be 1*3 which equals 3.
Now we go back to the for loop and continue the next cycle of the loop. Since x started as 3, now the step of the loop says x-- which means to decrease x by one. So now x = 2. So now the variable factorial already has a value of 3 in it from before. Now the new value of the variable factorial will equal whatever the old value was which is 3 times the new value of x which is now 2. so factorial = factorial*x will now be factorial = 3*2 which equlas 6.
Now again we go back to the for loop and x-- means that we decrease the value of x by 1 again. So now x = 1. However, the condition of the for loop states that x must be greater than 1 for the loop to continue. So that means the loop has finsihed. So the end value that is stored in the variable factorial will be 6.

So what will be displayed is

The factorial of 3 equals 6

3 represents the variable num which is the number that was inputed by the user and 6 represents the value that was last stored in our variable factorial. Remember, that we used the number 3 as an example. You can enter any number that you would like in this program and the program will tell you the factorial of any number you entered.


Few Notes:

Double - To store number that contain decimal places (23.4453, 7889.00784)(64 bit I believe)
Float - Also to store numbers that contain decimal places(2.54, 67.8) (32 bit I believe)
Int - To store whole numbers (integers) (1,2,3..)
Char - To store characters (A,B,C..)
Bool - Ture or false 0=false 1=true. (Almost no one rally uses this data type)
"

You need a better & more detailed description of data types. This is vital to coding.

Also you explain things a little bad. To tell someone a char is just for storing characters will lead to bloated code. A person should learn that a char or byte is a varible that holds a 8 byte value (range of 0 to 255 or -127 to 127) first before they learned they can be used for strings (non asian set which is double byte). Another note, booleans (bool) are actualy quite often used in C++.

Also float are generaly always 32bit & double is 64bit.


here is the basics of string or character functions:

strlen() = Gives you the length of the array
strcpy() = Copy one array to another array(overwrites the old array)
strcat() = To add the contents of one array to the end of another array.
strcmp() = To compare 2 arrays.

Don't forget in order to use these functions, you need to include #include<string> right below you #include<iostream>

I went over the string function strcmp() in the previous posts while going over itsallthesame2me's RPG that he is making. However, i wanted to give another example of this function. strcmp() is used to compare 2 character strings and strcmp() actually means "string compare". It is mainly used to compare 2 words that are either already built in the source code or even 2 words that the user himself inputs. Once the computer can verify that the 2 words are identical or not, then the computer can execute any code thereafter. This next program that I am going to write is like a password program. The program will tell you the test answers (or anything that you want) if you get the password correct. There is going to be one character array built into the code called "pspiso" and that is going to be our password. The program will then ask the user to input the unknown password. The program will then check if the password entered by the user matches our password of "pspiso" in our code using the strcmp() function. If the user does input the correct password, then he gets the test answers thats stored in the code. We will give the user 3 chances to type the correct password using the for loop I explained earlier. After the user uses all of his 3 chances up, then the program will say "Sorry but you used up all your chances already" and then the program will quit.
--------------------------------------------
#include<iostream>
#include<string>
using namespace std;

int main()
{
char pass[] = "pspiso";
char userpass[20];

for(int x=1; x<=3; x++)
{
cout<< "Please enter the password to cheat and see the test answers o_0 \n";
cin>> userpass;
if(strcmp (pass,userpass) == 0)
{
cout<< "HaHa, here are the test answers. Shh, don't tell the teacher! \n";
cout<< " 1.B 2.D 3.A 4.A 5.B \n";
break;// exits the loop because user got password correct
}
if(x<3)
cout<< "Invalid password. You have "<<3-x<<" chances left \n";
else
cout<< "Sorry no more tries. Goodbye \n";
}
}
-------------------------------------------

ok we start off with are 2 character arrays
char pass[] ="pspiso"; and char userpass[20];
The pass[] array is our password and the userpass[20] array is the array we set up for the user to input. Don't forget that "20" in the array means that we are allocating 20 spaces of memory for the user to enter. if you would like the password to be longer then 20 spaces, then you can just make it a higer number. Those are the 2 arrays that we will be comparing to see if they are the same.
Next we start with the for loop. for (int x=1; x<=3; x++) Since we didn't declare x earlier as an interger, we can do that here inside the for loop by typing "int x=1;". Next is the condition. As long as x is less than or equal to 3, then the loop will continue to do what is inside the 2 {} braces underneath the for loop. The last part of the for loop is the step x++. This means that everytime the code is executed inside the 2 {} breaces under the for loop, then go ahead and increase x by 1 after each loop. So that means that everything will be printed out or executed between the first brace under the for loop and the second to last brace exactly 3 times unless the loop breaks before that (if the user enters the correct password, then there is no need to continue the loop and the code "break;" will break out of it.) The reason why i say bewteen the first { brace under the for loop and the second to last } brace of the code is because the last } brace belongs to the int main() { brace.

So the first thing the program will ask you is for the password. Then the program will compare the 2 arrays ( the password array and the user entered password array) and see if they are identical using the code if(strcmp (pass,userpass) == 0)
If the 2 arrays or passwords are identical then the program will do whatever is printed between the next 2 { } braces under the if statement. In our case, the program will print the test answers for you. Once the test answers are printed out, the "break;" command will exit you out of the loop since there is no more need for the for loop to continue to its next step since the program is done now.
However, if the user does not enter the correct password, the the program will goto the next if statement if(x<3)
This means that if the for loop has not reached its third loop yet (thrid try), then print out "Invalid password, you have __ tries left. The reason why I have 3-x in the code is because x starts off as 1 and after each loop x increases by one to 2 then to 3. So if x starts off as 1 then 3-1 equals 2 turns left. If x then is 2 then 3-2 equals 1 turn left.
Then on the last part of the code you have else cout<< " Sorry no more tries"; which means that if x is anything other than x<3 then print out that statement. That means once the for loop and x and tries reach 3, then the user has run out of chances and that "Sorry no more tries" statement will appear.
Dont forget to add the code system("pause"); before the last brace if you are using dev-C++
Try going through each line of code if you don't understand something.


Ups to Spideron - pspiso (He created it, I just copied it and posted it here)
 
Since you copy/pasted this tutorial, would you mind giving FULL credits to the creator?
 
Nice guide :) Tempted to try & learn it now :p
 
Back
Top