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.
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.
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?
#include <iostream>
usingnamespace 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