-1

i need to compare what the user inputs to what i have previously stored using strncpy...i know that the strncpy part works, i run into problems when i compare input to puser->Username etc...

int admin_signIn(struct profile *puser)
{
 int i=0;


 for(i=0;i<3;i++)
 {
  strncpy((puser+i)->UserName, "admin", strlen("admin")+1 );
  strncpy((puser+i)->Pwd, "password", strlen("password")+1 );

  printf("Enter admin user name:");
  fgets(input,10,stdin);
  rewind(stdin);   
  printf("Enter admin password:");
  fgets(input,10,stdin);

    //printf("the password is %s", puser->Pwd);    
   if(strcmp((puser+i)->UserName, input)==0 && strcmp((puser+i)->Pwd, input)==0)
    {
     printf("The Administrator username and password is incorrect, please try again\n");                                      

    }
   else
    {
     printf("the info is good\n");
    }

  }
   printf("max number of attepmpts exceded, goodbye!");   
}    
4

2 に答える 2

2

In addition to the other problems mentioned, it looks like you're using one variable (input) to hold the username and password simultaneously; that seems unlikely to be successful...

 printf("Enter admin user name:");
 fgets(input,10,stdin);
 rewind(stdin);   
 printf("Enter admin password:");
 fgets(input,10,stdin);
if(strcmp((puser+i)->UserName, input)==0 && strcmp((puser+i)->Pwd, input)==0)
于 2012-04-22T15:40:29.513 に答える
0

これも試してください:

それ以外の:

  strncpy((puser+i)->UserName, "admin", strlen("admin")+1 );
  strncpy((puser+i)->Pwd, "password", strlen("password")+1 );

できるよ:

strcpy((puser + i)->UserName, "admin");
strcpy((puser + i)->Pwd, "password");

  char password[10];
  char username[10];

  fgets(username,10,stdin);
  rewind(stdin);   
  printf("Enter admin password:");
  fgets(password,10,stdin);

その後:

   if(strcmp((puser+i)->UserName, username)==0 && strcmp((puser+i)->Pwd, password)==0)
   {
      /* correct username and password. Do something.. */
   }

ここで、ユーザーが入力したユーザー名とパスワードを比較しています。

于 2012-04-22T16:10:01.157 に答える