input in c

Posted under C++ Course On By mohammed.mutawe

Input in c

We have seen in the print sentence lesson, how to show the values ​​that were defined inside the code and display them on the console screen, and in this lesson we will learn the function of entering values ​​in C++, which in turn works to save the value of the input and put it in the reserved variable that we defined.

 

Cin

Since the cin function is one of the functions of the standard C++ library, access to variables is very fast because C is one of the fastest languages ​​to implement tasks due to its proximity to the machine language, and there are characteristics and conditions for the cin input statement, namely:

  • The input must be of the same type as the variable.
  • Entering is followed by pressing the Enter button on the keyboard.
  • Define the variable in the cin input statement.

 

input process

We will define a variable for the input process, let it be int x, and we will put a value of type integer inside it, and then we will print the sentence on your console screen as follows:

  • Define a variable inside the main followed by a print statement to enter the age, for example:
#include <iostream>
using namespace std;

int main()
{
    int x;

    cout << "Please Enter Your Age: ";


    return 0;
}

 

  • Define the input statement cin so that the code looks like this:
// i/o example

#include <iostream>
using namespace std;

int main()
{
    int x;

    cout << "Please Enter Your Age: ";
    cin >> x;


    return 0;
}

 

  • Now that we have entered, we want to display the value of the input, so the code will be as follows:
// i/o example

#include <iostream>
using namespace std;

int main()
{
    int x;

    cout << "Please Enter Your Age: ";
    cin >> x;
    cout << "Your age is: " << x;

    return 0;
}
  • And so on, so that the final output will look like this on the console screen:

 

Example

Please, for a better understanding of the Cin function, copy the following code into your code editor.

// i/o example

#include <iostream>
using namespace std;

int main()
{
    int x;

    cout << "Please Enter Your Age: ";
    cin >> x;
    cout << "Your age is: " << x;

    return 0;
}

 

 

Leave a comment

Your email address will not be published. Required fields are marked *