User Input Data - The cin C++ Object

In this tutorial of our C++ course, we will learn how to receive user information from the keyboard at the command terminal using the cin << command.

Exchanging Information

To access a website, you need to enter the URL address and hit enter key.
To open a photo, you need to browse the file in the middle of the folders and click on it.
To listen to a song or video, you must do the same.

Want to type a text? You have to open the text editor and type, letter by letter, and hit the buttons, menus, and program options to format and make it pretty.

That is, you need to pass information to the computer (type something, click here or there, enter a URL etc). And based on what you do, it will do something.

If you click a button, it does one thing.
If you click another button, it does something else entirely different.
It reacts differently depending on the information the user gives.

This is what we are going to start learning how to do in C ++, and the first step is to learn how to receive user information from the keyboard. Come on?

How to Receive User Data: cin >>

Until now, all the information needed for our programs to function was given in code, such as initialization of variables with their values.
If we wanted to make the program run with other values, we had to change them in code, compile and run all over again. Imagine having to fiddle with the code of each program you use?

Now, let's learn how to make the user enter information and the program work on that data, whatever it is.

To do this, we use the cin object (let's learn in the object orientation section, what is an object, don't worry), which is an input stream object followed by the >>  symbol (stream extraction operator) and a variable:
  • cin >> variable_name;

What C ++ will do when it gets into this code is simply stop and wait for something to be typed on the keyboard, and when you press ENTER, the value you type will be stored in the variable_name variable.

Numbers User input with cin >>

Well, let's go.

The program below asks the user for an integer, their age value, stores it in the age variable and then displays the value entered:

#include <iostream>
using namespace std;

int main()
{
    int age;

    cout << "Type your age: ";
    cin >> age;
    cout << "Your age is " << age;

    return 0;
}

Then
C++ free tutorial


Already the program below asks two numbers to the user. Stores in variables num1 and num2.
Sum these values and store them in the sum variable, then display the sum value:

#include <iostream>
using namespace std;

int main()
{
    float num1, num2, sum;

    cout << "First number: ";
    cin >> num1;
    cout << "Second number : ";    cin >> num2;

    sum = num1 + num2;

    cout << "The sum is: " << sum;

    return 0;
}

Results:
How to sum two numbers in C++

We can also receive multiple user values by simply placing one or more >> operators next to each other. Let's redo the previous example.

Enter a number, hit space and enter another number:

#include <iostream>
using namespace std;

int main()
{
    float num1, num2, sum;

    cout << "Type two numbers: ";
    cin >> num1 >> num2;
    sum = num1+num2;

    cout << "The sum is: " << sum;

    return 0;
}


User input texts (strings)

A string (text, in programming) is nothing more than a group of characters.
Therefore, to read a string that the user types, we must declare a group of characters.

For example, to store a space of 50 characters, we declare:
char name[50];

In our Strings section we will study better why the statement is like this.
The program below asks your name and greets you:

#include <iostream>
using namespace std;

int main()
{
    char name[50];

    cout << "Type your name : ";
    cin >> name;

    cout << "Hello, " << name <<"! Are you fine?";

    return 0;
}

The result is:
User input in C++

Now test a full name that has white space.
What happened? We will better understand this in the string section of our course.

User Input Boolean Data in C ++

Finally, we can also receive boolean values.

#include <iostream>
using namespace std;

int main()
{
    bool val;

    cout << "Type something : ";
    cin >> val;

    cout << "This means, in boolean value: " << val;

    return 0;
}

Test by entering the value 0. What appeared?
Test by entering any other integer value. What happened?
Enter a character. What returns?

Exercises using cin>>

1. Run the code below.
Did it give worked correctly? What's his problem?

#include <iostream>
using namespace std;

int main()
{
    float num1, num2, sum;
    sum = num1 + num2;

    cout << "First number: ";
    cin >> num1;
    cout << "Second number : ";    cin >> num2;

    cout << "The sum is " << sum;

    return 0;
}


2. Write a program that asks for a number and displays the twice value.

3. Make a program that takes the side of a square and returns the value of its area.

4. Create a C ++ program that takes both sides of a rectangle and calculates its area (just use one cin command)

5. Make a program that gets its full name (including spaces) and prints it on the screen. Study and learn how to do this using the referral site link.


Study Reference

http://www.cplusplus.com/doc/tutorial/basic_io/

No comments:

Post a Comment