Restricting User Input to Integers in C++

Restricting User Input to Integers in C++

Page 1 of 1

6 Replies - 26373 Views - Last Post: 28 March 2007 - 04:19 PM Rate Topic: -----

#1 Max_Power82   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 06

Restricting User Input to Integers in C++

Posted 28 March 2007 - 09:50 AM

HI i want to restrict user input to positive integers greater than 0. I've come up with the while loop below which neatly prevents negative numbers and non integers, but i've no idea how to stop users inputting a text string, which crashes my program.

Thanks for your help!

 int main(){
	cout << "Question 2\na.  Please select the dimension of square matrices you wish to use in this demonstration.  n=";
	float n;  //n must be an integer, however the declaration of n as float allows the while loop below to filter out any user intput float number.
	cin >> n;
	while((n <1 || n - floor(n) != 0)
		{cout << "\nPlease enter an integer value of n greater than or equal to 1.  n="; 
		cin >> n; 
		cout << n;};
return 0;}
  


Is This A Good Question/Topic? 0
  • +

Replies To: Restricting User Input to Integers in C++

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Restricting User Input to Integers in C++

Posted 28 March 2007 - 10:01 AM

A quick and dirty way to ensure data validity is to use the .good() flag from the cin object. See ifstream operation about 2/3 down the page:

http://faculty.junia.../cs110/ch4b.htm

Another method is here:

http://www.dreaminco.../snippet591.htm

The following is one of the best ways to validate an integer I've seen:

http://home.att.net/...ode/strtol.html
Was This Post Helpful? 0
  • +
  • -

#3 Max_Power82   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 06

Re: Restricting User Input to Integers in C++

Posted 28 March 2007 - 11:08 AM

Thanks very much Mr Amadeus. I like the first method, the simple cin.good() method that's just what I need, but unfortunately it's not working, as before, when i input a string, even though the cin.good() flag is set to 0 as expected, the program output just loops endlessly though the while loop WITHOUT EVER ALLOWING THE USER TO INPUT VIA THE CIN!!!) It just says, "Please enter an integer value of n greater than or equal to 1 n= 1.0717e-008." over and over again.

As I said, I tried it with a debugging line to check that cin.good() was actually equal to zero after string input, it is. So the problem is somethign else, it is that the while loop keeps on going forever because it won't do as it has been asked and prompt the user for a new value. Do you know what's wrong? thank you very much

int main(){
	cout << "Question 2\na.  Please select the dimension of square matrices you wish to use in this demonstration.  n=";
	float n;  //n must be an integer, however the declaration of n as float allows the while loop below to filter out any user intput float number.  This is better than the float being cast to an int by the program, because the user is made aware of their error, and a message will appear.
	cin >> n;
	//cout << "cin.good is " << cin.good();
	while(n <1 || n - floor(n) != 0 || !(cin.good()))
		{cout << "\nPlease enter an integer value of n greater than or equal to 1.  n="; 
		cin >> n;
		cout << n;};

return 0; }



Was This Post Helpful? 0
  • +
  • -

#4 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Restricting User Input to Integers in C++

Posted 28 March 2007 - 11:13 AM

Worked OK for me...what behaviour are you expecting? note, I removed the trailing semi colon after your while loop.
int main()
{
	cout << "Question 2\na.  Please select the dimension of square matrices you wish to use in this demonstration.  n=";
	float n;  //n must be an integer, however the declaration of n as float allows the while loop below to filter out any user intput float number.  This is better than the float being cast to an int by the program, because the user is made aware of their error, and a message will appear.
	cin >> n;
	//cout << "cin.good is " << cin.good();
	while(n <1 || n - floor(n) != 0 || !(cin.good()))
		{
			cout << "\nPlease enter an integer value of n greater than or equal to 1.  n=";
		cin >> n;
		cout << n;
		}
		
return 0; 
}


perhaps the conditions are not as you expect? Remember that you will need to clear the buffer (cin.clear()) to reset if you are using .good().
Was This Post Helpful? 0
  • +
  • -

#5 Max_Power82   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 06

Re: Restricting User Input to Integers in C++

Posted 28 March 2007 - 12:39 PM

I was expecting that when the while loop kicks in, [as a result of a string being entered and the cin.good() being set to 0], then the program will prompt the user to enter a new value of n, rather than loop endlessly over and over again saying "Please enter an integer value of n greater than or equal to 1. n=".

I checked that the cin.good() is being set to 0 by the entry of a string, so the problem is now that the

cin >> n

is being completely ignored in the while loop.

do you know what's causing it?

Thanks again.

int main(){
	cout << "Question 2\na.  Please select the dimension of square matrices you wish to use in this demonstration.  n=";
	float n;  //n must be an integer, however the declaration of n as float allows the while loop below to filter out any user intput float number.  
	cin >> n;
	//cout << "cin.good is " << cin.good();
	while(n <1 || n - floor(n) != 0 || !(cin.good()))
		{cout << "\nPlease enter an integer value of n greater than or equal to 1.  n="; 
		cin >> n;
		//cout << n;
	}

return 0;}


Was This Post Helpful? 0
  • +
  • -

#6 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Restricting User Input to Integers in C++

Posted 28 March 2007 - 02:01 PM

if you get an error on
	cin >> n;


and cin.good() fails you need to clear the error condition and remove the corrupt characters from the input stream, e.g.
	//cout << "cin.good is " << cin.good();
	while(n <1 || n - floor(n) != 0 || !(cin.good()))
		{
		cin.clear();			   // clear error condition
		cin.ignore(100,'\n');  // remove characters
		cout << "\nPlease enter an integer value of n greater than or equal to 1.  n=";
		cin >> n;
		cout << n;
		}


Was This Post Helpful? 0
  • +
  • -

#7 Max_Power82   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 06

Re: Restricting User Input to Integers in C++

Posted 28 March 2007 - 04:19 PM

that's awsome, thank you very much for helping Mr Horace.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1