C++ Lesson 2 - Using Inputs and Types

Page 1 - Data Types

Skip to navigation

Dec
2

In this second lesson you will learn how to capture input, and what the different data types available are. Those who have used other languages before should be reasonable familiar with the datatypes that C++ offers.

In the last lesson I talked about the main method and its importance in making programs. Also I explained what methods were and how to output to the screen. I hope this doesn't add to any confusion but there also exists another type of main method called winMain - but I won't be explaining it until later, it's enough at the minute to just know that it exists.

Types are something that you will need to get used to very early on in programming in any language and not just in C++, you've already used them to some degree by completing lesson 1. Now it's time to take a better look at them in the context of C++.

The types you will be using the most in these tutorials are: int, string, char, bool, float, and double. Others exist such as short double, long double, long int, short int, and unsigned int - but I won't be talking about these in the immediate lesson either.

Here is a quick explanation on the most common 6 types:

int
Integers
Integers store positive and negative whole numbers only. Such as 10, 2, -2, 0, etc. Declaration of int variable: int x = 0; or int someint;
char
Character
This can hold a single letter, number, or symbol. Declaration of char variable: char c = 'a'; or char somechar;
string
String
A string is basically a variable containing some text - i.e. it is a group of characters. In order to use string you must include the string library at the start of the code. i.e. you should add #include<string> to any programs that use strings. Declaration of string variable: std::string aString = "some sample text"; or std::string s;
bool
Boolean
These are basically truth values - can only have the value true or false, anything else is invalid. These are typically used in conjunction with if statements (covered in the next lesson). Declaration of bool variable: bool isFound = true; or bool isFound;
float and double
Floating Point Integer
These are very similar types and use integers with decimal values such as 0.1. Declaration of float variable or double type: float aNumber = 0.0; or double anotherNumber;

Another type is what is known as an enumerated type. These can be useful in simplifying program structure - but they are not absolutely necessary.

enum colour {red, green, purple};

int main() {
    colour c;
    c = red;
    c = yellow;
    c = 3;
    c = (colour) 3;
    return 0;
}

Example 4 demonstrates how these types might be used. Line 1 demonstrates how a typical enumerator type would be set up. In this example (Line 1) a type called colour is created, and we have said that it can only have the values of red, green, and purple. Technically each of the values are integers 0,1,2 - though we have given them names in order to make the program easier to read - this is suggested for any programs that use enumerated types.

Line 4 of example 4 creates a variable called c, which is of type colour (the type we have just created). Line 5 of this example will set the value of c to be red - this is allowed as it is a value of colour.

On the next line (Example 4: Line 6) an error will occur. This is because c cannot have the value yellow as it does not exist in the colour type.

The next line (Example 4: Line 7) will also cause an error as 3 is an integer, not a colour from the colour type.

On the other hand as shown on Line 8 of Example 4 it is possible to use integers to select which colour c is. It is not recommended - but you can do it. It uses a method known as "casting". Casting is basically where a value of one type is converted to a value of another type. Note that not all types can be cast between. Such as a bool cannot be cast into a string. To cast a value put the type in brackets before the value in the other type (Example 4: Line 8).

That is enough of types for now, but rest assured we will be looking back at them in a later tutorial to explain expanding them into arrays, and templates, etc. In the next lesson we look at a structure which can also be considered as a type - these being called classes.