C++ Program to Get Input from the User

In this article, you will learn and get code to get or receive input from the user in C++ programming. Here is a list of programs for gathering user input:

To receive or get input from the user, use cin>>input. Here, input is the variable that stores the value of given number, character, or string.

The cin>> is used to receive the input data like integer, character, float, etc.

In C++, get an integer input from the user

This C++ program asks from user to enter an integer value or number to receive it and store in a variable say val. Then further displays the entered number on the screen:

#include<iostream>
using namespace std;
int main()
{
    int val;
    cout<<"Enter the Number: ";
    cin>>val;
    cout<<"\nThe Value is "<<val;
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

receiving input in C++ Programming

Now supply an integer input, say 25, and press the ENTER key; store it in the val variable; and print it back on the output as shown in the snapshot given below:

get integer input from user c++

Get character input from the user in C++

This program accepts user input and prints the character entered by the user on the output screen at run-time.

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cout<<"Enter the Character: ";
    cin>>ch;
    cout<<"\nYou've entered: "<<ch;
    cout<<endl;
    return 0;
}

Here's an example run with user input, using c as a character:

get character input from user c++

Get String Input from User in C++

This program gets string input from the user using cin. Later on, we wrote some programs that used functions to receive string input.

#include<iostream>
using namespace std;
int main()
{
    char str[200];
    cout<<"Enter the String: ";
    cin>>str;
    cout<<"\nYou've entered: "<<str;
    cout<<endl;
    return 0;
}

Here is its sample run with user input, codescracker as a string:

get string input from user c++

Here is another sample run with user input; this is codescracker.com as a string:

c++ get input from user

As you can see from this sample run, rest of the string, after this gets skipped. That is, the string before space, only gets received here.

How to Get String Input with Spaces?

Important: To get string input along with spaces, use gets() or getline() function.

To get string input using gets(), use it in this way:

gets(str);

whereas to get string input using getline(), use it in following way:

getline(cin, str);

Get String input with Spaces using gets()

This program uses gets() to get a string and all the spaces from the user. That is, this program doesn't skip any word from the entered string, regardless of the occurrence of spaces.

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[200];
    cout<<"Enter the String: ";
    gets(str);
    cout<<"\nYou've entered: "<<str;
    cout<<endl;
    return 0;
}

This is codescracker.com's sample run with user input, this is codescracker.com:

get string input with spaces c++

Note: The function gets() is defined in the stdio.h header file.

Using getline(), get String Input with Spaces

And here is another program that does the same job as the previous program. That is, this program also receives string input with spaces, using the getline() function:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    cout<<"Enter the String: ";
    getline(cin, str);
    cout<<"\nYou've entered: "<<str;
    cout<<endl;
    return 0;
}

Here is its sample run, with user input, codes cracker dot com:

c++ get string input from user

Please keep in mind that the function getline() is defined in the string header file. To declare a variable that stores or receives string input using this function, you have to use the string data type of the string header file.

The same program in different languages

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!