In C++, we use the cin
and cout
objects for standard input and output. These objects belong to the iostream
library. The cout
object, along with the <<
operator, is used to output data, like print in Python or JavaScript's console.log, while cin
, together with >>
, is used to take input from the user, akin to JavaScript's prompt or Python's input.
In the given C++ code, we first include iostream
to be able to use cin
and cout
. Then we define variables userName
and userAge
to store the user's input. We ask for the user's name and age using cout
, and take the user's input using cin
. At the end, we greet the user and tell them their age using cout
.
Comparable to string interpolation in your JavaScript or Python code, the <<
operator allows us to include variable values in strings output by cout
.
xxxxxxxxxx
using namespace std;
int main() {
string userName;
int userAge;
cout << "Please enter your name: ";
cin >> userName;
cout << "Enter your age: ";
cin >> userAge;
cout << "Hello, " << userName << ". You are " << userAge << " years old." << endl;
return 0;
}