Receiving data from the user C++ - ProgrammerTech
Language:

Search

Receiving data from the user C++

  • Share this:
Receiving data from the user C++

cin in C++

The cin function to receive data from the user. In this lesson, we will learn about the cin command. We know that programming consists of inputs and outputs, and between them are logic and software units. Today we will learn how to receive Input via the keyboard using the cin command.

The cin command makes the user able to enter data into the program while he is working. We use the cin c++ input command in >>>. The cin c++ command is just like cout, but cout uses a signal less than two times to write the following, while cin uses a sign greater than twice to receive the next. Every time we invoke the cin command, the interpreter waits for us to enter a number or a word from the keyboard. And after completing the entry and clicking the Enter button, the thing that was entered will be returned to the place from which the cin >> command was called.

cin >>  receve reception
cout <<  write the following

example 1

#include <iostream>
using namespace std;
int main()
{
    int currentYear = 2022;
    int year;
    cout << "Enter The Year your berth....";
    cin >> year;
    int age = currentYear - year;
    cout << "age = " << age << endl;
    return 0;
}

At the beginning of the program we used the iostream library that contains the input and output commands, then we used the int main function and after that we started building the program. We defined an int variable named currentYear in order to install the current year in it, which is 2022. We created another variable to receive births and its name is year and then we wrote a message asking the user to write his date of birth and put the cin command to receive the required data from the user. Then we set the numerical variable age which had the currentYear minus the user's year of birth. And we wrote a print command that prints the word age, and then prints the user’s age, which we subtracted from the year of birth. And when implementing this program and writing 1995-borns, its result will be in the form.

Enter The Year your berth.... 1995
age = 27

Now you have the super tool cin with which you can make great applications and even games to show to your friends and play with them

Example 2

Write a program that takes your personal data (name, age and rate) and then prints the information it took.

#include <iostream>
using namespace std;
int main()
{
    string name;
    int age;
    double cs;
    cout << "user name" << endl;
    cin >> name;
    cout << "how old are you" << endl;
    cin >> age;
    cout << "What is your average" << endl;
    cin >> cs;
    cout << "Hi : " << name << endl;
    cout << "You are your age : " << age << endl;
    cout << "Your GPA is : " << cs << endl;
    return 0;
}

At the beginning of the program we used the iostream library that contains the input and output commands, then we used the int main function and after that we started building the program. We create a string variable named name in order to receive the name, then we create an integer variable named age. Receive the age and a double decimal variable named cs to receive the cumulative average. Then we placed a print command asking the user for his name. We wrote to him to ask him “your name.” We used the cin command, followed by the name of the variable name, that is, it prompted the user to write his name.

Then we created a second print function that asks the user his age by typing "how old are you". We created the cin command, followed by the variable age, which has a numeric type to receive the user's age numbers. And we built a third print function in order to ask the user about his rate by showing him "how much is your rate" and below that we created the cin command. And we put for him the name of the variable cs, which is responsible for receiving the rate, and the rate always comes in decimal form. The variable was of type double. Then we created three print commands to print the name, age, and rate.

And when the program is executed, the result will be

user name
Muhammad
how old are you
29
What is your average
6.4
Hi : Muhammad
You are your age : 29
Your GPA is : 6.4

We notice from the previous implementation that it required the user to write his name and he wrote muhammad. And when he wrote it, he asked him to write his age and he wrote it 27. Then he asked him to write his average and he wrote it 6.4. And when you press the Enter button at the end, the data is printed in an orderly manner.

 

Entering text with the getline() function

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 cin command to make the compiler wait for the user to enter what they want. It then encapsulates what will be entered by the getline() function, which will consider all that is entered as a single text.

getline(cin, x);

example 3

#include <iostream>
using namespace std;
int main()
{
    string admin;
    cout << "Enter your admin: ";
    getline(cin, admin);
    cout << "Welcome " << admin;
    return 0;
}

