C++ Program to Get Input from the User


While writing a program in any language, taking input is a fundamental job that we do in almost all programs. Sometimes we take input directly from the console or take the inputs from the files. Taking inputs from the files is somewhat beneficial because it does not require us to type inputs again and again, or sometimes we can save some good input test cases into a file. However, in this article, we are going to focus on console-based inputs. We will learn different techniques to get inputs from the user in C++.

There are a few different approaches to taking inputs from the console. Few of them are Clike methods and few are using input streams that are present in C++. We will go through them one by one with a few examples for better understanding.

Taking input using scanf() function

In C language, we use the scanf() function to scan inputs from the console with a formatting string. This function is also available in C++ so to take inputs in a formatted fashion, the scanf() method is good to go.

Syntax

The basic syntax for the scanf() method with the format string.

scanf ( “<format string>”, <address of variable> );

Format specifier for scanf() formatting.

Format Specifier Description
%c For a single character input
%s For a string with no space
%hi Short signed integer
%hu Short unsigned integer
%Lf Long double
%d Decimal integer (signed), assumes base 10
%i Integer (detects base automatically)
%o Octal integer
%x Hexadecimal integer
%p Pointer
%f Floating point number

Example 1

#include <iostream> using namespace std; void takeInput() { int x; char s[50]; // C like string or character array char c; float f; cout << "Enter an integer: "; scanf( "%d", &x ); cout << "\nYou have entered an integer: " << x << endl; cout << "Enter a character: "; scanf( " %c", &c ); cout << "\nYou have entered a character: " << c << endl; cout << "Enter a float value: "; scanf( "%f", &f ); cout << "\nYou have entered float value: " << f << endl; cout << "Enter a string: "; scanf( "%s", s ); //string do not need address //convert to C++ like string from C like string string SCpp; SCpp.assign(s); cout << "\nYou have entered the string: " << SCpp << endl; } int main(){ takeInput(); }

Output

Enter an integer: 5
You have entered an integer: 5
Enter a character: K
You have entered a character: K
Enter a float value: 2.56
You have entered float value: 2.56
Enter a string: HelloWorld
You have entered the string: HelloWorld

In this approach, it works well for other datatypes but for strings, it only accepts C-like strings or character arrays. To display the string using “cout” we need to convert it into C++ like string object. Otherwise, we can use the printf() function to display the output. These are basic examples. Now let us see the effect of formatting string in the next example.

Example 2

#include <iostream> using namespace std; void takeInput() { int dd, mm, yyyy; cout << "Enter a date in dd-mm-yyyy format: "; scanf( "%d-%d-%d", &dd, &mm, &yyyy ); cout << "\nThe given date is: "; printf( "%d/%d/%d", dd, mm, yyyy ); } int main(){ takeInput(); }

Output

Enter a date in dd-mm-yyyy format: 14-10-2022
The given date is: 14/10/2022

In this example, we are taking inputs in the form (dd-mm-yyyy) this will not accept these three integer values in any other format. And in our output, we are displaying the same date in another format (dd/mm/yyyy). This is the actual use of formatted string input. In the next, we will see a simpler form to take inputs in C++ using the “cin” input stream to take any type of data directly to the specified variable.

Taking input using cin in C++

The cin is a C++ input stream class that takes input from streams using the extraction operator >>. This operator automatically inserts value to specified variable by taking inputs from the console. The syntax will be like the below.

Syntax

The basic syntax for the cin method

cin >> <input variable name>

Example 1

#include <iostream> using namespace std; void takeInput() { int x; string s; char c; float f; cout << "Enter an integer: "; cin >> x; cout << "\nYou have entered an integer: " << x << endl; cout << "Enter a character: "; cin >> c; cout << "\nYou have entered a character: " << c << endl; cout << "Enter a float value: "; cin >> f; cout << "\nYou have entered float value: " << f << endl; cout << "Enter a string: "; cin >> s; cout << "\nYou have entered the string: " << s << endl; } int main(){ takeInput(); }

Output

Enter an integer: 8
You have entered an integer: 8
Enter a character: L
You have entered a character: L
Enter a float value: 3.14159
You have entered float value: 3.14159
Enter a string: WeAreLearningC++InputTaking
You have entered the string: WeAreLearningC++InputTaking

Like other variables here we can directly take a string without taking it as a character array. In this approach, it is automatically assigning the given input to a string object. However, there is a problem regarding strings. We cannot input multi-word strings in this fashion. If we write a multi-word string it will only take the first word. Let us see this in the following example.

Example 2

#include <iostream> using namespace std; void takeInput() { string s; cout << "Enter a string: "; cin >> s; cout << "\nYou have entered the string: " << s << endl; } int main(){ takeInput(); }

Output

Enter a string: Hello World, This is a nice day
You have entered the string: Hello

To overcome this problem, we need to use the getline() function to take a string with spaceseparated words. In this method, it will end reading text when a new line character has been encountered.

Syntax

getline(std::cin, <string variable>)

Example 3

#include <iostream> using namespace std; void takeInput() { string s; cout << "Enter a string: "; getline(cin, s); cout << "\nYou have entered the string: " << s << endl; } int main(){ takeInput(); }

Output

Enter a string: Hello World, Have a nice day
You have entered the string: Hello World, Have a nice day

Conclusion

In this article, we have seen different usage of input taken from the user by using the scanf() method as well as the cin stream reader. Taking inputs to any other variable type is straightforward. But, both %s format specifier and cin class do not take input strings with spaces. Like C, a specified function is also present in C++ to read strings with spaceseparated words. The getline() method is can be used to take such input strings. We can also take inputs from files and also from string streams.

Updated on: 04-Apr-2023

623 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements