Ned Ned - 1 year ago 104
C Question

How to check if input is equal to preset value in C programming?

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");
}


}


This is what I've wrote.

Answer Source

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 .
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download