Wednesday, March 26, 2008

User Input and Variables
Made by Fuse and Phial
I like shrimpies.

Promoted Sites:
www.NextGenScene.net



What are Variables?:
Variables store data in the computers memory kinda like we store numbers in our head. If I told you to remember the number 5 and then I told you to remember the number 16 you have just stored those two numbers in your memory. And if I told you to add the number 3 to the first number you should be able to come up with the number 8. Variables work just like that, in fact the same process can be done in c++ like this

a = 5;
b = 16;
c = a + b;

How to Declare a Variable:
To declare a variable you first start with its type, followed by the name and a semicolon. You can also initialize a variable when you create it. This is done exactly the same way as creating a variable but the name is followed by and equals sign and the value.

The following example creates two integer variables.
int aVariable;
int anotherVariable = 10;

In the example aVariable is created as an integer type. This means that it can only hold an integer value. Since aVariable is not initialized it could hold any value at the moment. This could cause some strange outputs if you forget to initialize it before you use it. To solve this problem we create anotherVariable and initialize it to 10. Now we can use it and know what value it holds. Initializing variables is good programming practice, and it reduces the headaches of debugging strange outputs.

Listing 1.1
//Demonstrates using variables and Initializing variables
#include <iostream>
using namespace std;

int main()
{
int aVariable;
int anotherVariable = 10; //Initializing a value to anotherVariable

aVariable = 0; //Assigning a value to a variable

cout << "aVariable: " << aVariable << endl;
cout << "anotherVariable: " << anotherVariable << endl;

system("Pause");

return 0;
}


Output:
aVariable: 0
anotherVariable: 10


Data Types:
At this point we have only gone over one example of a data type called an integer, lets go over some other data types in detail.
Listing 1.2
Name
Description
Size
Range
char
Character or small integer.
1 byte
signed: -128 to 127
unsigned: 0 to 255
short int
Short integer
2 bytes
signed: -32768 to 32767
unsigned: 0 to 65535
int
Integer
4 bytes
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int
Long integer
4 bytes
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool
Boolean value. Takes one of two values, true or false.
1 byte
True or False
float
Floating point number
4 bytes
3.4e +/- 38 (7 digits)
double
Double precision floating point number.
8 bytes
1.7e +/- 308 (15 digits)
long double
Long double precision floating point number. 8 bytes
1.7e +/- 308 (15 digits)
wchar_t
Wide character
2 bytes
1 wide character

You might be looking at this going what the hell am i looking at, I'm going to explain the best way i can think of, and that is with a example program.
Listing 1.3
//Demonstrates the sizes of different data types
#include <iostream>

using namespace std;

int main()
{
//Some values may differ depending on your system.
cout << "size of int: " << sizeof(int) << " bytesn";
cout << "size of short int: " << sizeof(short int) << " bytesn";
cout << "size of long int: " << sizeof(long int) << " bytesn";
cout << "size of double: " << sizeof(double) << " bytesn";
cout << "size of float: " << sizeof(float) << " bytesn";
cout << "size of bool: " << sizeof(bool) << " bytesn";
cout << "size of char: " << sizeof(char) << " bytesn";

system("Pause");

return 0;
}

Output:
size of int: 4 bytes
size of short int: 2 bytes
size of long int: 4 bytes
size of double: 8 bytes
size of float: 4 bytes
size of bool: 1 bytes
size of char: 1 bytes


What is User Input?:
User input is a way of letting a user interact with your application, whether that is by using a keyboard, mouse, or other input device. Letting users interact with your application by inputting values of their choice increases the usability of the program. The keyboard is the most commonly used device for input into an application, and so that is what we will cover here.
In later lessons you will learn how to read input from a file. You will quickly learn that letting users input values of their choice opens up a whole new world of problems, from wrong type of input, to too much input. Later we will cover how to protect yourself against these bad inputs.

Input via the keyboard is stored in an input stream. Data is stored here for you to act upon how you want. To read in a value from the stream you first specify the input stream you want access to, followed by the extraction operator, and finally the variable to store the value in.

cin >> aInt;

You may be wondering what cin is. It's a stream object that is connected to keyboard input, it is automatically defined for you in the std namespace. A source of confusion for some beginners is the extraction operator (>>). It looks similar to the insertion operator (<<) so to help you remember which direction to point your arrows try to remember this:

cout << "Hi"; // << its pointing out, used for output

cin >> aInt; // >> its pointing in, used for input.



Storing User Input in Variables:
To store user input to a variable you will need to first declare the variable and its data type, and then ask the user for input like shown below.
Listing 2.1
#include <iostream>

using namespace std;

