khi em chạy chương trình thì nó cứ hiện ra lỗi này@@
Lỗi Access violation writing khi code trên visual studio 2013
Bạn thử đưa code lên xem, mình đoán là bạ truy xuất vào con trỏ có giá trị NULL thì fai
1 Like
#include<stdio.h>
#include<conio.h>
typedef struct sinhvien
{
char ma[11];
char ten[51];
char gioitinh;
float dtb;
}sv;
typedef struct node
{
sv info;
struct node*next;
}node;
void khoitao(node*&phead)
{
phead == NULL;
}
int rong(node*phead)
{
if(phead == NULL);
return 1;
return 0;
}
node*taonut(sv &x)
{
node*p = new node;
p->info = x;
p->next = NULL;
return p;
}
void themdau(node*&phead,sv x)
{
node*p = taonut(x);
if (rong(phead)==1)
phead = p;
else
p->next = phead;
phead = p;
}
void nhap1sv(sv &x)
{
printf("Nhap mssv:");
scanf("%s", &x.ma);
printf("\nNhap ten sinh vien:");
fflush(stdin);
gets(x.ten);
do{
printf("Nhap gioi tinh F or M:");
scanf("%c", &x.gioitinh);
if (x.gioitinh != 'F'&&x.gioitinh != 'M')
printf("Nhap sai,Nhap lai");
} while (x.gioitinh != 'F'&&x.gioitinh != 'M');
printf("Nhap diem trung binh:");
float t;
scanf("%f", &t);
x.dtb = t;
}
void xuat1sv(sv x)
{
if (x.gioitinh = 'F')
printf("\tNu");
if (x.gioitinh = 'M')
printf("\tNam");
printf("%11s%51s%s%f", x.ma, x.ten, x.gioitinh, x.dtb);
}
void nhapds(node*&phead)
{
int n;
sv x;
do
{
printf("Nhap so luong phan tu:");
scanf("%d", &n);
if (n <= 0)
printf("Nhap sai,Nhap lai");
} while (n <= 0);
for (int i = 1; i < n; i++)
printf("Sinh vien thu %d:", i);
nhap1sv(x);
themdau(phead, x);
}
void xuatds(node*phead)
{
for (node*p = phead; p != NULL; p = p->next)
xuat1sv(p->info);
}
void main()
{
sv x;
node*phead;
nhapds(phead);
xuatds(phead);
getch();
}
nè bạn…chỗ nhập giới tính k biết mình code đúng không
Yêu cầu là chỉ nhập F or M
F xuất ra là Nữ
M xuất ra là nam
Do GioiTinh là kiểu Char nên trong hàm xuat1SV
bạn thay câu lệnh printf
thành như sau :
printf(" %s %s %c %f",x.ma, x.ten,x.gioitinh,x.dtb);
Ngoài ra có mấy chỗ logic bạn chưa đúng ví dụ như chỗ này :
1 Like
Ngoài lỗi bạn Tuấn đã nói, thì bạn sửa thêm các lỗi sau:
- Phép so sánh trong C dùng
==
chứ ko phải=
void xuat1sv(sv x)
{
if (x.gioitinh = 'F') // dùng ==
printf("\tNu");
if (x.gioitinh = 'M') // dùng ==
printf("\tNam");
- Cậu lệnh này
fflush(stdin);
thực sự không hoạt động, bạn nên dùnggetchar()
để thay thế.
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct sinhvien
{
char ma[11];
char ten[51];
char gioitinh;
float dtb;
}sv;
typedef struct node
{
sv info;
struct node*next;
}node;
node* taonut(sv &x)
{
node*p = new node;
p->info = x;
p->next = NULL;
return p;
}
void khoitao(node*&phead)
{
phead == NULL;
}
int rong(node*phead)
{
if (phead == NULL)
return 1;
return 0;
}
void themcuoi(node*&phead,sv x)
{
node*p = taonut(x);
if (rong(phead))
phead = p;
else
for (node*tam = phead; tam->next != NULL; tam = tam->next)
tam->next = p;
}
void nhap1sv(sv &x)
{
printf("\nNhap ma sinh vien:");
scanf("%s", &x.ma);
printf("\nNhap ho ten sinh vien:");
fflush(stdin);
gets(x.ten);
do
{
printf("\nNhap gioi tinh F or M:");
scanf("%c", &x.gioitinh);
if (x.gioitinh != 'F'&&x.gioitinh != 'M')
printf("Nhap sai,Nhap lai");
} while (x.gioitinh != 'F'&&x.gioitinh != 'M');
printf("Nhap diem trung binh:");
float t;
scanf("%f", &t);
x.dtb = t;
}
void xuat1sv(sv x)
{
if (x.gioitinh == 'F')
printf("\tNu");
if (x.gioitinh == 'M')
printf("\tnam");
printf("%s%s%c%f", x.ma, x.ten, x.gioitinh, x.dtb);
}
void nhapds(node*&phead)
{
int n;
sv x;
do
{
printf("\nNhap so luong ainh vien:");
scanf("%d", &n);
if (n <= 0)
printf("Nhap sai, Nhap lai");
} while (n <= 0);
for (int i = 0; i < n; i++)
{
printf("\nNhap sinh vien thu %d:", i + 1);
nhap1sv(x);
themcuoi(phead, x);
}
}
void xuatds(node*phead)
{
for (node*p = phead; p != NULL; p = p->next)
xuat1sv(p->info);
}
int main()
{
node*phead;
nhapds(phead);
xuatds(phead);
getch();
return 0;
}
vẫn còn lỗi bạn ơi :(((((
…
Bạn chưa sửa theo ý thứ 2 mà mình đã nói
1 Like
làm được rrrr…cám ơn mng ^^