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.