แก้ยังไงดีครับผม โปรแกรมเอาไว้แอดสินค้า สามารถแก้ไขสินค้า ลบสินค้า และตรวจรายชื่

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ArTeeNoi
PHP Newbie
PHP Newbie
โพสต์: 2
ลงทะเบียนเมื่อ: 12/03/2011 12:08 pm

แก้ยังไงดีครับผม โปรแกรมเอาไว้แอดสินค้า สามารถแก้ไขสินค้า ลบสินค้า และตรวจรายชื่

โพสต์ที่ยังไม่ได้อ่าน โดย ArTeeNoi »

โค้ด: เลือกทั้งหมด

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

/*function prototype*/
void addProduct(void);
void delProduct(void);
void changeProduct(void);
void listallProduct(void);
void dispDtl(struct commodity r);
int found(struct commodity r, FILE *fptCmd, int);

struct commodity
{
int num,ins;
char name[15];
float price;
};

char doMore; // ask for do more

/******************************
* Main programe
******************************/
void main()
{
int choice = 1;

while (choice != 0)
{
clrscr();
printf("\t<<<<< MAIN MENU >>>>>\n\n"
"1. Add new Product\n"
"2. Delete a Product\n"
"3. Change Product\n"
"4. List all Product\n"
"5. Exit programe\n\n"
" Select Choice : ");
choice = getch();
switch(choice){
case '1':
addProduct();
break;
case '2':
delProduct();
break;
case '3':
changeProduct();
break;
case '4':
listallProduct();
break;
case '5':
exit(0);
default:
printf("\n\n     Error choice!!  Please any key to select again");
getche();
break;
} //end switch
} //end while
}//end main

/*******************************************************************************
* Add New Product
*******************************************************************************/
void addProduct(void)
{

FILE *fptCmd;
fptCmd = fopen("C:/mySource/dat/COMMODITY.DAT", "a+");
struct commodity c = { 0, 0, "", 0 };
char loop = 'Y';

/*error for open file*/
if(fptCmd == NULL)
{ printf("Error....cannot open file!\n\n");
exit(0); }

while(loop == 'Y')
{
printf("\n\n** Add new commodity **\n");
printf("\nEnter No. => ");
scanf("%d", &c.num);

if (found(c, fptCmd, c.num) != 0)
{
printf("Name => ");
fflush(stdin);
gets(c.name);
printf("Insurance => ");
scanf("%d", &c.ins);
printf("Price => ");
scanf("%f", &c.price);

fprintf(fptCmd, "%d %s %d %f\n",c.num, c.name, c.ins, c.price);
fflush(fptCmd); } //commit to write file

else
printf("Duplicate product....\n");

printf("Add product again[y/n] : ");
loop = toupper(getche());
}
fclose(fptCmd);
}

/*******************************************************************************
* Delete a product
*******************************************************************************/
void delProduct(void)
{
FILE *fptCmd;
fptCmd = fopen("C:/mySource/dat/COMMODITY.DAT", "r");
struct commodity c = { 0, 0, "", 0 };

FILE *fptNew;
fptNew = fopen("C:/mySource/dat/New.DAT", "w");

char confirm;
int num;

/*error for open file*/
if(fptCmd == NULL)
{ printf("Error....cannot open file!\n\n");
exit(0); }
printf("\n\n** Delete a product **\n");
printf("\nEnter No. => ");
scanf("%d", &num);

if(found(c, fptCmd, num) == 0) // search a key
{
printf("\nPlease confirm to delete [y/n] : ");
confirm = getch();
if(confirm == 'y'){
rewind(fptCmd);
while(fscanf(fptCmd, "%d %s %d %f",
&c.num, c.name, &c.ins, &c.price) != EOF)
{
if(c.num != num)
fprintf(fptNew,"%d %s %d %f\n",
c.num, c.name, c.ins, c.price);
} //end while

fclose(fptCmd);
fclose(fptNew);
remove("C:/mySource/dat/COMMODITY.DAT");
rename("C:/mySource/dat/NEW.DAT",
"C:/mySource/dat/COMMODITY.DAT");
printf("\nProduct has been deleted\n"); }

}
else
printf("\nNot found...");

fclose(fptCmd);
fclose(fptNew);
getch();
}

