Enter data from the user in C Plus Plus
Introduction In the previous lessons, we were writing the code and then testing it and it was executed as is, meaning that we already knew what would appear when the code was run because we were setting the values of the variables before running the program.
In this lesson, you will learn how to create a program that interacts with the user, as when you run it, the user will be asked to enter data, and after entering it, the program will process it and do something specific with it.
Enter data from the user in C Plus Plus Introduction In the previous lessons, we used to write the code and then try it, and it will be executed as it is, meaning that we already knew what would appear when running the code because we were setting the values of the variables before running the program. In this lesson, you will learn how to create a program that interacts with the user, since when you run it, the user will be asked to enter data, and after entering it, the program will process it and do a specific thing with it . Cin input command in C ++
Input command cin
in C++
To make the user able to enter data into the program while it is running, we use the input command cin >>
.
The command cin
is in the package std
, so you must write std::cin
if you do not want to include the package std
in your program.
Every time you call this command the translator waits for you to enter a number or a single word from the keyboard ( Keyboard ).
After completing the entry and clicking on the button, the Enter thing you entered will be returned to the place from which the command was called cin >>
.
Input command example cin
in C++
#include <iostream> using namespace std; int main() { // To store the information that the user will enter later, we have set the two variables string name; int age; // name here the user will be asked to enter his name and then the name he enters will be stored in the variable cout << "What's your name?"; cin >> name; // age here the user will be asked to enter his age and then the number he enters will be stored in the variable cout << "How old are you?"; cin >> age; // Finally, we display the information entered by the user in an orderly manner cout << "Your name is " << name << ", and your age is " << age << " years."; return 0; }
• We will get the following result when running, noting that we have marked the data that we waited for the program to be entered from the keyboard in yellow.
How old are you? 25
Your name is Saddam, and your age is 25 years.
The difference between command cin
and command cout في
C++
In the beginning, knowing why the commands are called these words will make it easier for you to remember the difference between them, so always remember their origin:
cin
means the word See In Which in turn is used to insert a value into the variable.cout
means the word See Out Which in turn is used to get the value of the variable.
The command is cin
followed by the name of the variable that we will store the value entered by the user in it, and the arrows to the right are as follows.
// x store what the user will enter into the variable cin >> x;
After that , cout
we put anything we want to display or any variable we want to display its value, and the arrows are to the left as follows.
// x display the value entered in the variable cout << "x = " < < a;
Problems caused by entering blank spaces in C++
Always remember that the command cin >>
is used to enter a number or a single word only, meaning that what you enter should not contain blank spaces.
If what you entered contains blank spaces, this will cause problems in the code because the compiler will consider that you are trying to enter a set of values and not a single value.
When the compiler finds you have entered a set of values at the same time that you are trying to store only one value in a variable, it will consider that you want to enter the other values the next time you ask the user to enter values.
In the following example, we created a program that asks the user to enter his name and e-mail, after which it shows him what he entered.
When running the program, we will deliberately enter the full user name (his name and last name) with a blank space between them until you see the error that will occur.
Example of a program that asks the user to enter his name and e-mail, after which it shows him what he entered.
#include <iostream> using namespace std; int main() { // To store the information that the user will enter later in the name and email we have prepared the two variables string name; string email; // name here the user will be asked to enter his name and then the name he enters will be stored in the variable cout << "Enter your name: "; cin >> name; // email Here the user will be asked to enter his email and then the email he enters will be stored in the variable cout << "Enter your email: "; cin >> email; // Finally, we display the information entered by the user in an orderly manner cout << "\n\n---------------------"; cout << "\nName = " << name; cout << "\nEmail = " << email; return 0; }
• We will get the following result when running, noting that we have marked the data that we waited for the program to be entered from the keyboard in yellow.
Enter your email: <- Note that he did not wait for us to enter the email but considered it entered
---------------------
Name = saddam <- name only in the variable saddam Note that it has put
Email = alawiri <- by default email in the variable alawiri Note that it has put
Entering text by the function getline()
in C++
If you want to make the user able to enter more than one word and store everything he enters in one text variable, you can use the command cin
to make the compiler wait for the user to enter what he wants, and then wrap what will be entered by the function getline()
that will consider everything entered It is one text.
Entering text with the getline() function in C++ If you want to make the user able to enter more than one word and store everything he enters into one text variable, you can use the cin command to make the translator wait for the user to enter what he wants, and then encapsulate what will be entered by the getline () function, which will be considered All entered is one text.
So the following command is what you need to receive a text from the user.
// x stores all text entered by the user in the variable getline(cin, x);
Note: The function getline()
is in the package std
, so you must write std::getline(std::cin, x)
if you do not want to include the package std
in your program.
In the following example, we have defined an example that asks the user to enter his full name (his name and family name) and then displays a welcome message to him.
Example of entering text with the function getline()
in C++
#include <iostream> using namespace std; int main() { // To store the name that the user will enter later, we have prepared the variable string name; // name here the user will be asked to enter his full name and then the name he enters will be stored in the variable cout << "Enter your name: "; getline(cin, name); // Finally, we display the information entered by the user in an orderly manner cout << "Welcome " << name; return 0; }
• We will get the following result when running, noting that we have marked the data that we waited for the program to be entered from the keyboard in yellow.
Welcome saddam alawiri