C++ 3. Variables and input

In the last tutorial, C++ 2. Escape Sequences, you learned about Escape Sequences. All we have done so far is printing stuff to the screen. I think it's time for us to move on and let us interact with the console. Before we do, let's talk about variables.

 

Variables

Understanding variables is essential to be able to program. Remember all those times when you asked yourself, your parents, friends, teachers, siblings, pets and imaginary friends the question "When will I ever use math"? Well, I guess you can say that when you're creating a program is one of those times you will use math.

In math, a variable is a symbol for a number we don't know yet. It's the same in programming, but we call the symbol identifier.

#include <iostream>

int main() {
	int a = 2;
	int b = 5;
	int y = a + b;
	std::cout << y << std::endl;
}

When this program is executed it will output 7 on the screen. The first three lines inside the main function are declarations. Variables are declared with a name and a data type. All of our variables in this example are data of type int, these will only hold integers. The names are ab and y. They are referred to identifiersa and b are assigned values, 2 and 5. In this line:

	int y = a + b;

We are really saying:

	int y = 2 + 5;

 

The variables doesn't have to be integers.

#include <iostream>
#include <string>

int main() {
	std::string animal = "frog";
	std::cout << "My spiritual animal is " << animal << std::endl;
}

The output from this code will be "My spiritual animal is frog". The data type used here is string, which holds a sequence of characters.

 

Talking to the computer

We will now use the input stream object std::cin and the stream extraction operator >> to obtain a string typed at the keyboard. The program below will wait for the user to input an adjective and hit enter, and the output will depend on it.

#include <iostream>
#include <string>

int main() {
	std::string adjective;
	std::cout << "Input an adjective: ";
	std::cin >> adjective;
	std::cout << "Frogs are " << adjective << std::endl;
}

Output:

Input a value: awesomeFrogs are awesome

 

	std::string adjective;

string is declared with the identifier adjective. Notice that we don't assign this variable a value, we just declare it so we can use it later.

 

	std::cout << "Input an adjective: ";

Outputs "Input an adjective: " on the screen. This line can be pronounced "std::cout gets the string "Input an adjective: "."

 

	std::cin >> adjective;

Here we use the standard input stream object cin and the stream extraction operator >>. When these are used together this way, std::cin takes character input from the standard input stream, normally the keyboard. This line can be pronounced as "std::cin gives a value to adjective."

The computer will wait for the user to input a value when this line is executed. When the user hits the Enter key, the computer will take that value and give it to the waiting variable.

 

	std::cout << "Frogs are " << adjective << std::endl;

This outputs "Frogs are <adjective>" on the screen, where <adjective> is substituted with whatever the user wish it to be.

In the next article we will create a Pythagorean Theorem Calculator. Click here to read C++ 4. Creating a Pythagorean Theorem Calculator.

Enjoyed this article? Give the teacher an apple.

cookie

0

Author

authors profile photo

Articles with similar tags

Comments