limit input to only integer

Hi guys, I'm pretty new to C++ and I've been trying to figure out how to limit user input to only integer. I've found out a way using while loop to get rid of string inputs but when input is decimal my program crashes. Can someone shed a light on how it's supposed to be done please?

Here is part of my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  
int bunny

cout << "\n"  << "Number of Bunnies intended to pat: ";
	
	
	while (!(cin >> bunny)) 
	{
		cout << "Entry must be a whole number! Try again: " ; 
		cin.clear();
		cin.ignore(500, '\n');
	
	
	}
	
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cctype>

int main()
{
    int num_bunnies = -1 ;

    while( num_bunnies < 1 )
    {
         std::cout << "\nNumber of Bunnies intended to pat: ";

         char c ;
         // if failed to read or the character just after the number is not a white space
         if( !( std::cin >> num_bunnies ) || ( std::cin.get(c) && !std::isspace(c) ) )
         {
             std::cout << "Entry must be a whole number! Try again: " ;
             std::cin.clear();
             std::cin.ignore( 500, '\n' );
             num_bunnies = -1 ; // go through the loop again
         }

         else if( num_bunnies < 1 )
            std::cout << "Number of Bunnies must be a positive number! Try again: " ;
    }

    std::cout << "#bunnies == " << num_bunnies << '\n' ;

}
Thank you JLBorgers! It works perfectly! However, as a total novice at C++ myself, there were a few things I didn't understand and I would appreciate if you don't mind elaborating for me:

1. When set up the integer = -1, what does it do exactly?
int num_bunnies = -1 ;
nvm I figured it out.
2. I didn't understand the second part of the if statement and the declaration of "char c". I would really appreciate good elaboration.



Last edited on
The -1 is just a set value to let you know that the number that was input was invalid or there was no input, in other words used as an error flag. The second part of the if statement means if there are more characters left in the buffer (other than whitespaces -- newline left from cin >>) then read it in and if it did read in any clear the error flags , ignore everything that could possible be left in the buffer and set bunneis to -1(letting you know to run the loop again as it is an invalid input).
Thank you giblit for the explanation. I understand the error triggering with -1 now as well as the loop, but I'm still kinda confused on
if( !( std::cin >> num_bunnies ) || ( std::cin.get(c) && !std::isspace(c) ) )

What exactly do cin.get(c) and isspace(c) do? I'm guessing the logic behind this is if these two conditions are both false then it would trigger the scenario below them, and I understand the first part before "||" means if cin does not match the data type being declared, but how does the part after "||" work? Once again I really appreciate all the answers.
In if( !( std::cin >> num_bunnies ) || ( std::cin.get(c) && !std::isspace(c) ) )

If std::cin >> num_bunnies is successful, ( !( std::cin >> num_bunnies ) is false ), then

1. Extract the very next character in the input buffer into c with
std::cin.get(c) http://www.cplusplus.com/reference/istream/istream/get/

2. Check if that character is a white space with
std::isspace(c) http://www.cplusplus.com/reference/cctype/isspace/

3. If it is not a white-space character, a whole number was not entered

eg. If the input buffer contained 123.78<newline>, 123 would be read into num_bunnies, and . would be read into c, . is not a white-space character.

If the input buffer contained 123xy<newline>, 123 would be read into num_bunnies, and x would be read into c, x is not a white-space character.

If the input buffer contained 123<newline>, 123 would be read into num_bunnies, and newline would be read into c, newline is a white-space character.

If the input buffer contained 123 <newline>, 123 would be read into num_bunnies, and space would be read into c, space is a white-space character.
Thank you JLBorgers for the detailed explanation!
Topic archived. No new replies allowed.