• Register

Every newbie knows how to print the data with cout. Basically printing data is the first step we learn in the Hello world program. In this article, I’ll discuss user input.


Operators and Syntax

Header file <iostream> contains two basic operators, cin and cout. Cout is used to print output and cin is used to get the user input. Special input operator is used with the keyword cin to get the input from the user. The whole syntax will be like this : cin >> name;

Here variable name is just an example. You can replace it any variable you want to create.

Before getting input from the user, we have to define the variable in which we want to store the data. Otherwise the compiler will show an error like this: name was not declared in this scope. It shows the error, since the compiler needs the declared variable before putting the data in the variable because memory should be allocated before getting the input.



int x; 
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value

Hope this helps.

100 points
7 2