We created a text variable, admin, and this variable will store the value that the user will enter from the keyboard. Then we gave a message that appears to the user asking him to enter the name of the admin, and then the name of the admin will be stored in the text variable. Then we arranged the display of the data in an orderly manner.

When you run the code and write Saja Alsadig, the result will be as follows

Enter your name: Saja Alsadig
Welcome Saja Alsadig

Example 4

I am writing a calculator program using terms in C++ language and it has elementary operations (addition, subtraction, division, multiplication).

#include <iostream>
using namespace std;
int main()
{
    int choice;
    cout << "+1 or ÷ 2 or ×3 or -4" << endl;
    cin >> choice;
    if (choice >= 1 &&choice <= 4 )
    {
        int a, b;
        cout << "The first number = ";
        cin >> a;
        cout << "The second number = ";
        cin >> b;
        if (choice == 1)
            cout << a << " + " << b << " = " << a + b << endl;
        if (choice == 2)
            cout << a << " ÷ " << b << " = " << a / b << endl;
        if (choice == 3)
            cout << a << " * " << b << " = " << a * b << endl;
        if (choice == 4)
            cout << a << " - " << b << " = " << a - b << endl;
    }
    else
    {
        cout << "enter one of the four digits" << endl;
    }
    return 0;
}

We also note that a calculator program has been created that includes only four operations, which are the basic required and are placed within four conditions to choose the type of operation.

  • If the user initially enters the number 1, the process will be sum.
  • If user enters 2, the process will be split.
  • If you enter the number 3, the process will be multiplied.
  • If you enter the number 4, it will be a subtraction process.

When you run the program and choose the number 2, for example, and we enter two numbers for it.

1-7.webp

 

And if we enter the number 4 and give it two numbers.

2-6.webp

 

We note from the previous two operations that he was initially asked to specify the type of operation by choosing its number. Then he asked us to enter the first number, then he asked for the second number, and when entering it and pressing the Enter button, he showed us the process we had done with the output.

Example 5

Write a program in C++ that calculates the age of any person, we first give him the person's birth and then we give him the date of the current year and he prints the age on the screen.

#include <iostream>
using namespace std;
int main()
{
    int dob;
    cout<<"Enter your date of birth"<<endl;
    cin >>dob;
    int tcy;
    cout<<"Enter the birth of the current year"<<endl;
    cin >>tcy;
    cout<<"Your age is: "<<tcy-dob<<endl;
    return 0;
}

We have created a scalar variable, dob, and this variable is responsible for a person's date of birth when the program asks him to write his birthdate. Then we created a variable tcy which is responsible for entering the current year when requested by the user. Then we put it at the end to print the current date of birth for us and subtract it from the user's date of birth and print his age on the screen. When you press run, it will ask to enter the user's birth and then ask him for the current year, and after that it will print his age.

3-3.webp

 

I wrote to him the births of 1994, then he asked me the year we are in, and I put 2021, then he printed my age on the screen, which is 27 years old.

4-5.webp

 

Example 6

Write a program that calculates the percentage of a student's mark in an exam.

#include <iostream>
using namespace std;
int main()
{
    int exd;
    cout<<"Enter the full score for the exam"<<endl;
    cin>>exd;
    double yse;
    cout<<"Enter the full score for the exam"<<endl;
    cin>>yse;
    cout<<"Your score in percentage is :"<<(yse/exd)*100<<endl;
    return 0;
}

We created an exd variable to ask the user to enter the full exam score, i.e. the perfect score. Then we created a variable yse to ask the user to enter the student's score on the exam. And we asked him in the end to divide the user's score by the full score of the exam multiplied by 100 and print the percentage of the score on the screen as follows.

5-5.webp

 

We created an exd variable to ask the user to enter the full exam score, i.e. the perfect score. Then we created a variable yse to ask the user to enter the student's score on the exam. And we asked him in the end to divide the user's score by the full score of the exam multiplied by 100 and print the percentage of the score on the screen as follows.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com