PDA

View Full Version : code quản lý cửa hàng tạp hóa đoạn addnhomsanpham bi báo lỗi[lỗi gì]



oversoul_0612
08-10-2011, 08:26 PM
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "malloc.h"
#include "string.h"


typedef struct
{
char MaNhom;
char TenNhom;
}nhomsanpham;




int main()
{
int menuchinh(void);
int menu1(void);
int menu2(void);
void addnhomsanpham(nhomsanpham nsp[100], int *n);
void shownhomsanpham(nhomsanpham nsp[100], int *n);
void delnhomsanpham(nhomsanpham nsp[100], int *n);
void updatenhomsanpham(nhomsanpham nsp[100], int *n);
void writetofile(nhomsanpham nsp[100],int *n);
void readfromfile(nhomsanpham nsp[100],int *n);


int choice;
int choice1;
int choice2;
nhomsanpham *nsp;
int n=0;

nsp=(nhomsanpham*)malloc(100*sizeof(nhomsanpham));

tt:
menuchinh();
printf("\n\n Nhap vao lua chon: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
system("cls");
menu1();
printf("\n\n Nhap vao lua chon: ");
scanf("%d", choice1);
switch(choice1)
{
case 1:
readfromfile(nsp,&n);
addnhomsanpham(nsp,&n);
writetofile(nsp,&n);
menu1();
break;
case 2:
readfromfile(nsp,&n);
if(n!=0)
{
updatenhomsanpham(nsp,&n);
writetofile(nsp,&n);
}
else
{
printf("\n Khong co nhom san pham nay!");
printf("\n\n khong the thuc hien chuc nang!");
}
menu1();
break;
case 3:
readfromfile(nsp,&n);
if(n!=0)
{
delnhomsanpham(nsp,&n);
writetofile(nsp,&n);
}
else
{
printf("\n Khong co nhom san pham nay!");
printf("\n\n khong the thuc hien chuc nang!");
}
menu1();
break;
case 4:
readfromfile(nsp,&n);
if(n==0)
{
printf("\n Khong co nhom san pham nay!");
printf("\n\n khong the thuc hien chuc nang!");
}
menu1();
break;
case 0:
goto tt;
break;
}

break;
case 2:
menu2();
printf("\n\n Nhap vao lua chon: ");
scanf("%d", choice2);
switch(choice2)
{
case 1:
menu2();
break;
case 2:
menu2();
break;
case 3:
break;
case 4:
break;
case 0:
goto tt;
break;
}


break;
case 3:
printf("tim kiem san pham");
break;
case 4:
exit(0);
break;
}
}


int menuchinh(void)
{
printf("| %-50s |\n", " MENU ");
printf("| %-50s |\n", "1. Quan ly nhom san pham");
printf("| %-50s |\n", "2. Quan ly san pham");
printf("| %-50s |\n", "3. Tim kiem");
printf("| %-50s |\n", "4. thoat");
}


int menu1(void)
{
printf("| %-50s |\n", " Quan ly Nhom san pham ");
printf("| %-50s |\n", "1.1 Them moi");
printf("| %-50s |\n", "1.2 Sua");
printf("| %-50s |\n", "1.3 Xoa");
printf("| %-50s |\n", "1.4 Liet ke danh sach");
printf("| %-50s |\n", "1.0 Tro ve menu chinh");
}


int menu2()
{
printf("| %-50s |\n", " Quan ly San pham ");
printf("| %-50s |\n", "1.1 Them moi");
printf("| %-50s |\n", "1.2 Sua");
printf("| %-50s |\n", "1.3 Xoa");
printf("| %-50s |\n", "1.4 Liet ke danh sach");
printf("| %-50s |\n", "1.0 Tro ve menu chinh");
}


void addnhomsanpham(nhomsanpham nsp[100],int *n)
{
nhomsanpham newnsp;
int i;
char tt;

printf("\n\n Them 1 nhom san pham moi vao danh sach\n");
do
{
repeat:
i=0;
fflush(stdin);
printf("\n Nhap ma nhom: ");
scanf("%s", newnsp.MaNhom);
while(i<*n)
{
if(strcmp(newnsp.MaNhom,nsp[i].MaNhom)==0)
{
printf("Ma nhom da ton tai!\n");
goto repeat;
}
i++;
}
fflush(stdin);
printf("\n Nhap ten Nhom: ");
scanf("%[^/n]", newnsp.TenNhom);
nsp[*n]=newnsp;
printf("\n Da them thanh cong!\n");
(*n)++;
fflush(stdin);
printf("\n Ban muon tiep tuc?(Y/N): ");
scanf("%c",&tt);
}while(tt=='y'||tt=='Y');
}


