Specific input

Im supposed to let the user cin a number that has to be a 4-digit number.
how can i do so?


Read it as a std::string, make sure it's length() is 4 characters long, make sure all four characters are digits (http://www.cplusplus.com/reference/cctype/isdigit/).
1
2
3
4
5
6
7
8
9
10
11
12
13
while(std::cout << "Please enter a 4 digit integer: " && (!(cin >> input) || input < 1000 || input > 9999))
{
    if(!cin)
    {
        std::cerr << "Error - invalid input." << std::endl;
        std::cin.clear();
        std::cin.ignore(1024, '\n');
    }
    else
    {
        std::cerr << "Error - invalid range of input." << std::endl;
    }
}
@giblit: 0123 is four digits and is not accepted by your program.
Yeah, I suppose if it's for like an ID or something, most people don't use leading zeros. They weren't explicit on leading zeros or not. But, I guess technically 4 digits would be 0-9.
The way I see it, why arbitrarily disallow 1000 possible combinations? Especially considering a 4-digit PIN is incredibly insecure already.
See, you made the assumption it was for a PIN and I made the assumption it had something to do with math. There is no way to know unless the OP explicitly states it. Either way I'm sure they can figure out what they want on their own for something this trivial.

Actually it looks like you viewed their old posts. http://www.cplusplus.com/forum/beginner/142546/ So you can probably ignore what I mentioned.
Last edited on
Thank you for the reply both.
To settle the argument lol
Based on what im trying to code.. the 4 digit number can't start with a 0
Based on what im trying to code.. the 4 digit number can't start with a 0
if that is the case that would mean you have a range of [1000, 9999] which my solution would handle. Basically something like if(input < 1000 || input > 9999) //out of range

or did I misunderstand and you mean it won't allow it as in you are using int instead of string?
Last edited on
no you got it right...
but it isn't working.
can you check what im doing wrong please?

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
#include <iostream>
using namespace std;
void main()
{  
		// Defining Variables
 
        int userID = 0;
        int password = 0; 
        int newpassword;
 
		// Initialization
		cout << "Please enter your 6-digit user id: "; 
        cin >> userID;
		cout << "Please enter your 4-digit password: ";
		cin >> password;
		// Loop
		for ( int c=3; c>0; c--)
		// Start if
		// Process
		if (userID == 286957 && password == 2468)
			{
				// Print
				cout << "Logged in successfully. You should change your password at the first login. \nThe new password should be a 4-digit number and different from \nthe current password. ";
				cout << "Please enter your new 4-digit password: ";
				cin >> newpassword;
				if ( newpassword != password && newpassword < 1000 || newpassword > 9999 ) 
				cout << "Your password has been changed \n";
				

				else 
				{
					cout << "Invalid";
					exit (1);
				}
				break;
				
			}
			

			else	
			{
				cout << "Invalid userID/password. You can try "<< c << " more time/s \n";
				cout << "Please enter your 6-digit user id: "; 
				cin >> userID;
				cout << " Please enter your 4-digit password: ";
				cin >> password;

			}
			//End if
		
		
		system ("pause");
}
It should be if ( newpassword != password && (newpassword < 1000 || newpassword > 9999) ) Though technically I did it for the failed case. Yours is for the good case so it should be > 999 and less than 10000 so I would do if(newpassword != password && newpassword > 999 && newpassword < 10000)

By the way what is up with this for ( int c=3; c>0; c--) looks so weird :P the normal way is for(int i = 0; i < 3; ++i) oh I see actually it is so you get proper numbers on line 42. Though I probably would just do 3 - i for the amount of times left. Or something like tries - i
if ( newpassword != password && newpassword < 1000 || newpassword > 9999 )
of line 26 is not proper.

Your program will accept 1000000 coz newpasword > 9999 will evaluate to true.
Also 0 will be accepted coz newpasword < 1000 will be true.

I think you meant to say

if(newpassword != passord && (newpasword > 999 && newpassword < 10000))
Topic archived. No new replies allowed.