-1

我需要将用户输入的内容与我之前使用 strncpy 存储的内容进行比较...我知道 strncpy 部分有效,当我将输入与 puser-> 用户名等进行比较时遇到问题...

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

除了提到的其他问题之外,您似乎正在使用一个变量 ( input) 同时保存用户名和密码;这似乎不太可能成功......

 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 回答