You are on page 1of 5

# Variables and User Input

1
Variables and User Input
Variables - What are they
Variables are simply addresses with a set amount of data that can be stored at each address. The amount of data that
can be stored differs between CPU architectures. The standard is 32-bit today. The address for an integer called
myInteger is defined when your program is compiled and it takes up 4 bytes or 32 bits.
Declaring variables
Declaring variables is easy just <variable_type> <variable_name>.
Let's say you want to declare an integer.
int myInt;
Assigning values
What are variables without values? In order to use a variable you just need to declare it and give it a value.
myInt = 0;
Can't I just declare myInt and assign it a value in one go? The answer is Yes! we can as in the following way :
int myInt = 0;
Types of Variables
Boolean (bool)
The bool type is a 1 byte data type that is either true or false. A true being any number other than zero and false
being zero. The true keyword uses the value 1 to assign true.
bool canJump = false;
Integers
An integer is a number that does not have any decimal places. It is a whole number, for example 1,2,3,4 are all
integers. 4.3 is not, as it is a real number. If you were to try and place the number 4.3 into an integer the number will
be truncated to 4.
short myVariableName; // stores from -32768 to +32767
short int myVariableName; // stores from -32768 to +32767
signed short myVariableName; // stores from -32768 to +32767
signed short int myVariableName; // stores from -32768 to +32767
unsigned short myVariableName; // stores from 0 to +65535
unsigned short int myVariableName; // stores from 0 to +65535
int myVariableName; // stores from -32768 to +32767
signed int myVariableName; // stores from -32768 to +32767
unsigned int myVariableName; // stores from 0 to +65535
long myVariableName; // stores from -2147483648 to +2147483647
long int myVariableName; // stores from -2147483648 to +2147483647
Variables and User Input
2
signed long myVariableName; // stores from -2147483648 to +2147483647
signed long int myVariableName; // stores from -2147483648 to
+2147483647
unsigned long myVariableName; // stores from 0 to +4294967295
unsigned long int myVariableName; // stores from 0 to +4294967295
What is the difference between a "long" and a "signed long int"? In my mind, the only difference is 12 extra
keystrokes. Pick one that works for you.
Char
A char is an 8 bit integer. This means that an unsigned char can store between 0 and 255, and a signed char can store
between -128 and 127. Unsigned chars are commonly used to store text in ASCII format, and can be used to store
strings of information. A char can be initialized to hold either a number or a character.
char myChar='A';
char myOtherChar=65;
Both characters that I have just initialized would be equal. The number 65 is the ASCII code for the letter 'A', so both
characters would contain the 8-bit value of 65, or the letter 'A'. ASCII is a system where numerical value is assigned
to every character you can think of. For a complete conversion chart visit http:/ / ascii-code. com/
Floats
Floats are floating point numbers, which means that these numbers can hold decimal places. This allows us to store
numbers such as "8.344" and "3432432653.24123".
float myFloat; // Creates a floating point variable
myFloat = 8.3; // Stores 8.3 in the new variable
Floating point numbers have a fixed size in memory. This means that a single float cannot possibly precisely store
all of the decimal values in the real number system. While in many cases it will not matter, it is important to note the
float data type usually stores only a good approximation of a decimal value, not an exact value.
Doubles
Doubles are like "floats", which means they can store decimal places. Doubles can generally store more information
than a standard float.
double myDouble; // Created myDouble
myDouble = 8.78; // Stores 8.78 in myDouble
As noted with the float data type, double usually stores only an approximation of exact decimal values (albeit,
usually a higher precision approximation than the smaller float data type).
Variables and User Input
3
Lesson 2 Program
Getting User Input
Let's start simple. We'll read in a char and put it back out.
#include <iostream>
int main()
{
char myChar;
std::cout << "Enter a character. ENTER: ";
std::cin >> myChar;
std::cout << "You entered: " << myChar << std::endl;
std::cin.clear(); //ignore any superfluous input
std::cin.sync(); //synchronize with the console
std::cin.get(); //wait for the user to exit the program
return 0;
}
you could also go with a namespace, as shown here:
#include <iostream>
using namespace std;
int main()
{
char myChar;
cout << "Enter a characer. ENTER: ";
cin >> myChar;
cout << "You entered: " << myChar << endl;
cin.clear();
cin.sync();
cin.get();
return 0;
}
Where To Go Next
Variables and User Input
4
Topics in C++
Lesson 1: Introduction to C++ Lesson 8: Pointers Lesson 12: The STL
Lesson 2: Variables and User Input Lesson 9: Classes and Inheritance Lesson 13: STL Algorithms
Lesson 3: Simple Math Lesson 10: Templates 1
Lesson 4: Conditional Statements Lesson 11: Templates 2
Lesson 5: Loops
Lesson 6: Functions and Recursion
Lesson 7: More Functions
Part of the School of Computer Science
Article Sources and Contributors
5
Article Sources and Contributors
Variables and User Input Source: http://en.wikiversity.org/w/index.php?oldid=846324 Contributors: Alex loves you, Autumnfields, Bensaved, Cbradleyo, Chyld989, Cybiko123, DM2088,
Dominis, Gbaor, Giawa, Ivanlul, Miklcct, Niks2k6, Pnguyen, Remi0o, Shatner, Solpeth418, Steaxauce, 12 anonymous edits