Variables and User Input
From Wikiversity
Contents |
[edit] 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.
[edit] Declaring variables
Declaring variables is easy just <variable_type> <variable_name>.
Let's say you want to declare an integer.
int myInt;
[edit] Assigning values
What are variables without values? In order use a variable you just need to declare 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;
[edit] Types of Variables
[edit] 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;
[edit] 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
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.
[edit] 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.
[edit] 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
[edit] 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
[edit] Lesson 2 Program
[edit] Getting User Input
Let's start simple. We'll read in a char and put it back out.
#include <iostream>
using namespace std;
int main()
{
char myChar;
std::cout << "Enter a characer. ENTER: ";
std::cin >> myChar;
std::cout << "You entered: " << myChar << 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;
}
[edit] Where To Go Next
| Topics in C++ | ||
| Beginners | Data Structures | Advanced |
|---|---|---|
|
||
| Part of the School of Computer Science | ||