Изучаю книгу Брайан Керниган, Деннис Ритчи: Язык Программирования C, прошёл первую главу и решил написать свою простую программу.
При вводе "admin", "pass" выводится "[*.*] Welcome user" вместо "[^.^] Hello dear Administrator".
Не могу понять почему данный код не рабоатет правильно?
Код
#include <stdio.h>
#define MAX_CHAR 50
int isAdmin(char login[], char password[]);
void getData(char login[], char password[]);
char adminL[MAX_CHAR] = "admin\0";
char adminP[MAX_CHAR] = "pass\0";
int main(void) {
char user_Login[MAX_CHAR], user_Password[MAX_CHAR];
int checking_For_Admin;
for (int i = 0; i < MAX_CHAR-1; ++i) {
user_Login[i] = 0;
user_Password[i] = 0;
}
printf("\t----------------------------\n");
printf("\t| Program v1.0 |\n");
printf("\t----------------------------\n");
getData(user_Login, user_Password);
checking_For_Admin = isAdmin(user_Login, user_Password);
if (checking_For_Admin == 1) {
printf("\n\n[^.^] Hello dear Administrator.\n");
}
else if (checking_For_Admin == 0) {
printf("\n\n[*.*] Welcome user.\n");
}
return 0;
}
int isAdmin(char login[], char password[]) {
int response, correct_Login, correct_Password;
extern char adminL[MAX_CHAR];
extern char adminP[MAX_CHAR];
// Login and password verification
correct_Login = correct_Password;
response = 0;
for (int i = 0; i < MAX_CHAR-1; ++i) {
//Login verification
//if (adminL[i] != '\0' && login[i] != '\0') {
if (adminL[i] != login[i])
correct_Login = 0;
//}
// Password verification
//if (adminP[i] != '\0' && password[i] != '\0') {
if (adminP[i] != password[i])
correct_Password = 0;
//}
}
if (correct_Login == 1 && correct_Password == 1)
response = 1;
return response;
}
void getData(char login[], char password[]) {
int c, j, z;
j = z = 0;
// Entering login
printf("\n[+] Enter login: ");
while ((c = getchar()) != EOF) {
if ((j == MAX_CHAR-1)) {
login[j] = '\0';
break;
}
else {
if (c == '\n') {
login[j] = '\0';
break;
}
else {
login[j] = c;
}
++j;
}
}
// Entering password
printf("[+] Enter password: ");
while ((c = getchar()) != EOF) {
if ((z == MAX_CHAR-1)) {
password[z] = '\0';
break;
}
else {
if (c == '\n') {
password[z] = '\0';
break;
}
else {
password[z] = c;
}
++z;
}
}
}