In this article, we will learn how to get user input in C++ in programming from users.
By the end of this article, you will be able to answer the below questions:
Different ways to get user input in C++
- How to Get an Integer Number Input from User.
- How to Get an Floating/Decimal Number Input from User.
- How to Get a Character Input from User.
- How to Get a String Input from User.
- How to Get Multi word String Input from User.
1. How to Get an Integer Number Input from User
In this example, we are taking an integer as input from the user. Users can enter an integer of their choice and print the entered integer value.
C++ Program Example
#include<iostream>
using namespace std;
int main()
{
int intval;
cout << "Enter the Integer Number: ";
cin >> intval;
cout << "\nThe Integer Value is " << intval;
cout << endl;
return 0;
}
Output
Enter the Integer Number: 12
The Integer Value is 12
2. How to Get an Floating / Decimal Number Input from User
In this example, we are taking a decimal number as input from a user. Users can enter any decimal number of their choice and print the entered number as a result output.
C++ Program Example
#include<iostream>
using namespace std;
int main()
{
float fltval;
cout << "Enter the Decimal Number: ";
cin >> fltval;
cout << "\nThe Decimal Value is " << fltval;
cout << endl;
return 0;
}
Output
Enter the Decimal Number: 23.33
The Decimal Value is 23.33
3.How to Get a Character Input from User
In this example, we are taking a character input from the user and printing the entered number on the screen.
C++ Program Example
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter the Character from Keyboard: ";
cin >> ch;
cout << "\n You Pressed Character Key " << ch;
cout << endl;
return 0;
}
Output
Enter the Character from Keyboard: D
You Pressed Character Key D
4. How to Get a String Input from User
Sometimes we want to take a string from the input as we have defined a character array of 200 as name length and printing the result entered by the user.
C++ Program Example
#include<iostream>
using namespace std;
int main()
{
char name[200];
cout << " What is your Name: ";
cin >> name;
cout << "\n Nice to Meet you! " << name;
cout << endl;
return 0;
}
Output
What is your Name: Devenum.com
Nice to Meet you! Devenum.com
This can be done this way also, another way to do this define a string name as asking the user to input the name.
#include<iostream>
using namespace std;
int main()
{
string name;
cout << " What is your Name: ";
cin >> name;
cout << "\n Nice to Meet you! " << name;
cout << endl;
return 0;
}
Output
What is your Name: Devenum.com
Nice to Meet you! Devenum.com
5. How to Get Multi word String Input from User
To get the multi-word string in c++ we have to use getline() method as we are doing in the below example.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string address;
cout << " What is your Address? ";
getline(cin, address);
cout << "\n Ohh! You live at: " << address;
cout << endl;
return 0;
}
Output
What is your Address? House Flat no 203 ,City CA,USA
Ohh! You live at: Flat no 203 ,City CA,USA
This can also be done with the below code.
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char address[200];
cout << " What is your Address? ";
gets_s(address);
cout << "\n Ohh! You live at: " << address;
cout << endl;
return 0;
}
Output
What is your Address? House Flat no 203 ,City CA,USA
Ohh! You live at: Flat no 203 ,City CA,USA