Advertisement Jump to content
Sign in to follow this  

Unity Getting numerical input (long post)

This topic is 4912 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Here's more trouble I'm having. It's again a problem with checking to see whether or not an integer has been entered, and how to deal with it. I've tried advice taken from an earlier thread of mine (retired now), and also that page Zahlman finds useful. It's still a lottery simulator I'm trying to do. But I'm having trouble determining whether or not the supplied input is an integer or not. Here's the relevant code I have at the moment:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <list>
#include <limits>
#include <string>

std::list<int> InputNumbers; // Player input numbers
std::list<int>::iterator InputCounter; // Player's list iterator

void GetNumbers()

	int Input;


		int Remaining=6-InputNumbers.size();

		std::cout<<std::setw(10)<<"\n\nEnter a number between 1 and 49.  "<<Remaining<<" numbers still need to be chosen.   ";

			std::cout<<"\nPlease enter a valid number.";






	//int NumberInputs;

The behaviour is a little bizarre. I use this as test data: 1m3 33 m55 1s2 m2i3 2s6g The letters are there to test the robustness of the input handler. As you can see, the theory is that if the input is invalid (i.e. not an integer), there should be some kind of error handling going on. Thing is, the cin buffer is read one character at a time, so using my test data gives me this: As you can see, the buffer isn't clearing properly, and the results are listed at the bottom. I can't see a way round this behaviour. Hacking around with the test data sometimes leads to list elements holding the value -83029409 etc. I know I could use std::getline(), but all that did was put 0 into the list element if the input was invalid. This is really irritating, actually. If any of you have any ideas about how to hack around this, I'd love to hear them. Thanks for your help, ukdeveloper.

Share this post

Link to post
Share on other sites
When you get invalid input, you can clear the input buffer - try stream.ignore(std::numeric_limits<int>::max(), '\n');.

That will clear the rest of the line, regardless of its length.

Share this post

Link to post
Share on other sites
You add Input to your list whether it is valid or not. You need that line to be in an else statement, that way invalid inputs don't result in odd values getting stored.

> 1 is stored to Input, 5 left, m3 still in cin
> m is invlid, cin is cleared, but 1 is still Input, so it has been added twice
> 33 is stored to Input, 3 left, cin empty
> m is invlid, cin is cleared, but 33 is still Input, so it has been added twice
> 1 is stored to Input, 1 left, s2 still in cin
> s is invlid, cin is cleared, but 1 is still Input, so it has been added twice

Which brings us to the end.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!