C++/Variables and User Input

< C++

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. 32-bit is the most common with 64-bit becoming more popular due to the advent of 64-bit CPU architectures and operating systems. The address for an integer called myInteger is defined when your program is compiled and it typically takes up 4 bytes or 32 bits.

You can think of them as boxes that you store data in. Certain boxes can store certain items. You can only store integers in short, int, or longs and store reals (numbers with decimal places) in float or double. Numbers from one type can go to another, but you are converting the numbers into another type. Non-integer variables can be char for characters, or bool for true/false values.

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. If you were to try and place the number 4.3 into an integer the number will be truncated to 4.

// Short is normally defined as a 16-bit integer.
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 is guaranteed to be 16-bit, but modern implementations use 32-bit for an int.
int myVariableName;  // stores from -32768 to +32767
signed int myVariableName;  // stores from -32768 to +32767
unsigned int myVariableName;  // stores from 0 to +65535
 
// Long is a 32-bit number.
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

Now we can attribute reasons to the ranges of int , long and so on. for int : 2^16 = 65536. now this is the total range of a variable . Dividing by 2 we get , 32768. So -32768 to 32767 ( it should have been 32768 but 1 place is taken by 0 ).


Now the size of long is 4 bytes ( 32 bits) . So range is 2^32.

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, but it will store only the ASCII value.

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).

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;
}

However, not using "using namespace std;" is considered good programming practice.

Where To Go Next

Topics in C++
Beginners Data Structures Advanced
Part of the School of Computer Science
This article is issued from Wikiversity - version of the Thursday, February 12, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.