Hi guys, this is another c++ tutorial. (the 2nd one) If you haven't viewed the 1st one, please do so now at:
http://www.mpgh.net/forum/161-progra...llo-world.html
This tutorial is meant for those who would like to learn c++, but it's either too hard, or are clueless on where to learn from.
-----------------------------------------------------------------------------
Tutorial 1: Learning the very basics of C++, creating your very own first c++ program; learning to print messages to the screen.
Tutorial 2: "learning" variables and learning how to get a person's input. (math basically.

)
--------------------------------------------------------------------------
Let's get started. Now think back to Pre-Algebra/Algebra 1 when variables were introduced to you. It's the same in c++ as it is in math. Variables are used to store something in it(usually numbers).
Simple Algebra problem:
2x = 10. <- Solving this means that x equals 5. The number 5 is stored into the variable x.
Just like in math, we can also do this in programming.

---------------------------------------------------------------------------
Open your compiler (Visual Studio C++ 2010 is the one we're using) and create a new console application. Title it w/e you would like.
Next, this is the current code you should have:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
Now what we would like to do is to store a number, an integer to be specific, to any variable. But for this lesson, I'll pick x as our variable.
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int x;
}
Now the only thing you should have INSIDE your int main() function is int <variable>;. In this case, it's int x;
If we just put <x> instead of <int x>, we would have an error. Why? Integer isn't the only type there is. There is char, string, DWORD, etc. The compiler will be confused on what you're declaring x as. Therefore, we must put int x; to tell our compiler that it's an integer.
Now copy this code:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "We will be using a function to get the user's input. " << endl;
cin >> x;
}
Everything in this code has been reviewed except for 1 thing, cin >> x;
I will be explaining it now:
-----------------------------------------------------
cin >> x;
wha? why are the 2 arrows going the opposite way from cout <<? the << means something with cout means something is being outputted.
cin with the 2 arrows going >> means something will be inputted into storage type you use. (int, char, string, etc)
cin << won't work; you can try, but it just won't work!
cin basically means to input something in. Whatever number you input in, will be the number for x.
HOWEVER, it must be within a max number's limit, or it will restart to 0-> 2147483647(max number)
Let's say i input "534" into our variable 'x'. X is now also known as "534". How can I prove this? Read on to see!

----------------------------------------------------
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "We will be using a function to get the user's input. " << endl;
cin >> x;
cout << "The value of x is: " << x << endl;
cin.get();
return 0;
}
I will now explain the new line that is unknown to you guys.
-------------------------------------------------------
cout << "The value of x is: " << x << endl;
As you learned cout << before, you should know what cout does; it prints anything in quotes to the screen. Whoa, wait. Why is our variable 'x' not in quotes!?!? If you did put 'x' in quotes, you would get the letter "x" printed out, instead of the value stored in x. so in order to get the number stored in x out to the screen, we put it there w/o the quotes to give us the value of x.
----------------------------------------------------------------------------------
Note: I realized i didn't explain what endl; meant. It just means end line. It's used so that if there are multiple lines of cout << used, it will display it in a new line, insteadofdisplayingitlikethis.
-----------------------------------------------------------------------------------
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "We will be using a function to get the user's input. " << endl;
cin >> x;
cout << "The value of x is: " << x << endl;
cin.get();
return 0;
}
So run this and you'll get a console window up. type in any number for x, and it will be shown.
Example:
I input "54353"
it will output:
The value of x is: 54353
--------------------------------------------------
That's it for this lesson today!

I hope this helps others who want to learn c++ or just ones who were confused.
If this helped, and you liked it, press the thanks button below!

----------------------------------------------------
Next tutorial:
Tutorial 2 1/2: using math operations with cin and cout. (I thought that combining this into cin/variables would be too long, so i cut it into 2 lessons.)
Enjoy!
