Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
因此,我尝试使用c创建简单的注册和登录_C - Fatal编程技术网

因此,我尝试使用c创建简单的注册和登录

因此,我尝试使用c创建简单的注册和登录,c,C,所以我尝试用c创建注册和登录,我发现了这个错误,我使用2函数,正如你所看到的,在第一个函数中,你必须插入用户名和密码,而在第二个函数中,你必须使用自己的用户名和密码变量登录,之后,程序将检测第二个函数中的变量是否与第一个函数中的变量不同,如果第二个函数中的变量与第一个函数中的变量不同,程序将在声明密码或用户名错误时进行循环 但是当我运行程序时,它会自动地说我的密码和用户名是错误的,那么我需要做什么呢 main(){char username[512]; int password; printf

所以我尝试用c创建注册和登录,我发现了这个错误,我使用2函数,正如你所看到的,在第一个函数中,你必须插入用户名和密码,而在第二个函数中,你必须使用自己的用户名和密码变量登录,之后,程序将检测第二个函数中的变量是否与第一个函数中的变量不同,如果第二个函数中的变量与第一个函数中的变量不同,程序将在声明密码或用户名错误时进行循环

但是当我运行程序时,它会自动地说我的密码和用户名是错误的,那么我需要做什么呢

main(){char username[512];
int password;
printf("Pendaftaran\nMasukan Username = ");
scanf("%s", &username);
printf("Masukan password = ");
scanf("%s", &password);
printf("Registrasi selesai...\n");
login(username, password);}
void login(char username[], int password{
char un[512];
int pw;
printf("Selamat datang di RS XYZ\nSilahkan masukan username = ");
scanf("%s", &un);
if(un[512]=username){
    printf("Masukan password = ");
    scanf("%s", &pw);
    if(pw!=password){
        printf("Password salah\n");
        login(username, password);
    }
    else{
        menu();
    }
}
else{
    printf("Username tidak terdaftar\n");
    login(username, password);
}

由于您使用char类型的用户名和密码,因此%s无法在scanf函数中读取字符串,请改用%c读取字符

如果用户名和密码包含多个字符,请使用字符数组。例如,char用户名[20]。现在可以在scanf函数中使用参数%s填充它们。要比较它们,请使用strcmp函数,记住必须包含string.h

在您的情况下,一个可能的解决方案是:


欢迎访问stackoverflow.com。请花些时间阅读,特别是命名和。也请和。最后,请学习如何创建一个。另外,请阅读,并了解您的问题可能被否决的一些原因。最后,请。请在你的问题中直接发布你的代码。我忘记了这一点,我用数组修复了它,正如你所说的,但如果你只使用字符fe,它仍然不起作用。char username您必须在scanf中使用%c,而不是%s//这样才行。如果您有字符数组,就不能像上面那样比较它们。你必须使用strcmp函数。我现在使用了数组,我也尝试过使用strcmp,但它仍然有这样一个bug。你能发布你的代码吗?编辑了我的答案@Jabberwocky
main(){
char username[20];
int password;
printf("Enter your username: ");
scanf("%s", &username);
printf("Enter your password: ");
scanf("%d", &password); //as you read int you have to use %d
login(username, password);
}
void login(char username[20], int password
{
char un[20];
int pw;
printf("Enter your username: ");
scanf("%s", &un);
if((strcmp(un,username)==0)){
    printf("Enter pass: ");
    scanf("%d", &pw);
    if(pw!=password){
        printf("Password wrong\n");
        login(username, password);
    }
    else{
        menu();
    }
}
else{
    printf("Username wrong\n");
    login(username, password);
}