Home > Backend Development > C++ > body text

C++ program to get input from user

WBOY
Release: 2023-09-08 16:17:02
forward
1030 people have browsed it

C++ program to get input from user

When writing a program in any programming language, receiving input is the basic job we do in almost all programs. Sometimes we get input directly from the console and sometimes we get input from a file. Getting input from a file has certain benefits as it does not require us to type it over and over again and sometimes we can save some good input test cases to a file. However, in this article we will focus on console-based input. We will learn different techniques for getting input from the user in C.

There are several different ways to get input from the console. Some of them are C-like methods, while others use input streams that exist in C. We will cover them one by one and provide some examples for better understanding.

Use the scanf() function to receive input

In C language, we use the scanf() function to scan input from the console in the form of a formatted string. This function is also available in C, so to receive input in a formatted form, use the scanf() method.

grammar

Basic syntax of the scanf() method, including format string.

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

scanf() formatted format specifier.

The Chinese translation of is:
Format specifierDescriptionDescription
%c For single character input
%s For strings without spaces
%Hi Short signed integer
%hu Short unsigned integer
%Lf 长双
%d Decimal integer (signed), assuming base 10
%i Integer (automatically detect base)
%o Octal integer
%x Hexadecimal integer
%p pointer
%f Floating point number
The Chinese translation of

Example 1

is:

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();
}
Copy after login

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
Copy after login

In this method, it works for other data types, but for strings, it only accepts C-like strings or character arrays. To display a string using "cout" we need to convert it to a C-like string object. Otherwise, we can use printf() function to display the output. These are basic examples. Now let's see the effect of formatting the string in the next example.

Example 2

is translated as:

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();
}
Copy after login

Output

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

In this example, we receive input in the form (dd-mm-yyyy), it will not accept any other format for these three integer values. And in our output, we display the same date in another format (dd/mm/yyyy). This is what formatted string input is actually for. Next, we'll see a simpler form using the "cin" input stream to directly input any type of data into a specified variable.

Use cin to receive input in C

cin is a C input stream class that uses the extraction operator>> to obtain input from the stream. This operator automatically inserts a value into the specified variable by getting input from the console. The syntax is as follows.

grammar

Basic syntax of cin method

cin >> <input variable name>
Copy after login
The Chinese translation of

Example 1

is:

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();
}
Copy after login

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
Copy after login

Like other variables, we can use strings directly instead of making them as character arrays. In this method, it automatically assigns the given input to a string object. However, there is a problem with strings. We cannot enter multi-word strings this way. If we write a multi-word string, it will only take the first word. Let us see this in the example below.

Example 2

is translated as:

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();
}
Copy after login

Output

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

To overcome this problem, we need to use the getline() function to get a space-delimited string. In this method it ends reading the text when a newline character is encountered.

grammar

getline(std::cin, <string variable>)
Copy after login
The Chinese translation of

Example 3

is:

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();
}
Copy after login

Output

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

in conclusion

In this article, we have seen different usages of reading user input using the scanf() method and the cin stream. Assigning inputs to other variable types is straightforward. However, neither the %s format specifier nor the cin class accept input strings with spaces. Like the C language, there is a designated function in C for reading a string with space-separated words. You can use the getline() method to accept this input string. We can also get input from files and string streams.

The above is the detailed content of C++ program to get input from user. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!