C++: Main. Booya!
Introduction to variables and datatypes
Let's go over some of the datatypes of C++.
bool- Evaluate to either "true" or "false," and take up 1 byte of memory
int- These represent any whole number,no decimals allowed, and take up 4 bytes of memory. Can handle then numbers -2,147,483,648 to 2,147,483,647.
char- These represent any character on a keyboard. Characters must be placed in single quotations, like these. 'A' '~' '7' and take up 1 byte of memory.
short- These are like Int, but take up only 2 bytes. Can handle the numbers -32,768 to 32, 767.
long- These represent ...ah... don't bother with these....just use int.
float- These represent decimal characters, and take up 4 bytes of memory. Can handle any number from +-(1.2 x 10^-38 to 3.4 x 10^38)
double- Like a float, but take up 8 bytes of memory. Can handle any number from +-(2.3 x 10^-308 to -1.7 x 10^308)
string- holds a string of characters that form what we like to call "words." You must have #include<string> in your header in order to store strings into variables.
Examples of the above:
bool = true
int = 25
char = 'B'
short = 120
float = -54.67
double = 129875647212309.17
string = "what in the world!?"
Now, with that out of the way, we move onto variables.
Remember algebra, when you had those crappy questions like 3y - 9 = 12 and you had to figure out what "y" was equal too? Well, "y" was the variable. Well... what exactly is y equal too? In the given expression, "y" would be equal to 7... because 3*7-9 = 12. So, we can say the following;
y = 7
Now, whenever we use the variable "y" the program will instantly know that we mean "7."
In C++ however, we must tell the program which type of data is represented by the variable, such as int, double or char. We must also be careful how we name our variables. For example; Names MUST start with either a letter or an underscore character, you cannot use C++ keywords as variable names and C++ is case sensitive! Here are some examples of how to properly initialize variables.
int _number = 10; //creates a variable of int type, named number with a value of 10
int cash = 40; //creates a variable of int type, named cash with a value of 40.
string name123 = "johnny"; //creates a variable of string type, named name, with a value of johnny
char firstLetter = 'j'; //creates a variable of char type, named firstletter, with a value of j
double money = 12.2543; //creates a variable of double type, named money, with a value of 12.25.
*remember, we need to put the semi-colon at the end of the declaration so that C++ knows we're done with our statement.
If you grow tired and weary of typing the same datatype declaration over and over, you can use typedef to change it, such as doing something like...
typedef double d; //now C++ recognizes "d" as double, so you can type d name = 100.12.
Now to try out some stuff.
Open up C++ and include all of the appropriate header files and namespace, if you forget, here it is..
using namespace std;
int cash = 20;
string name = "Johnny";
cout << name+" only has ";
cout << cash;
cout << " dollars in his checking account."<<endl;
Mess around with the variables a bit and try to get a feel for them using the statements. As homework, create a blank cash sum and name, like this...
then "ask" the user to enter in an amount using a cin statement, and then display their name and the amount they have to the console window.
*NOTE- If you only want to ONLY allow positive numbers, you can put "unsigned" before the datatype, such as
unsigned int cash = 40;
*NOTE- If an integer goes past it's max number, it wraps back to the beginning.
*NOTE- Knowing the amount of memory a variable takes up is important for using a function called sizeOf() which you'll learn about later, which basically parses through information based on it's size.