int main()
{

// Declare the variables that you are going to be working with.
int a;
int b;

// Ask for the user's input.
cout << "Please enter a number: " << endl;
cin >> a;

cout << "Please enter another number: " << endl;
cin >> b;

cout << "The value you entered for a is: " << a << endl;
cout << "The value you entered for b is: " << b << endl;

system("Pause");
return 0;
}

Output:
The value you entered for a is: 5
The value you entered for b is: 3

Lesson Review:
What you learned in this lesson:
  • What a variable is.
  • The many types of variables available.
  • How to declare, initialize, and store a value in a variable.
  • What user input is.
  • How to extract input from a user and store it in a variable.


Practice Exercise:
Make a program that asks the user for their birthday and displays it back to them.
Sample output.
What is your birthday?
Enter the month: 8
Enter the day: 21
Enter the year: 1988
Your birthday is 8/21/1988.

Sunday, January 20, 2008

Intro To C++!

Tutorials created by Fuse

Promoted Sites:
www.NextGenBoards.com



Tools used for this tutorial-



Tutorial Introduction-

I will be writing a series of c++ tutorials over the next few months. I am going to teach the basics (console programming) to the more advanced (including winapi, and directx). I have been studying c++ on and off for about 4 years now, recently i have been reading allot of directx tutorials and I'm planning on completing my series of c++ tutorials with a complete directory of tutorials with everything you need to know to make a full direct3d game. I'm hoping to add some 3d modeling and game audio tutorials as well but that will come after the programming tutorials. Hopefully i can get this whole project done within 6 months, its definitely possible. I hope you enjoy and take in what i am trying to teach, it will take allot of dedication and practise to be able to program on your own two feet, but i will try to make learning the c++ language as easy as possible. Lets get started. If you have not already, download Visual Studio 2008 Express, i have provided a link above.

After downloading install the program to its default directory.

When installation is complete, open up Visual C++ 2008 Express. If you have any problems installing or downloading just post a comment and i will reply asap.


Making a new project in visual C++:

If you have been following along visual c++ should already be open, if not go ahead and open it. To make a new project go to File>New>Project. The new project window will open, we need to make a "Win32 Console Application" so make sure you click it. Before we can move on we need to name our project, for the sake of this tutorial we will call it helloworld.



Click OK, then click next. In the application settings under application type click the bubble for "Console Application". Then under the "Additional Options" check the "Empty project" box.



This will be the default settings that you will use to make a console application, go ahead and click finish.

We only have one more step before we are done setting up the project, we need to add a source file to our project. To do this Right click on the source files folder then click add>new item. The source files folder can be found under the solution explorer which is located on the left side of the screen. If you do not see it hold down Ctrl+Alt+L to open it up.




The "Add New Item" window will pop up, we need to add a new C++ Source File (.cpp file) so click the C++ Source File and then name your source file, you can name it whatever you want, i went ahead and named mine hellosource.



Click add, you will probably notice that a new tab has opened up named hellosource.cpp that contains a editor, this is where we will program our actual console application.




We are now ready to start programming!



Your First Program!:
At this point you are probably wondering what a console application is. Ill try to explain it as simple as i can, a console application is a computer program designed to be used via a text-only computer interface. Dos is a type of console application. Lets go ahead and get started on some programming! Im going to go ahead and type out the source code, and explain each part in detail afterwards.

//Include the iostream standard file
#include <iostream>

//Include the standard namespace library
using namespace std;

//Beginning of the main function
int main ()
{
//Prints Hello World! to the screen and
//starts a new line using endl;
cout << "Hello World!" << endl;

//Pauses the main loop, and resumes with user input
system("Pause");

//return the value of zero
return 0;
}
To compile and run this program copy and paste all of the above code into the visual c++ editor exactly as shown. Goto the debug menu and click "Start Debugging" or you can just press F5 on your keyboard. You should end up with something like this.

Now lets learn what each part of the code does.

Descriptions from cplusplus!

// This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.


#include <iostream>
Lines beginning with a pound sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

using namespace std; All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.


int main () This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.


Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.

cout << "Hello World" << endl; This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.

cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (which usually is the screen).

cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.


<< endl is used to end the line and start a new one, it is used mainly for formatting reasons.


Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).


return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

Lesson Review:

Congratulations you are now a c++ programmer, lets review what we just learned.

  • How to make a new visual c++ project
  • How to add a source code to the project
  • How to make a basic hello world console application
  • In depth explanations of each part of the hello world program


Practice Exercise:

C++ takes alot of practice so i will have a exercise at the end of each tutorial, i will try to make the exercises as interesting as possible.

Exercise: Mess around with the cout statement, try to make a program display a small paragraph. The most important thing is to get a feel for programming, stay tuned for my next tutorial, and if there any questions or suggestions then just leave me a comment.