/********************************************************************************
* Change commodity data
*******************************************************************************/
void changeProduct(void)
{
FILE *fptCmd;
fptCmd = fopen("C:/mySource/dat/COMMODITY.DAT","r+");
struct commodity c ={0, 0, "", 0};

FILE *fptNew;
fptNew = fopen("C:/mySource/dat/NEW.DAT", "w");

int num;
/*error for open file*/
if(fptCmd == NULL)
{ printf("Error...cannot open file!\n\n");
exit(0); }

printf("\n\n** Change data **\n");
printf("\nEnter No. => ");
scanf("%d", &num);

if(found(c, fptCmd, num) == 0)
{
printf("Name => ");
fflush(stdin);
gets(c.name);
printf("Insurance => ");
scanf("%d", &c.ins);
printf("Price => ");
scanf("%f", &c.price);

c.num = num;
fprintf(fptNew, "%d %s %d %f",
c.num, c.name, c.ins, c.price);

rewind(fptCmd);
while(fscanf(fptCmd, "%d %s %d %f",
&c.num, c.name, &c.ins, &c.price) != EOF)
{
if(c.num != num)
fprintf(fptNew, "%d %s %d %f\n",
c.num, c.name, c.ins, c.price);
} //end while
fclose(fptCmd);
fclose(fptNew);
remove("C:/mySource/dat/COMMODITY.DAT");
rename("C:/mySource/dat/NEW.DAT",
"C:/mySource/dat/COMMODITY.DAT");
printf("\nProduct has been change\n"); }
else
printf("\nNot found...\n");

fclose(fptCmd);
fclose(fptNew);
getch();
}

/*******************************************************************************
* List all commodity record
*******************************************************************************/
void listallProduct(void)
{
FILE *fptCmd;
fptCmd = fopen("C:/mySource/dat/COMMODITY.DAT", "r");
struct commodity c = {0, 0, "", 0};

/*error for open file*/
if(fptCmd == NULL)
{ printf("Error....cannot open file!\n\n");
exit(0); }

printf("\n\nNo.    Name            Insurance          Price\n");
printf("---------------------------------------------------\n");

rewind(fptCmd);

while(fscanf(fptCmd, "%d %s %d %f\n",
&c.num, c.name, &c.ins, &c.price) != EOF)
{ printf("%2d     %-10s        %2dY           %8.2f\n",
c.num, c.name, c.ins, c.price); }
printf ("---------------------------------------------------\n");
fclose(fptCmd);
printf("\nPress any key to return to menu");
getch();
}

/*******************************************************************************
* Display data
*******************************************************************************/
void dispDtl(struct commodity r)
{
printf("\n===============================================\n");
printf("No.    Name            Insurance          Price\n");
printf("===============================================\n");

printf("%2d     %-10s        %2dY           %8.2f\n",
r.num, r.name, r.ins, r.price);
printf("\n-----------------------------------------------\n");
}

/*******************************************************************************
* Searching
*******************************************************************************/
int found(struct commodity r, FILE *fptCmd, int num)
{
rewind(fptCmd);
while(fscanf(fptCmd, "%d %s %d %f",
&r.num, r.name, &r.ins, &r.price) != EOF)
{
if(r.num == num)
{ //search found
dispDtl(r);
return 0;
}
} //end while
return -1; //notfound
}
เป็นโปรแกรมเอาไว้แอดสินค้า สามารถแก้ไขสินค้า ลบสินค้า และตรวจรายชื่อสิ้นค้าทั้งหมดได้

อยากให้ช่วย(แก้)ดูน่ะครับ
ในเมนูย่อย addProduct เวลาเข้าเมนูนี้ก็ต้องคอยใส่ลำดับทุกครั้ง(Enter No.) ผมอยากแก้ให้เป็น Auto อ่าครับ
เช่น ครั้งแรกที่เข้าใช้เมนูย่อยนี้ โปรแกรมก็จะเริ่มที่ No.1 ให้เลย เราก็แค่ใส่ ชื่อ ประกัน ราคา
สมมุติผมแอดไปสัก5อย่าง พอเวลาเข้ามาเมนูนี้อีกที่ ให้มันเริ่มที่ No.6 เลยอ่าครับ
ไม่ทราบว่าผมต้องแก้หรือเพิ่มอะไรตรงไหนบ้างครับ

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 34