setting range to variables

Hi,

anyone know how to limit a user input?

e.g

#include <iostream>

using namespace std;

int main()
{
int userInput; /*i want the user input has to be between 1 - 100 and making sure that if the input number other than 1 - 100 it will give error or the repick the number. so how do i make that variable ? is it ( 1 < userInput < 100 ) */

cout << "Pick a number between 1 and 100: ";
cin >> userInput;

return0;
}

Thank you in advance.


Last edited on
Put it in a loop.

Repeat if:
- Not an int was inserted, OR
- an int less than 1 was inserted, OR
- an int greater than 100 was isnerted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
	int userInput; /*i want the user input has to be between 1 - 100 and making sure that if the input number other than 1 - 100 it will give error or the repick the number. so how do i make that variable ? is it ( 1 < userInput < 100 ) */

	do
	{
		cin.clear();
		cout << "Pick a number between 1 and 100: ";
		cin >> userInput;
	} while(cin.bad() || userInput < 1 || userInput > 100);
	
	return0;
}
Last edited on
Thank you, that was fast. Btw, what does cin.bad() do?
and how do you write in a format for the program in the box with
1
2
3
4
5
on the outline ?
sorry if i am asking too much. new too c++ and this forum xD
cin.bad checks if the cin operation has failed. If you do something like try to input a character while an integer is expected, this flag will be triggered.

I did do a mistake, above, I forgot to clear the cin.bad() flag with cin.clear()

TO get the number lines, you need to put code tags around your code. Put [co de] and [/co de] around your code.
What do you mean? I'm having a similar problem.
pardon if am asking too much but could u explain more about the cin.bad() and cin.clear() ?

i dont know how it works. if i input a char into a variable that suppose to be an integer it will just give an infinite loop even though there is cin.bad() written.

btw, thanks for pointing out the logical OR operator to set a range to variable.
I've never used cin.bad before but it appears to check that you receive valid input. You declare:

int UserInput.

This only takes integers, obviously. So cin.bad gets triggered if you attempt to store, say, a char within your int

What I don't understand is why it wouldn't just take the ascii value of a char?
Last edited on
I did do a mistake, above, I forgot to clear the cin.bad() flag with cin.clear()


This is the part I don't understand. How do you clear cin.bad with cin.clear?

I tried running the program when I put 0.1 in. It shouldn't work, and it doesn't, instead it outputs Pick a number.. an infinite number of times =S
cin.bad() indicates whether the input stream is in an error state.
cin.clear() clears the error indicators.

I thought the infinite loop was due to the fact that the offending input was still there on the input stream.

So I tried


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
 
using namespace std;
 
int main()
{
        int userInput; /*i want the user input has to be between 1 - 100 and 
making sure that if the input number other than 1 - 100 it will give error or the
 repick the number. */
       
        do
        {
	  if(cin.bad())
	    {
	      cin.clear();
	      cin.ignore();
 	    }
	  cout << "Pick a number between 1 and 100: " ;
	  cin >> userInput;
	} while( cin.bad() || (userInput < 1) || (userInput > 100));
	
        return 0;
}


But this also result in an infinite loop when given a character.

But the following works as expected:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
 
using namespace std;
 
int main()
{
  int userInput; /*i want the user input has to be between 1 - 100 and making
 sure that if the input number other than 1 - 100 it will give error or the repick
 the number.  */
  cout << "Pick a number between 1 and 100: " ;
  while (true)
    if ( (!(cin >> userInput)) || (userInput < 1) || (userInput > 100))
	    {
	      cout << "Pick a number between 1 and 100: " ;
	      cin.clear();
	      cin.ignore();
	    }
	    else break;

return 0;
}



Can someone please help me understand why the first code result in an infinite loop on a character input?
Last edited on
Try checking cin.fail() instead of cin.bad().

cin.bad() only checks the badbit flag, which isn't usually the one that gets set when cin fails upon entering a character instead of an integer.

cin.fail(), on the other hand, also checks if the failbit flag is set. (that's the one that usually gets set when cin fails like this)

You could also just put
if (!cin.good())
so you won't have to worry about which error flags got set (cin.good() checks all of them).
Thanks long double main (96). You are correct, the cin.bad() clause was not triggering.

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>
 
using namespace std;
 
int main()
{
        int userInput; /*i want the user input has to be between 1 - 100 and 
making sure that if the input number other than 1 - 100 it will give error or the
repick the number. ) */
       
        do
        {
	  if(cin.fail())
	    {
	      cin.clear();
	      cin.ignore();
 	    }
	  cout << "Pick a number between 1 and 100: " ;
	  cin >> userInput;

	  if(cin.bad())
	    cout <<"Bad input"<<endl;
	  //The cin.bad() clause does not trigger
	  
	} while( cin.fail() || (userInput < 1) || (userInput > 100));
	
        return 0;
}


OP: you should follow the advice given in
http://www.cplusplus.com/forum/articles/6046/
for validating input
Last edited on
woots...many reply o0.
sometimes even though cin.bad or fail is used.
it will just give an infinite loop.
and if it does not give an infinite loop it will count the number of character of words u input or the digit. and it will do the number of loops.

e.g
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Guess my number
// Demonstare computer guessing my number.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	int randomNumber = rand();
	int tries = 0;
	int secretNumber;
	int x = 99;
	int lol;

	cout << "\tGuess My No.\n\n";

	do
	{
    if ( cin.fail() )
	{
		cin.clear();
		cin.ignore();
	
	}
		cout << "Please choose a number between 1-100: ";
		cin >> secretNumber;

	if ( cin.fail() || secretNumber<1 || secretNumber>100 )
	{
		cout << "You made an illegal choice\n";
	}

	}while ( cin.fail() || secretNumber<1 || secretNumber>100 );

	cout << "\nThe computer is guessing.\n";


	do
	{
		lol = (rand() % x) +1;
		cout << ".\n";

		++tries;
	}while ( lol != secretNumber );

	cout << "Computer managed to guess it in " << tries << " tries.\n";

	system ("pause");

	return 0;
}


try input aaaaa u will see what i meant
Last edited on
Topic archived. No new replies allowed.