This article focuses on understanding variables in the language by writing 3 different programs. Essentially, variables help store information or, simply put, remember things in your computer’s memory.
The first program that we are going to write is named ‘Echo.’ Much like the echo you hear when you yell at a mountain, this program has to ‘echo’ or print back whatever it receives as input.
To ask for INput, we use ‘cin’ in :
cin >>As can be seen, the ‘>>’ symbols are used with ‘cin’ as well, much like what has been discussed in the previous article in this series; they represent the flow of data. In our example, they signify the flow from the user input to the variable where we want to store the value.
Well! What is a variable?
Think of it like being asked to repeat after someone. What you have to do is listen to that person, remember everything in your short-term memory, and then repeat it. Our computers work somewhat similarly. They have a memory that is wiped every time the computer reboots, called RAM (Random Access Memory), which is used to store any piece of information that is useful during calculations or an application’s runtime. We define a name for a chunk of this memory and store data in those labeled chunks of storage.
Variables have different types based on their size, which defines the largest value they can hold, and the type of value they store. These types include:
| Type | Usage |
|---|---|
| bool | Used for logical states (true or false) |
| char | Used for individual characters |
| short | Used for small integers (numbers without decimal points) |
| int | Used for integers |
| long | Used for longer integers |
| float | Used for floating-point numbers |
| double | Used for larger floating-point numbers (has exactly double the size of float) |
Which are known as primitive data types. There are some other types that other programmers define, like string, which is a part of the standard library and comes in handy for handling strings of characters, be it a single character or a word or even a whole essay.
Variables need to be first declared so as to allow the computer to decide where to place them. Here’s the standard format for declaring variables in the  language:
data_type variable_name;For example, let’s declare a variable of type int named “age”:
int age;Also, “initial values” can be assigned to variables at the time of declaration. In the line of code below, we define a variable called “age” of type int and initialize it with the value 19
int age = 19;Variables can have their values modified and updated by using the ‘=’ sign, as shown below:
age = 20;Here’s an example: the code below declares a variable named a and sets its initial value to 10, prints it, updates its value to 20, and prints it again. So, the output of the application will be 10 on the first line and 20 on the second.
int a = 10;
cout << a << endl;
a = 20;
cout << a << endl;The first step our program takes is to prompt the user for input. To do this, we declare a variable of type string named “input”. While the name of the variable doesn’t impact how the code functions or its performance, it’s a common practice to choose names that reflect their purpose. This practice helps other programmers better understand the code.
string input;Next, we utilize cin to prompt the user for input, which is then stored in the “input” variable.
cin >> input;Lastly, our program simply prints back the text it received as input.
cout << input << endl;endl is added to ensure proper formatting, addressing the issue with shell prompts described in the previous article.
Combining the three lines that we just wrote with the base structure of a program, our final (runnable) code would look something like this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cin >> input;
cout << input << endl;
}The text inserted here will be echoed
The text inserted here will be echoedWe also include string to enable our program to utilize the string type.
The second program we’re writing in this article is called the “Hello” program. Its purpose is to ask the user for their name and greet them accordingly.
Radin
Hello, RadinMaking slight modifications to the code we wrote in the previous step, we can achieve the desired goal.
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cin >> input;
cout << input << endl;
}First, let’s just rename the “input” variable to “name,” as it’s now intended to contain the user’s name. (It’s worth emphasizing once more that the variable names will in no way affect your program’s performance and output -at least in the language- but it’s a good practice to name them according to their usage.)
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cin >> name;
cout << name << endl;
}Next, we add “Hello” before the “name” variable in the cout statement.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cin >> name;
cout << "Hello" << name << endl;
}Radin
HelloRadinSo, currently, you’ll see that the text “Hello” and the name’s content are mashed together without any space in between. But that’s an easy fix—just add a space after the "Hello" message. Adding a comma could also make the greeting sound more natural.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cin >> name;
cout << "Hello, " << name << endl;
}Radin
Hello, RadinThe final example we’re covering in this article is a program that takes two numbers as input and calculates their sum.
First, let’s declare two variables of type int called “a” and “b” to represent the two numbers. Note that we can declare these two variables in one line with a comma between them as they share the same type. Then, we’ll use cin to prompt the user for these two numbers. As shown below, cin can accept two different variables in one line just like cout.
int a, b;
cin >> a >> b;Then, the program prints the two numbers added together. All four basic math operations (+, -, *, and /) are available in the language.
cout << a + b << endl;If we combine all the lines of code we’ve written so far, we’ll get the code below, which works as intended,
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b << endl;
}1
2
3but has a slight problem: it lacks interaction with the user. When this code is executed, it simply waits for the user to type in the numbers, providing no instruction.
To address this issue, we can use cout to print prompts, instructing the user on what our program expects.
cout << "Enter the first number ";
cin >> a;We’re not using endl here. Instead, we can simply add a single space after our prompt, allowing the user to type their input directly next to it without having it joined to the prompt.
Here’s the modified and final code with prompts for the user:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter the first number ";
cin >> a;
cout << "Enter the second number ";
cin >> b;
cout << "The sum of these two numbers is: " << a + b << endl;
}Enter the first number 2
Enter the second number 3
The sum of these two numbers is: 5In this article, we explored the use of variables in the language by writing three different programs. In the next article, we’ll delve into if statements in the
language. Hope to see you there soon!
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cin >> input;
cout << input << endl;
}The text inserted here will be echoed
The text inserted here will be echoed#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cin >> name;
cout << "Hello, " << name << endl;
}Radin
Hello, Radin#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter the first number ";
cin >> a;
cout << "Enter the second number ";
cin >> b;
cout << "The sum of these two numbers is: " << a + b << endl;
}Enter the first number 2
Enter the second number 3
The sum of these two numbers is: 5