User Input
We already know how to output the data with the object cout
and operator <<
. However, sometimes we need to get values directly from the user. To enable this operation use cin
in combination with the extraction operator >>
.
pythoncin >> x; // Get the value for the variable x
For example, here we want to get the value for the variable a and output it:
python9912345678910111213141516171801 #include <iostream>02 using namespace std;0304 int main() {05 // Declare the variable06 int a;0708 // Print the message for user to get the value09 cout << "Print a number: ";1011 // Get user input12 cin >> a;1314 // Display the input of the user15 cout << "Your number is: " << a;1617 return 0;18 }
In the line #12 the user print the value which will be stored in the variable a
.
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 2. Capítulo 3
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo