Activity 2 - Variables and Inputs
Awesome! Now let’s go a step further. We will now not only print the message, but input the name of the receiver and make our program say “Hello”.
To do that we will need variables.
Variables
A variable is a container that stores information. There are many kinds of variables, each storing different types of information:
- string: represents a sequence of characters, letters, numbers, and symbols. strings are always wrapped in double quotes. Examples:
"hello"
,"1,2,3"
. - int: represents positive or negative integer numbers. Examples:
1
,100
,-250
. - char: represents a single character. Examples:
'A'
,'e'
,'I'
. chars are always wrapped between single quotes. - bool: represents either a
true
orfalse
value.
To create a variable, declare it as follows:
int number = 10;
Let’s look at this line of code in more detail:
We first need to specify the type of information that will be stored inside that variable. In this case, it will be of the int
type. Then we need to declare the name of the container; in this case, we will name our variable number
. Finally we specify the value that the container will hold; in this case 10
. We end with a semicolon to indicate that the line is complete.
We can now use the variable number
inside the program to reference the stored number. If we would like to print its value, we would use the following line of code:
cout << "The number is: " << number << endl;
Running this line of code, the console will print:
The number is: 10
We can use cout
to print words, numbers or variables. If we would to print more that one thing, we would have to add <<
between each term:
Example:
int hour = 13;
cout << "It is " << hour << " in the afternoon" << endl;
This will print:
It is 13 in the afternoon.
Next, we’d like the recipient of the message to be able to write his or her name to the console, (which we will be storing inside a variable), and the program to say “Hello” to him or her.
To input the name we will be using the following code:
string name;
cin >> name;
Let’s dig into this line of code.
Console Input
Similar to how we can print to console using cout
, we can input data into the console by using cin
, and specifying the name of the variable in which the input will be stored.
In the previous example we are stating that whatever data we input, it will be stored inside the variable name
.
That being said, if we input:
Queen
Then the variable name
will store the string "Queen"
.
Now it’s time to begin with our program.
These are the steps to follow for this activity:
- Declare a string variable named
name
. - Use
cin
to request input from the user, and store the inputted data into thename
variable. - Print to console
"Hello (name)"
, where(name)
should be replaced by the value of thename
variable. - Click Run. Type in
"Queen"
as the name of the recipient in the console tab, and make sure you get"Hello Queen"
as output from the console.
HELP!
If after clicking the Run button, an error message in red appears, it means that there is something wrong with your code. Please ask for help from your instructor.
You should also check for the following
- Ensure the spelling, capitalization, and symbols used match with the example given.
- Make sure that you add a semicolon at the end of each line of code.
- Remember that all strings must be between quotation marks.
- All the code that you write need to be within the brackets
{}
in theint main
function.