In order for the user to be able to set the value of the variable himself, it is necessary to be able to input values from the keyboard.
Handling the standard input in C++ is done by applying the operator cin >> <variable>
.
cin >> a;
After this operator, the data that is input from the keyboard to store it in a
a
-variable.
It is possible to combine several
cin
statements into one
For example:
cin>>a;
cin>>b;
performs the same actions as the record
cin>>a>>b;
that is, the first data entered is entered into the variable a, the second into the variable b
*** Advanced Material: language programing C ***
For input data from the keyboard an input operator is used, which in general has the following structure
scanf ( "<input formats>", <variable's address>);
Input formats – this is a quoted string listing one or more data input formats.
For example, the most commonly used
%d input an integer variable (variable of type nt)
%f input a real variable (variable of type float)
%с input a one symbol (variable of type char)
For example, scanf ( "%d%d", &a, &b);
This operator, waits for an input from the keyboard the values of two integer variables. The first number input from the keyboard will store to cell
a, the second to cell
b.
After the input format, the addresses of the memory cells to which the inputed values are written are listed with a comma. Variable values are inputed by specifying the address of this variable. Therefore, it is necessary to put an ampersand sign in front of the variable name: &a
is the address of the variable a
The number of input formats and and the number of variables must match!