Input and Output

Lesson by Rogério Júnior

Now that you have written a program that prints text on the screen, let's make one that reads what is written on it

Before we make the computer save something that is written on the screen, we have to warn it so that it reserves a space in its memory to store that information. That is called declaring a variable. There are several types of variables, like int, double, char, float, amongst others. Every kind of variable stores a different kind of data.

Let's make a program in which we write an integer number on the screen and it prints that number added by 10. The first thing we'll do inside the main function will be declaring an integer variable. In order to do that, we write a line that first contains the type of the variable, in this case, int, and afterwards we write its name, that we will call num, followed by ; to indicate the end of the command. The line will then be int num;. After processing this command, the computer will reserve in its RAM memory enough space to store an integer value.

To insert a value in variable num, we will use the function cin, which is in iostream. This is the function we use to read information as input. We write the name cin followed by two greater signs (>) and we tell the computer in which variable it should store the information. After reading everything, we type ";". In order to read a number and save it in variable num, we write: cin >> num;. If we wanted to read two numbers and store them in two integers x and y, respectively, we would write the command cin >> x >> y;, indicating to the computer the variables in which the information read (in order) should be stored.

Now that the program stored the number, we will make it print what we want. If the number written is stored in variable num, we will then print num+10. We will do this is a similar way as we did before, but with the function responsible for printing, typing the command cout<<num+10;. Let's go to the code:

#include <iostream> // cin and cout

using namespace std;
int main(){
  int num; // declare the integer variable "num"
  cin>>num; // read what's written and save it to variable "num"
  cout<<num+10<<"\n"; // print the value of "num" added by 10
  return 0;

To see the code working, compile and execute the program (pressing F9 on CodeBlocks) and type any integer number you want. When you press enter, the number you typed will be stored in variable num, and after this process the program will print the number you typed added by 10 in your computer's screen.

Let's work now with another kind of variable, the double. This type is used to store rational numbers with several decimal places of precision. Problems that require variables of the type double usually request a certain precision when printing the answer. To limit the number of decimal places that will be presented in the cout commands, we use the command cout.preceision(precision you want) followed by the command cout.setf(ios::fixed).

With that in mind, let's make a program that, given the radius of a circle (not necessarily an integer), writes its area with precision of 2 decimal places. The program must print the phrase "A area eh X" (which stands for "The area is X" in Portuguese,) followed by an end of line, where X is the value of the area.

The first thing we will do is, again, declare a variable. However, it will now be a double and we will call it raio (radius, in Portuguese.) We thus write the line double raio;. Afterwards, we read the value of the radius and store it in this variable using the function cin by typing cin >> raio;.

Let's declare a variabel to approximate the value of \(\pi\) e call it pi. We write the code double pi;. For the computer, that it is just any variable like all others and does not get the value of  \(\pi\) just for having its name. We need to give it the value we want. In this case, a good approximation would be 3.14159, since we only want two decimal places in the answer. To give a value to a variable, we use the operator "receive," which is '='. If, in the code, we type a=b;, we are giving the variable a the value of b (and you read that as "a receives b"). In this case, we must type the command pi=3.14159;. After that, we mus then print the quote "A area eh " (which stands for "The area is " in Portuguese) followed by the value of raio*raio*pi (the area of the disk), with precision of two decimal places. We do that with the command cout<<raio*raio*pi<<"\n";. Let's check the code:

#include <iostream> // cin and cout

using namespace std;

int main(){
  double raio; // declare the variable raio, which stands for the radius
  cin>>raio; // read the value and store it in raio
  double pi; // declare the variable pi
  pi=3.14159; // make pi be 3.14159
  cout.precision(2); // choose the precision I want in the output

  cout<<"A area eh "<<raio*raio*pi<<"\n"; // print the quote I need with two decimal places of precision
  return 0;

Limits of the variables

Maybe you are wondering what is the biggest number you can store in a variable.

According to C++ reference, we can store in an int variable all the values from \(-2^{31}+1\) to \(2^{31}-1\), and in bigger values, you should use variable of the type long long, which can store values from \(-2^{63}+1\) to \(2^{63}-1\).

But remember that we shouldn't always use long long variables, because they need twice the space in memory to store their value when compared to int. Therefore, you should try using int variables and, if you realize you will exceed its limits, switch to long long.

In the case of real variables, our recommendation is that you always use double variables becase it can store numbers with much higher precision than float. Thus, using double leaves you less exposed to rounding errors.

Now that you've learned that, let's solve some problems.