-3

I've tried to play with little details, and yet, doesn't seem to work properly. And I don't know why!

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char adminName[20];
    char userName[20];
    adminName[20]= "Admin";

    printf("Please, enter your name:");
    scanf("%s", userName);

    if(userName == adminName) {
        printf("Welcome, Admin! \n");
    }

    printf("Wrong log-in details, %s. \n", userName);
    return 0;
}
YasirAnqa
  • 3
  • 1
  • Change this `if(userName == adminName)` **->** `if(strcmp(userName,adminName)==0)`. – ameyCU Nov 11 '15 at 06:01
  • Because you are comparing strings. That's not how comparing strings works. That's not how any of it works! – NSNoob Nov 11 '15 at 06:03

2 Answers2

0

In your question there are a lot of mistake. First of all

adminName[20] ="admin";

will throw an warning, and your admin array is not made "admin", while the 20th element which doesn't even exists, as u have max of adminName[19].

You can't directly copy a string to another array, use this for string copy

strcpy();

And for comparison, use

strcmp();

And better learn string.h header files. Those two are present in string.h header file

0

Please try this, it works as you wanted, read notes in the code, it is normal to make a few mistakes while learning.

#include <stdio.h>
#include <stdlib.h>

int main(  )
{
  char adminName[20];
  char userName[20];

  strcpy( adminName, "Admin" ); // use strcpy

  printf( "Please, enter your name:" );
  scanf( "%s", userName );

  if ( !strcmp( userName, adminName ) ) // use strcmp
  {
    printf( "Welcome, Admin! \n" );
  }
  else              // use else
    printf( "Wrong log-in details, %s. \n", userName );

  return 0;
}
BobRun
  • 756
  • 5
  • 12