![]() |
| ||
| User Input: Strings and Numbers [C++] User input/output is a key component in the programs you write, yet most online tutorials just provide a quick-and-dirty method of grabbing data. In other words, it works right up until the point the user does something unexpected. In this tutorial you will learn how to avoid the common mistakes by doing it properly. Take this typical method that tutorials use to teach user input: int number; Now let's say you want the user to enter a string that contains a space in it. Since using cin >> myString won't work (it will only grab the first word and then exit), you decide you need to find a function that will grab a whole line of input.After some Googling, you decide that getline() is probably the best way to do it. You add it to your existing code, and this is how your entire program looks: #include <iostream> Now you run it and the program totally skips the getline() statement! Please enter a number. That's rather odd. Let's try using only getline for our input (which means the number will be put into a string instead): #include <iostream> The program's output being: Please enter a number.The problem went away. You will probably suspect something is up with cin and are right. What's actually happening in the first example is:
To fix this the best approach, although not that easy for newbies, is to avoid the use of cin until you know what you're doing, and let getline() handle your user input. It not only solves the newline problem, but it also solves a number of other problems induced by using cin.This is relatively easy for people to do until they get to numbers (which usually can't be read into strings because they need to be manipulated). For this, you can use a stringstream to convert the string back into a number:#include <iostream> This outputs: Please enter a number. You can now see how the operation is nearly identical, except that you are taking an extra step by first putting the input a string, and then putting it into a separate stream before popping it back out in the number. Finally, you actually can mix cin with getline(), if you know what you are doing. The best method is to use cin.ignore(<some large number>, '\n') to clear the input buffer. You could simply hard code a large number, although it's usually better to use a built-in constant. The Standard Template Library provides such a constant, which is numeric_limits<streamsize>::max() If you were to implement this in your first example, it would work like this: #include <iostream> And the output would be: Please enter a number. However, this should only be used when absolutely necessary, as this is just a band-aid for the function. It doesn't really fix some of the other problems that come with using it. So perhaps it is best to stick with getline(). Don't do the quick-and-dirty method that online tutorials teach you, because it will eventually come back and bite you in the back of the neck. Flushing the input buffer like mentioned previously is only a last resort for using cin; it's not a good alternative to using getline() to handle your input. A little bit of trouble now using getline() will save you a lot of pain later! |
| ||
| Re: User Input: Strings and Numbers [C++] Can you explain what cin.ignore actually does, and why we need to specify some randomly large number? Is there no command to just clear the input buffer of the newline character? |
| ||
| Re: User Input: Strings and Numbers [C++] >Can you explain what cin.ignore actually does cin.ignore basically ignores (another way of saying 'trashes') characters from the input buffer. >why we need to specify some randomly large number? That number is the maximum number of characters that cin.ignore trashes before it encounters a newline character. Theroretically, you would only need 1 to trash a newline, but you never know what else is sitting in the buffer, and this is a pretty safe way of emptying it. >Is there no command to just clear the input buffer of the newline character? You could simply use cin.get() to manually remove character-by-character from the input buffer, like this: int ch; // notice that this is int, not char, so it can hold larger valuesThis method isn't as reliable for clearing the input buffer though, so it's better to stick with cin.ignore(). Additionally, it's a good idea to call cin.clear() before clearing the input buffer, because what cin.clear does is erase any bad bits (errors) that have occured. If there are errors that haven't been cleared, you can't do read (or clear) the input buffer. |
| ||
| Re: User Input: Strings and Numbers [C++] Coding the tutorial with the magic number given to ignore function would not be such a good idea. Either declare the constant at the start of the program, in a header file or better yet use inbuilt constants like max value provided in the limits header file. Another robust method for accepting a integer worth incorporating in your tutorial is: int main() Hope, it made sense, bye. |
| ||
| Re: User Input: Strings and Numbers [C++] >Coding the tutorial with the magic number given to ignore function would not be such a good idea. Well, I know that it's far better to use something like this, std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');but I didn't want to scare newbies with a long chunk of code, when I was simply trying to demonstrate clearing the input buffer. But if you think it should be changed, I can get Davey to edit it... >Another robust method for accepting a integer worth incorporating in your tutorial is: I wanted to keep the tutorial relatively simple, so I didn't really cover input validation. I'm considering writing some code snippets that would accomodate this (and perhaps link to it at the end of the tutorial, much like Dave Sinkula did in his excellent C input tutorial). ~s.o.s~, thank you very much for your suggestions... |
| ||
| Re: User Input: Strings and Numbers [C++] Considering that you have already declared using namespace std at the top of your code, the complicated statement becomes cin.ignore(numeric_limits<streamsize>::max(), '\n');Not so complicated, is it ? ;) But even so please make the changes since we want publication quality tutorials and we don't need the magic numbers to mar that. > I wanted to keep the tutorial relatively simple, so I didn't really cover input validation. No problem as such, include the code which I pasted whenever you think its appropriate. Just wanted to let you know. And last but not the least, really good tutorial. I am waiting for the next one in the series... ;) |
| ||
| Re: User Input: Strings and Numbers [C++] >Not so complicated, is it ? Well, I was kind of trying to exaggerate the effect of using that statement by adding the std:: prefix to make my point... ;) >But even so please make the changes since we want publication quality tutorials and we don't need the magic numbers to mar that. Alrighty then, I'll make some modifications, and then send them to Davey for approval. >And last but not the least, really good tutorial. Thank you! >I am waiting for the next one in the series... When I have some more time I'll get around to it... |
| ||
| Re: User Input: Strings and Numbers [C++] Read the content, I realized that the convinence of the function getline(). I have been using the following method to process input containing number and string. Quote:
|
| All times are GMT -4. The time now is 10:47 pm. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC