I'm writing a program where you allow someone to login. but i can't make it print successfully login when the id and password i input is equal to the preset value.
int main()
{
int id,pass,idC=0,passC=0,i,myid,mypass,tries=1;
myid = 10082612;
password = 123456;
printf("**LOG IN**\n\n");
printf("ID: ");
scanf("%d", &id);
printf("Password: ");
scanf("%d", &pass);
while(id != 0){
id /= 10;
idC++;
}
while(pass!= 0){
pass /= 10;
passC++;
}
if(idC==8 && passC ==6 || idC==7 && passC==6){
if(id==myid && pass==mypass){
printf("Successfully Login");
}
else{
printf("Unknown ID");
}
}
else if(idC!=8 && passC==6 || idC!=7 && passC==6){
printf("The Student ID should be in 7 or 8 digits, please try again");
}
else if(idC==8 && passC!=6 || idC==7 && passC!=6){
printf("Please enter a 6-digit pin");
}
}
It's because you're losing the original values of id
and pass
due to:
while(id != 0){
id /= 10; //THIS PART
idC++;
}
while(pass!= 0){
pass /= 10; // AND THIS PART
passC++;
}
To avoid this, you can :
int idCopy = id;
int passCopy = pass;
//and then do
while(idCopy != 0){
idCopy /= 10;
idC++;
}
while(passCopy!= 0){
passCopy /= 10;
passC++;
}
//and use the original values of id and pass like in your code .