ช่วยแก้โค๊ดหน่อยค่ะc++

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

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

milk2009
PHP Newbie
PHP Newbie
Posts: 3
Joined: 07/04/2011 9:01 am

ช่วยแก้โค๊ดหน่อยค่ะc++

Post by milk2009 » 07/04/2011 11:04 am

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
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 book
{
char Bookcode[5],Bookname[20];
int Amount1, Amount2, Amount3, Amount4, Amount5;
float Price,Sum_Price,Total=0,reduce
};
char doMore;

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 : ");
hoice = 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

void addProduct(void)
{

FILE *fptCmd;
fptCmd = fopen("C:/c++/book.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 book **\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);
}

void delProduct(void)
{
FILE *fptCmd;
fptCmd = fopen("C:/c++/book.DAT", "r");
struct commodity c = { 0, 0, "", 0 };

FILE *fptNew;
fptNew = fopen("C:/c++/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:/c++/book.DAT");
rename("C:/c++/new.DAT",
"C:/c++/book.DAT");
printf("\nProduct has been deleted\n"); }

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

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

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

FILE *fptNew;
fptNew = fopen("C:/c++/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:/c++/book.DAT");
rename("C:/c++/new.DAT",
"C:/c++/book.DAT");
printf("\nProduct has been change\n"); }
else
printf("\nNot found...\n");

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

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

printf("\n....................................................\n");
printf("Bookcode : %s\nBookname : %s\nAmount : %d\nPrice : %2f\n\n"
,Bookcode,Bookname,Amount,Price);
if(Sum_Price>=1000)
{
reduce=Sum_Price/10;
Total=Sum_Price-reduce;
printf("Total_Price : %2f",Total);
}
else
{
printf("Total_Price is : %2f",Sum_Price);
}
getch();

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
}



***************************************************************มันerror26ตัวค่ะช่วยหน่อยนะค่ะ

Compiling WW.CPP:
Error WW.CPP 16: Cannot initialize a class member here
Error WW.CPP 34: Undefined symbol 'hoice' in function main()
Error WW.CPP 63: Too many initializers in function addProduct()
Error WW.CPP 75: 'num' is not a member of 'commodity' in function addProduct()
Error WW.CPP 77: 'num' is not a member of 'commodity' in function addProduct()
Error WW.CPP 81: 'name' is not a member of 'commodity' in function addProduct()
Error WW.CPP 83: 'ins' is not a member of 'commodity' in function addProduct()
Error WW.CPP 85: 'price' is not a member of 'commodity' in function addProduct()
Error WW.CPP 87: 'num' is not a member of 'commodity' in function addProduct()
Error WW.CPP 87: 'name' is not a member of 'commodity' in function addProduct()
Error WW.CPP 87: 'ins' is not a member of 'commodity' in function addProduct()
Error WW.CPP 87: 'price' is not a member of 'commodity' in function addProduct()
Error WW.CPP 103: Too many initializers in function delProduct()
Error WW.CPP 126: 'num' is not a member of 'commodity' in function delProduct()
Error WW.CPP 126: 'name' is not a member of 'commodity' in function delProduct()
Error WW.CPP 126: 'ins' is not a member of 'commodity' in function delProduct()
Error WW.CPP 126: 'price' is not a member of 'commodity' in function delProduct()
Error WW.CPP 128: 'num' is not a member of 'commodity' in function delProduct()
Error WW.CPP 130: 'num' is not a member of 'commodity' in function delProduct()
Error WW.CPP 130: 'name' is not a member of 'commodity' in function delProduct()
Error WW.CPP 130: 'ins' is not a member of 'commodity' in function delProduct()
Error WW.CPP 130: 'price' is not a member of 'commodity' in function delProduct()
Error WW.CPP 153: Too many initializers in function changeProduct()
Error WW.CPP 172: 'name' is not a member of 'commodity' in function changeProduct()
Error WW.CPP 174: 'ins' is not a member of 'commodity' in function changeProduct()
Error WW.CPP 174: Too many error or warning messages in function changeProduct()

touleg
PHP Super Member
PHP Super Member
Posts: 480
Joined: 25/01/2010 11:57 pm
Contact:

Re: ช่วยแก้โค๊ดหน่อยค่ะc++

Post by touleg » 07/04/2011 2:39 pm

โค้ดมั่วแหลก

milk2009
PHP Newbie
PHP Newbie
Posts: 3
Joined: 07/04/2011 9:01 am

Re: ช่วยแก้โค๊ดหน่อยค่ะc++

Post by milk2009 » 10/04/2011 3:02 pm

ช่วยหน่อยนะค่ะหนูต้องอาจารย์อ่ะค่ะถ้าไม่ส่งก้อไม่จบนะค่ะช่วยหนูหน่อย อย่าว่าหนูเลยค่ะ อาจารย์หนูเขาไม่ได้สอน สอนแบทเรียนแล้วเขาให้หนูมาหาโค๊ดเองค่ะ

Post Reply

Return to “Programming - C/C++ & java & Python”

Users browsing this forum: No registered users and 4 guests