User input in C++ can be given by using the cin keyword along with the extraction operator ">>". In the following piece of code, we will declare the variable "x" and then let the user give the value of "x".
#include <iostream> using namespace std; int main() { cout << "User input in C++.\n"; char y; cout << "Enter a character:"; cin >> y; cout << "Your character is:" << y; return 0; }
By adding user input in your code, you can create a simple calculator. An example of a calculator is given below-
#include <iostream> using namespace std; int main() { cout << "Simple calculator in C++\n"; cout << "Enter the first number:"; double x; cin >> x; cout << "Enter the second number:"; double y; cin >> y; cout << "The sum of the numbers is:" << x+y; cout << "\nx-y=" << x-y; cout << "\ny-x=" << y-x; cout << "\nThe product of the numbers is:" << x*y; cout << "\nx/y=" << x/y; cout << "\ny/x=" << y/x; return 0; }
That is all for this post on user input in C++. Hope you found it easy to understand. Please follow this blog to learn coding languages like Python, C++, etc.
Until next time,
Aarav Iyer
References: