Home > database >  Unexpected output when strings of same character length are compared
Unexpected output when strings of same character length are compared

Time:05-03

I aim to create a program that validates credentials (using nested ifs) by comparing input from the user to an already declared string. The program works as expected until I change the pass to "12345" or any 5-figure string. Then output that comes out for correct credentials is Incorrect user ID and password. I'm assuming this semantic error is occurring due to how strings are stored but since my basics are lacking I'm still unsure. This may be a redundant question but since this is my first genuine question on StackOverflow, I hope everyone will be lenient/tolerant. I'd be happy to provide clarification if my question was unclear. I also do apologize if I was unable to frame my title/question properly.

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

int main() {
    
    char name[10] = "k", pass[5] = "1234", input_id[10], input_pass[5];
    printf("Enter user ID: ");
    scanf("%s", input_id);
    printf("Enter user pass: ");
    scanf("%s", input_pass);
    
    if (strcmp(name, input_id) == 0) {
        if (strcmp(pass, input_pass) == 0) {
            printf("Accept");
        } else {
            printf("Incorrect User ID or Password");
        }
    } else {
        printf("Incorrect User ID or Password");
    }
    return 0;
}

CodePudding user response:

You need one more character for the string termination symbol. I adapted the code accordingly. Also note the use of more secure functions which limit input length. Also check if there is input at all.

The programmatic handling of user names and passwords is a very advanced topic. You can learn a lot from it. You have made the first steps, have fun on your journey. :-)

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

int main(int argc, char** argv) {
    
    char user_id[11] = "k", pass[6], input_user_id[20], input_pass[20];
    // next line is same as: pass[6] = "12345"
    pass[0] = '1'; pass[1] = '2'; pass[2] = '3'; pass[3] = '4'; pass[4] = '5'; pass[5] = '\0';
    int len_user_id = strlen(user_id);
    int len_pass = strlen(pass);

    printf("Enter user ID (max. 10 characters): ");
    char* user_id_value_string = fgets(input_user_id, sizeof input_user_id, stdin);
    if (user_id_value_string == NULL) {
        printf("Error: No user ID entered!\n");
        return 1;
    }
    unsigned long len_user_id_input = strlen(user_id_value_string);
    if (user_id_value_string[len_user_id_input - 1] == '\n') user_id_value_string[len_user_id_input - 1] = 0;
    printf("user ID: %s\n", user_id_value_string);
    printf("user ID length: %lu\n", strlen(user_id_value_string));

    printf("Enter user pass (max. 5 characters): ");
    char* pass_value_string = fgets(input_pass, sizeof input_pass, stdin);
    if (pass_value_string == NULL) {
        printf("Error: No password entered!\n");
        return 1;
    }
    unsigned long len_pass_input = strlen(pass_value_string);
    if(pass_value_string[len_pass_input - 1] == '\n') pass_value_string[len_pass_input - 1] = 0;
    printf("pass: %s\n", pass_value_string);
    printf("pass length: %lu\n", strlen(pass_value_string));
    
    if (strlen(user_id_value_string)  == len_user_id
        && strncmp(user_id, input_user_id, len_user_id) == 0 ) {
        if (strlen(pass_value_string) == len_pass
            && strncmp(pass, input_pass, len_pass) == 0) {
            printf("Accept\n");
        }
        else {
            printf("Incorrect User ID or Password\n");
        }
    }
    else{
        printf("Incorrect User ID or Password\n");
    }
    return 0;
}
$ gcc passcheck.c
$ ./a.out        
Enter user ID (max. 10 characters): k
user ID: k
user ID length: 1
Enter user pass (max. 5 characters): 12345
pass: 12345
pass length: 5
Accept
$ ./a.out        
Enter user ID (max. 10 characters): kevin
user ID: kevin
user ID length: 5
Enter user pass (max. 5 characters): 12345
pass: 12345
pass length: 5
Incorrect User ID or Password
$ 
  • Related