C++ User Input - std::cin

C++ User Input is done mainly through the cin object.

std::cin>>variable;

We have already learned the usage of cout, and cin is also used in mostly the same way, except the data flow direction is now reversed. And also note that we have to declare a variable before using cin because we need to store the data coming from the user. So, a complete application will be like this.

#include <iostream>

int main (){

    int number;

    std::cin>>number;

    std::cout<<number;

}

So, when the application is run, user will be prompted and then the user can input a value like 212, and then the program will output that number. So, talking about how to get user input in C++, it is pretty easy because we already have used cout pretty much. But, please do note the syntax. Make sure to use >> because remember, it denotes the direction of data flow (a simple trick to remember).

But, when it comes to what really happens, this becomes interesting. But I am not going to linger on it for so long, but I guess this will give a brief explanation on what’s going on.

So, just like cout, cin is also an object and the >> sign is overloaded in the istream class. And hence, how this works is, when we enter data in the console, that data is passed to the cin object and then it is passed to the variable next to it. However, when input is taken from the console or standard input stream, inputs are differentiated with spaces (or with characters which is a special case). Hence, if we enter 2 numbers with spaces in between, this program will just grab the first one and output it because the cin identifies them as 2 inputs. This really becomes such an important case when you try to get more sophisticated input using strings with spaces and stuff. So, as the cin differentiates input using white spaces, those words you type, are identified as different inputs. Hence, what you input will not be the output. Take a look.

#include <iostream>

int main(){

    char name[25];

    std::cin>>name;

    std::cout<<name;

}

And the output will be fine as long as you won’t type spaces.

So, when it comes to getting input like this, you need to use some other method. Which is the getline() method built to the cin object.

So, to use this method, you just type the following.

#include <iostream>

int main(){

    char name[25];

    std::cin.getline(name,25);

    std::cout<<name;

}

Note that this function takes 2 parameters. 1st one, the variable and the 2nd is the number of characters you want to capture.

Now, I know it is a bit annoying that you have to specify the number of characters but there are ways to get around this. And we will talk about it when we get to strings (yeah, yeah the string class).

But, FYI, sometimes, this method is also useful, like let’s say you want to get an input for some database and you don’t want the user to mess up your system, in that case you can limit the number of characters and make the app safer.

So, that’s it for this tutorial. There is a lot to talk about that we didn’t really get into just so we can maintain the simplicity. But if you want to get your hands dirty, make sure let us know in the comments section!


No comments:

Post a Comment