Tuesday, July 14, 2009

Can you help me debug this program? c++ updating a record, its error is in the search function?

#include "stdafx.h"


#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;ctype.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;string.h%26gt;





void searchNupdate();


void dispRec();


void displayRecord();


void displayTitle();


void updateRecord();


void search(int, int, int);





struct book


{


char bookID[6];


char title[31];


char author[31];


char copyright[5];


}bookrec;





FILE *bookfile;





int main()


{


bookfile= fopen("BOOK.DAT", "rb");





if(bookfile==NULL)


{


printf("File does not exist, press any key..");


getch();


}


else


{


fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);


if(feof(bookfile))


{


printf("File is empty, press any key..");


getch();


}


else


searchNupdate();


}


fclose (bookfile);





return 0;


}





void searchNupdate(void)


{


char rep;


int found=0;


int target=0;


int size;





displayTitle();


do


{


printf("Input book ID to be updated:");


scanf("%d", %26amp;target);





search(target,%26amp;found,size);





if(!found)


{


printf("Record is not found\n");


}


else


{


displayRecord();


}


do


{


printf("Update (Y/N)? :");


scanf("%c", %26amp;rep);


}


while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');


if(rep=='y')


{


updateRecord();


}


do


{


printf("Update another (Y/N)? :");


fflush(stdin);


scanf("%c", %26amp;rep);


}while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');


}


while(rep=='y');


fclose(bookfile);


}





void displayTitle(void)


{


printf("\n\t\t==UPDATE A FILE==\n");


printf("\t\t==BOOK==\n");


printf("UPDATE RECORDS:\n\n");


}





void dispRec(void)


{


printf("%s%s%s%s\n",bookrec.bookID,bo...


}





void displayRecord(void)


{


int lineCounter=0;





while(!feof(bookfile))


{


displayTitle();





while(lineCounter%26lt;10 %26amp;%26amp; !feof(bookfile))


{


dispRec();


lineCounter++;


fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);


}


if(!feof(bookfile))


{


printf("Press any key to continue..");


getch();


}


else


printf("End of file, press any key..");


getch();


}


}





void updateRecord(void)


{


char nBookid[6];


char nTitle[31];


char nAuthor[31];


char nCopyright[5];


int field;


char rep;





do


{


printf("Which field to update[1-4]? :");


scanf("%d", %26amp;field);





while(field%26gt;4)





if(field==1)


{


printf("Input new book ID:");


gets(nBookid);


}


else if(field==2)


{


printf("Input new book title:");


gets(nTitle);


}


else if(field==3)


{


printf("Input new book author:");


gets(nAuthor);


}


else


{


printf("Input new copyright:");


gets(nCopyright);


}





do


{


printf("Update another field(Y/N)? :");


scanf("%c", %26amp;rep);


}


while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');





} while(rep=='y');





fseek(bookfile, 1, SEEK_SET);


fwrite(%26amp;bookrec, sizeof(bookrec), 1, (bookfile));


fclose(bookfile);


}





void search(int target[6], int found, int size)


{


int i;


int count=0;


int field=0;





bookfile= fopen("BOOK.DAT", "r+");


fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);





while(!feof(bookfile) %26amp;%26amp; found!=0)


{


if(target[i]==field)


{


found=1;


}


else


{


count++;


fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);


}


}


size= count * sizeof(%26amp;bookrec);


}

Can you help me debug this program? c++ updating a record, its error is in the search function?
i will try to help, email me and tell me whether or not you are trying to reverse engineer an already created program, if you are, i will not help, but if so, ill tell you what you need to do
Reply:what is that all about i did not understand any of the language
Reply:Search has conflicting parameter types.





It is declared as


void search(int, int, int);


but as implemented with:


void search(int target[6], int found, int size);





as you can see they are not the same. But I have no idea what some of the parameters are. The size paramater is useless, you don't even use it inside or outside of the function (if you plan to use it out of the function, change it to a reference to an int).





Your search function has way too many things wrong with it to work properly. Your variable i is uninitaliazed, so target[i] will just return junk. field is always 0, so target[i] is always compared with 0 (unless this is what you intended). found isnt a reference so it isnt useful outside of the function (as any changes to it applicable inside the function). bookfile is closed with fclose() (again unless you intended this). Sorry I can't be constructive in providing a solution, but I hate trying to decipher other people's code. But I hope these bring some problems out to light.





Also, the line -





while (tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');





will always end up in a infinite loop if rep is 'y','Y','n',or 'N'. This is because rep never changes in the loop and so the statement is either always true or false. Try something like this.





while (tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n')


{


scanf("%c", %26amp;rep);


}

pokemon cards

No comments:

Post a Comment