void shownhomsanpham(nhomsanpham nsp[100],int *n)
{
int i;
if(*n!=0)
{
printf("\n |==============================================|");
printf("\n | DANH SACH NHOM SAN PHAM |");

printf("\n |==============================================|");
printf("\n | Ma Nhom | Ten Nhom |");

for(i=0;i<*n;i++)
{
printf("\n |---------------------------------------------|");
printf("\n |%-13s|%-32s|", nsp[i].MaNhom, nsp[i].TenNhom);
}
printf("\n |==============================================|");
}
}


void updatenhomsanpham(nhomsanpham nsp[100],int *n)
{
nhomsanpham newnsp;
int i;
int vt=-1;
char update;
char tt;
do
{
printf("\n Nhap ma nhom can sua: ");
scanf("%s", newnsp.MaNhom);
for(i=0;i
{
if(strcmp(nsp[i].MaNhom,newnsp.MaNhom)==0)
{
vt=i;
break;
}
}
if(vt>=0 && vt)
{
fflush(stdin);
printf("\n Nhap ten nhom: ");
gets(newnsp.TenNhom);
fflush(stdin);
printf("\n Ban co muon cap nhat thong tin khong?(Y/N)");
scanf("%c", &update);
if(update=='y' || update=='Y')
{
if(strcmp(newnsp.TenNhom,"")!=0)
{
strcpy(nsp[vt].TenNhom,newnsp.TenNhom);
}
printf("\n Thong tin da duoc cap nhat!\n");
}
}
else
{
printf("\n Ma nhom khong ton tai!\n");
}
fflush(stdin);
printf("\n Ban co muon tiep tuc khong?(Y/N)");
scanf("%c",&tt);
}while(tt=='y' || tt=='Y');
}
void delnhomsanpham(nhomsanpham nsp[100], int*n)
{
char id[10];
int i;
int j;
int vt=-1;
char del;
char tt;
do
{
printf("\n Nhap ma nhom san pham can xoa:");
scanf("%s", id);
for(i=0;i
{
if(strcmp(nsp[i].MaNhom, id)==0)
{
vt=i;
break;
}
}
if(vt>=0 && vt
{
fflush(stdin);
printf("\n Ban co muon xoa khong?(Y/N)");
scanf("%c", &del);
if(del=='y' || del=='Y')
{
for(j=vt;j
{
nsp[j]=nsp[j+1];
}
(*n)--;
printf("\n Da xoa nhom san pham khoi danh sach!\n");
}
}
else
{
printf("\n Ma nhom san pham khong ton tai!\n");
}
fflush(stdin);
printf("\n Ban co muon tiep tuc khong?(Y/N)");
scanf("%c", &tt);
}while(tt=='y' || tt=='Y');
}


void writetofile(nhomsanpham nsp[100],int *n)
{
FILE *file;
int i;

if((file=fopen("nhomsanpham.dat", "wb"))==NULL)
{
printf("\n File chua duoc tao!\n");
return;
}
for(i=0;i
{
fwrite(nsp+i, sizeof(nhomsanpham),1, file);
}
fclose(file);
printf("\n DA LUU RA FILE THANH CONG!\n");
}


void readfromfile(nhomsanpham nsp[100],int *n)
{
FILE *file;
int i =0;

if((file=open("nhomsanpham.dat", "rb"))==NULL)
{
printf("\n File chua duoc tao!\n");
return;
}

while(!feof(file))
{
nhomsanpham s;
fread(&s, sizeof(nhomsanpham),1,file);
nsp[i]=s;
}
*n=i-1;
fclose(file);
shownhomsanpham(nsp,n);
}

Chỗ addnhomsapham đoạn if(strcmp(newnsp.MaNhom,nsp[i].MaNhom)==0) bị báo lỗi mọi người chỉ giúp e cách sửa với