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 >>
.
cin >> x; // Get the value for the variable x
For example, here we want to get the value for the variable a and output it:
9912345678910111213141516171801 #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
.
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 2. Luku 3
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme