-2

I'm trying to take inputs from user about their login credentials (username and password) and then checking whether the username and password are valid or not.

#include <stdio.h>

int main()
{
    printf("-----------------------------------");
    printf("-----------REGISTER----------------");
    char username[10], pwd[6];
    char check_usn[10], check_pwd[6];
    printf("\nEnter your username\n");
    scanf("%s", username);
    printf("Enter your password (length should be 6\n");
    scanf("%s", pwd);
    printf("-----------------------------------");
    printf("-------------LOG IN--------------");
    printf("\nEnter your username\n");
    scanf("%s", check_usn);
    printf("Enter your password (length should be 6\n");
    scanf("%s", check_pwd);
    int i = 0, key = 0;
    while(i<(sizeof(username)/sizeof(username[0])))
    {
        if(username[i] == check_usn)
        {
            key = i;
        }
        else
        {
            i++;
        }
    }
    if(pwd[key] == check_pwd)
    {
        printf("Valid password");
    }
    else
    {
        printf("Wrong password");
    }
    return 0;
}

Warning : comparison between pointer and integer And also I'm getting the wrong output.

I tried this now and now I'm getting segmentation fault.

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

int main()
{
    printf("-----------------------------------");
    printf("-----------REGISTER----------------");
    char username[10], pwd[6];
    char check_usn[10], check_pwd[6];
    printf("\nEnter your username\n");
    scanf("%s", username);
    printf("Enter your password (length should be 6\n");
    scanf("%s", pwd);
    printf("-----------------------------------");
    printf("-------------LOG IN--------------");
    printf("\nEnter your username\n");
    scanf("%s", check_usn);
    printf("Enter your password (length should be 6\n");
    scanf("%s", check_pwd);
    int i = 0, key = 0;
    while(i<(sizeof(username)/sizeof(username[0])))
    {
        if(strcmp(username[i],check_usn) == 0)
        {
            key = i;
        }
        else
        {
            i++;
        }
    }
    if(strcmp(pwd[key],check_pwd) == 0)
    {
        printf("Valid password");
    }
    else
    {
        printf("Wrong password");
    }
    return 0;
}
  • Could you provide the entired warning? – RobertS supports Monica Cellio Nov 01 '19 at 08:13
  • 1
    [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) - read it. This code has multiple places where you're comparing `char` against `char*`, which is clearly wrong. – WhozCraig Nov 01 '19 at 08:14
  • `username[i] == check_usn`, here `username[i]` is a char while `check_usn` is character array. Same for `if(pwd[key] == check_pwd)` – kuro Nov 01 '19 at 08:15
  • 4
    `scanf("%s", username);` is very dangerous. If you input a name longer than the provided array, -1 for `\n`, you run into buffer overflow. Instead use f.e.`scanf("%9s", username);`. Same goes for the others. – RobertS supports Monica Cellio Nov 01 '19 at 08:15
  • 2
    You write `"Enter your password (length should be 6\n"`, but `pwd` is defined as `char pwd[6]`. That is not enough space for 6 letters and nul termination character. Please study more on how strings work in C. Strings must always have room for nul termination. – user694733 Nov 01 '19 at 08:23
  • @user694733 I did not seen that `printf()` statement tough, which is completely malicious under that circumstances. Yes, If the user is giving in now 6 characters, as the programmer instructs to do, the user already has buffer overflow. – RobertS supports Monica Cellio Nov 01 '19 at 08:36

2 Answers2

1

Like what @WhozCraig and @Kuro said, you'r comparing a character against a character array. Seems like your programming logic is stuck in between. One approach to comparing strings is by primarily using C functions and comparing the functions returned result against your desired functionality, for example:

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

int main()
{

    char username[10], pwd[6];
    char check_usn[10], check_pwd[6];

    printf("-----------------------------------\n");
    printf("-----------REGISTER----------------\n");
    printf("\nEnter your username\n");
    scanf("%s", username);
    printf("Enter your password (length should be 6\n");
    scanf("%s", pwd);

    printf("-----------------------------------");
    printf("-------------LOG IN--------------");
    printf("\nEnter your username\n");
    scanf("%s", check_usn);
    printf("Enter your password (length should be 6\n");
    scanf("%s", check_pwd);

    if( (strncmp( username, check_usn, strlen(username))) == 0 ) // strncmp returns 0 if strings match
    {
      if( (strncmp( pwd, check_pwd, strlen(pwd))) == 0 ) {
        printf("login success\n");
      }
      else {
        printf("invalid password\n");
      }
    }
    else {
      if( (strncmp( pwd, check_pwd, strlen(pwd))) != 0 ) { // if strncmp != 0 the strings don't match
        printf("invalid username & invalid password\n");
      }
      else {
        printf("invalid username\n");
      }
    }

    return 0;
}

Another approach to comparing strings is to compare the strings character for character, where you find yourself parsing the string. For example, you could use a function(or you can insert it directly into your main function) that compares the string and then compare the functions returned result with your desired functionality. the function would look something like this:

bool equalStrings (const char  s1[], const char  s2[])
{
    int  i = 0;
    bool areEqual;

    while ( s1[i] == s2 [i]  &&
                 s1[i] != '\0' &&  s2[i] != '\0' )
        ++i;

    if ( s1[i] == '\0'  &&  s2[i] == '\0' )
       areEqual = true;
    else
       areEqual = false;

    return areEqual;
}

Excerpt From: Stephen G. Kochan. “Programming in C (4th Edition) (Developer's Library)”. Apple Books. 
V. Doe
  • 56
  • 7
0

I can deduce that the thing that you're trying to do is simple: get 2 usernames and 2 passwords from the user (stdin) and then compare them.

The first error that you must fix is the fact that every string in C ends with NUL character \0, known also as terminator. You should take into account this fact when fetching strings from user by using this formula: scanf("%Ns", str), when N is the length of str array - 1 (for NUL character).

The second error is the fact that string comparisons are done using strcmp() function from <string.h> rather than the equals operator (which compares the address).

The warning came from the fact that comparison username[i] == check_usn compares single character with a pointer. In reality, username[i] is a single character and check_usn is the char * - string.

I can suggest how I would write this program:

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

int main() {
    printf("-----------------------------------");
    printf("-----------REGISTER----------------");
    char username[11], pwd[7];
    char check_usn[11], check_pwd[7];

    printf("\nEnter your username\n");
    scanf("%10s", username);

    printf("Enter your password (length should be 6\n");
    scanf("%6s", pwd);

    printf("-----------------------------------");
    printf("-------------LOG IN--------------");
    printf("\nEnter your username\n");
    scanf("%10s", check_usn);
    printf("Enter your password (length should be 6\n");
    scanf("%6s", check_pwd);

    if(strcmp(username, check_usn) == 0 && strcmp(pwd, check_pwd) == 0)
    {
        printf("Valid password");
    }
    else
    {
        printf("Wrong password");
    }
    return 0;
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103