0

I'm trying to make a password code that is supposed to ask the user for a password and then ask the user to re-enter this password to confirm. The program is supposed to compare the two strings and if all the characters match then proceed but mine does not seem to work... any suggestions?

#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");
  }
otboss
  • 621
  • 1
  • 7
  • 16

3 Answers3

1

You need to use strcmp() (or a variant) to compare "strings" in C

John3136
  • 28,809
  • 4
  • 51
  • 69
1

This is not comparing the contents of the strings

administration_password != administration_validation

for that you need

strcmp(administration_password, administration_validation) != 0

in the first case, you are comparing the address of the first elements of each array which obviously are different.

Tip: use scanf() safely by telling it what the maximum width of the read string should be, this way

scanf ("%254s", administration_password);

since administration_password is a 255 length array, so 254 characters + the '\0'.

This is the corrected code

#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;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

You need to use strcmp() for strings. Here is the link to documentation !.

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


Note administration_password and administration_validation are pointers (they hold memory addresses). So when you compare them, you are comparing the addresses which is saved in them. In almost all cases, these addresses are different and therefore your condition in the while statement is always true.

Ehsan Ab
  • 679
  • 5
  • 16
  • The condition in the while statement is `(administration_password != administration_validation)`. This is always true because `administration_password` and `administration_validation` are most of the time contains different memory addresses. – Ehsan Ab Jan 28 '15 at 02:06
  • iharob, I don't get what you are trying say. Name of the array is a pointer in C. – Ehsan Ab Jan 28 '15 at 02:09