codescracker
c++

C++ Program to Get Input from User



« Previous Program Next Program »

In this article, you will learn and get code to get or receive input from user in C++ programming. Here are the list of programs on getting input from user:

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.

Get an Integer Input from 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:

// C++ Program to Get an Integer Input from User
// ----codescracker.com----

#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 build and run under Code::Blocks IDE. Here is its sample run:

receiving input in C++ Programming

Now supply an integer input say 25 and press ENTER key store it in 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 User

This program gets character input from user and prints the entered character by user at run-time, back on the output screen:

// C++ Program to Get a Character Input from User
// ----codescracker.com----

#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 is its sample run with user input, c as character:

get character input from user c++

Get String Input from User

This program gets string input from user using cin. Later on, we've also created some programs to receive string input using functions.

// C++ Program to Get String Input from User
// ----codescracker.com----

#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 string:

get string input from user c++

Here is another sample run with user input, this is codescracker.com as 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 receive string input from user along with all the spaces. That is, this program doesn't skip any word from the entered string, without mattering about occurrence of spaces.

// C++ Get String Input with Spaces using gets()
// ----codescracker.com----

#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;
}

Here is its sample run, with user input this is codescracker.com:

get string input with spaces c++

Note - The function, gets() is defined in stdio.h header file

Get String Input with Spaces using getline()

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

// C++ Get String Input with Spaces using getline()
// ----codescracker.com----

#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

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

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker