01/10/2018, 15:23
Lỗi khi nhập chuỗi
Chào mọi người, em đang viết hàm để đăng kí tài khoản cho người dùng bằng C. Khi người dùng nhập username và pass thì ghi vào file. nhưng khi nhập chuỗi thì lại báo cái lỗi này. Ai tìm lỗi giúp với ạ. Em cảm ơn.
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
struct user{
char username[50];
char pwd[50];
};
void sign_up(void)
{
user *p = NULL;
char pass[50];
FILE *f = fopen("user.bin", "wb");
if (f == NULL)
{
printf("Open error!");
return;
}
printf("Choose a username: ");
scanf("%s",&p->username);
printf("Choose a password: ");
scanf("%s",&p->pwd);
printf("Confirm password: ");
scanf("%s",&pass);
if (strcmp(p->pwd, pass) == 0)
{
printf("Sign up success!");
fwrite(p, sizeof(user), 1, f);
fclose(f);
}
else
{
printf("Password does not match!, please try again.");
sign_up();
}
}
int main()
{
sign_up();
_getch();
return 0;
}

Bài liên quan
Hi Võ Đức Dân.
Bạn xem lại tài liệu hàm scanf.
Chỗ scanf bạn viết rất máy móc.
Thực sự không nên dùng (f)scanf, mình thì mình sẽ dùng fgets(stdin, …).vì nó nhận size là một tham số.