C中的密码确认

Password Comfirmation in C

我正在尝试制作一个密码代码,该代码应该要求用户输入密码,然后要求用户重新输入此密码以进行确认。该程序应该比较两个字符串,如果所有字符都匹配则继续,但我的似乎不起作用...有什么建议吗?

#include <stdio.h>
#include <string.h>

int main() 
 {
  /*variables*/

  char administration_password[255]="";
  char administration_validation[255]="";

  /*end of variables*/

  printf("\n");
  printf ("       *******************************************\n");
  printf ("                Data Management Software\n");
  printf ("       *******************************************\n\n");


  printf ("*****\n");
  printf ("SETUP\n");
  printf ("*****\n");
  printf ("\n");
  printf ("\n");

  printf("At least one administrator is required to use this sorftware.\n\n");
  printf("Please set the administration password: ");


  scanf ("%s", administration_password);
  printf ("\n");
  printf("Re-enter the password to confirm: ");
  scanf ("%s", administration_validation);
  printf ("\n\n");


   while (administration_password != administration_validation)
    {
     printf ("\n");
     printf ("The two passwords to not match please try again.\n");
     printf ("\n");
     printf ("Please set the administration password: ");
     scanf ("%s", administration_password);
     printf ("\n");
     printf ("Re-enter the password to confirm: ");
     scanf ("%s", administration_validation);
     printf ("\n\n");
    }
   printf ("\n");
   printf ("\n");
   printf ("**************************************\n");
   printf ("The Password has successfully been set\n");
   printf ("**************************************\n");
   printf ("\n");
   printf ("\n");
  }

您需要使用 strcmp()(或变体)来比较 C

中的 "strings"

这不是比较字符串的内容

administration_password != administration_validation

你需要

strcmp(administration_password, administration_validation) != 0

在第一种情况下,您正在比较每个数组的第一个元素的地址,这显然是不同的。

提示:通过告诉它读取字符串的最大宽度应该是多少来安全地使用scanf(),这样

scanf ("%254s", administration_password);

因为administration_password是一个255长度的数组,所以254个字符+'[=18=]'.

这是更正后的代码

#include <stdio.h>
#include <string.h>

int main()
{
 /*variables*/

 char administration_password[255]="";
 char administration_validation[255]="";

 /*end of variables*/

 printf("\n");
 printf ("       *******************************************\n");
 printf ("                Data Management Software\n");
 printf ("       *******************************************\n\n"); 

 printf ("*****\n");
 printf ("SETUP\n");
 printf ("*****\n");
 printf ("\n");
 printf ("\n");

 printf("At least one administrator is required to use this sorftware.\n\n");
 printf("Please set the administration password: ");

 scanf ("%254s", administration_password);
 printf ("\n");
 printf("Re-enter the password to confirm: ");
 scanf ("%254s", administration_validation);
 printf ("\n\n");

 while (strcmp(administration_password, administration_validation) != 0)
    {
    printf ("\n");
    printf ("The two passwords to not match please try again.\n");
    printf ("\n");
    printf ("Please set the administration password: ");
    scanf ("%s", administration_password);
    printf ("\n");
    printf ("Re-enter the password to confirm: ");
    scanf ("%s", administration_validation);
    printf ("\n\n");
    }
  printf ("\n");
  printf ("\n");
  printf ("**************************************\n");
  printf ("The Password has successfully been set\n");
  printf ("**************************************\n");
  printf ("\n");
  printf ("\n");

  return 0;
}

您需要对字符串使用 strcmp()。这是 link 到 documentation

  char administration_password[255]="";
  char administration_validation[255]="";


注意 administration_passwordadministration_validation 是指针(它们保存内存地址)。所以当你比较它们时,你是在比较保存在它们中的地址。在几乎所有情况下,这些地址都是不同的,因此 while 语句中的条件始终为真。