Sunday, August 2, 2009

What's wrong with my vb C program???

#include "stdAfx.h"


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


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


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


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


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





int create();


void inputData();


void createFile();


int addRec();


void DisplayTrans();


int update();


void addFile();


void deleteRec();





void printfRecord();


int isFound(char *target,int cnt);


void deleteRec(char *target,int cnt);





void displayTitle();


void search(char[],int*,int*);


void displayRec();


void updateRec(int);


void searchUpdate();


void searchNdisplay();





struct


{


char transNum[5];


char studID[11];


char dateBorrowed[11];


}trHeaderRec,trHeaderTemp[4];








FILE *trHeaderFile;











int main (void)


{


int choice;





trHeaderFile = fopen("TRHEADER.DAT","r");





if((trHeaderFile = fopen("TRHEADER.DAT","r"))==NULL)


trHeaderFile = fopen("TRHEADER.DAT","w+");


printf("File could not be opened!");





else


{


switch(choice)


case 1: create();


break;


case 2: DisplayTrans();


break;


case 3: addRec();


break;


case 4: deleteRec();


break;


default:searchNdisplay;


break;


}





int create()


{


char response;





trHeaderFile = fopen("TRHEADER.DAT","rb");





if(trHeaderFile != NULL)


printf("FILE ALREADY EXISTS...\n");





do


{


fflush(stdin);


printf("Hit 'Y' to OVERWRITE, otherwise hit 'N'?\t");


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


}while(toupper(response) != 'Y'%26amp;%26amp;toupper(response) != 'N');





if(toupper(response) == 'Y')


{





createFile();


fclose(trHeaderFile);


}





else


{


createFile();


fclose(trHeaderFile);


}








return 0;


}





void createFile()


{


char response;


trHeaderFile = fopen("TRHEADER.DAT", "wb");


do


{


inputData();


fwrite(%26amp;trHeaderRec, sizeof(trHeaderRec), 1, trHeaderFile);


do


{


printf("Hit 'Y' to ADD MORE, hit 'N' to save and exit....\t");


fflush(stdin);


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





}while(toupper(response) != 'Y' %26amp;%26amp; toupper(response) != 'N');





}while(toupper(response) == 'Y');


}








void inputData()


{


system("cls");


printf("4-character Transaction Number: ");


scanf("%s", trHeaderRec.transNum);


printf("10-character Student I.D.: ");


scanf("%s", trHeaderRec.studID);


printf("10-character Date Borrowed[mm/dd/yyyy]: ");


scanf("%s", trHeaderRec.dateBorrowed);





}





//display





void DisplayTrans()


{





system("cls");





trHeaderFile=fopen("TRHEADER.dat","r")...


if((trHeaderFile= fopen("TRHEADER.dat","r"))==NULL)


{


printf("The file does not exist.");


}


else


{


fread(%26amp;trHeaderRec,sizeof(trHeaderRec)...





if(feof(trHeaderFile))


printf("File is empty");











else





{








printf("\t\t\t ***Transaction Report***\n");


printf("\n%5s%s%8s%s%4s%s", "", "TransNum", "", "studID", "", "dateBorrowed", "");








}





do





{





printf("\n%7s%s%12s%s%8s%s", " ", trHeaderRec.transNum, "", trHeaderRec.studID, "", trHeaderRec.dateBorrowed, "");


fread(%26amp;trHeaderRec,sizeof(trHeaderRec),1...











}while(!feof(trHeaderFile));














}fclose(trHeaderFile);





}





//add

















int addRec()


{


char reply;





trHeaderFile=fopen("TRHEADER.DAT","wb"...





if(trHeaderFile!=NULL)


printf("THE FILE ALREADY EXISTS\n");





do


{


fflush(stdin);


printf("Overwrite[Y/N]?\n");


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


}while(toupper(reply)!='Y'%26amp;%26amp;toupper(...





if(toupper(reply)=='Y')


{ addFile();


fclose(trHeaderFile);


}


else


{ addFile();


fclose(trHeaderFile);





}


return 0;


}








void addFile()


{


char reply;


trHeaderFile=fopen("TRHEADER.DAT","wb...


do


{


inputData();


fwrite(%26amp;trHeaderRec,sizeof(trHeaderR...


do


{


printf("Add more[Y/N]?:");


fflush(stdin);


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


}while(toupper(reply)!='Y'%26amp;%26amp;toupper(r...





}while(toupper(reply)=='Y');


}








//void deleteRec()





//{


//headerFile=fopen("TRHEADER.DAT","r"...


//if(headerFile==NULL)


// {





// printf("The file does not exist!\n");


// printf("Press any key to end!!");


// fflush(stdin);


// getch();


// }


// else


//{


//fread(%26amp;headerRec,sizeof(headerRec)...


//if(feof(headerFile))


// {


// printf("The file is empty");


// fflush(stdin);


// getch();


// }


// else


// searchNdisplay();


// }


// fclose(headerFile);





// return 0;


//}


void searchNdisplay()


{


char target[11],ans,ANS;








trHeaderFile=fopen("TRHEADER.DAT","r"...





int cnt=0,found=0;


fread(%26amp;trHeaderRec,sizeof(trHeaderRec...


while(!feof(trHeaderFile))


{





strcpy(trHeaderTemp[cnt].studID,trHe...


strcpy(trHeaderTemp[cnt].transNum,tr...


strcpy(trHeaderTemp[cnt].dateBorrowe...


fread(%26amp;trHeaderRec,sizeof(trHeaderRe...


cnt++;


}


fclose(trHeaderFile);


do


{


printf("Input studentID:");


fflush(stdin);


gets(target);





if(isFound(target,cnt))


{


printfRecord();


do


{


printf("Do you want to delete this record?(y/n): ");


fflush(stdin);


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


}while(ans!='y'%26amp;%26amp;ans!='Y'%26amp;%26amp;ans!='n'...





if(ans=='y'||ans=='Y')


{


deleteRec(target,cnt);


printf("\nThe record has been deleted\n");


}





do


{


printf("Do you want to search another record?(y/n): ");


fread(%26amp;trHeaderRec,sizeof(trHeader...


fflush(stdin);


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


}while(ANS!='y'%26amp;%26amp;ANS!='Y'%26amp;%26amp;ANS!='n'...


}


else


printf("Record is not in the list!");





trHeaderFile=fopen("TRHEADER.DAT","...





int cnt=0,found=0;


fread(%26amp;trHeaderRec,sizeof(trHeaderRec...


while(!feof(trHeaderFile))


{


strcpy(trHeaderRec.studID,trHeaderTe...


strcpy(trHeaderRec.transNum,trHeader...


strcpy(trHeaderRec.dateBorrowed,trHe...


fread(%26amp;trHeaderRec,sizeof(trHeaderRe...


cnt++;


}


fclose(trHeaderFile);


}while(ANS=='y'||ANS=='Y');





fclose(trHeaderFile);


}


int isFound(char *target,int cnt)


{


int found;





for(int a=0;a%26lt;cnt;a++)


{


if(strcmp(trHeaderTemp[a].studID,tar...


{


found=1;


}


else


fread(%26amp;trHeaderRec, sizeof(trHeaderRec),1,trHeaderFile);





}


return found;


}


void printfRecord()


{





printf("%s\t%s\t%s\t%s\t%s\t%s\t\n",t... trHeaderRec.dateBorrowed);


}


void deleteRec(char *target,int cnt)


{


int num=0;


for(int a=0;a%26lt;cnt;a++)


{


if(strcmp(trHeaderTemp[a].studID,tar...


{


for(int b=cnt;b%26gt;=num;b++)


{


trHeaderTemp[cnt]=trHeaderTemp[cnt...


}


break;


}


else


num++;


}


}





// update


int update()


{


trHeaderFile=fopen("TRHEADER.DAT","r")...





if(trHeaderFile==NULL)


{


printf("The file does no exist\n\n press any key to continue\n");


getch();





}


else


{


fread(%26amp;trHeaderRec,sizeof(trHeaderRec...


if(!feof(trHeaderFile))


printf("file is empty...\n\n");


else


searchUpdate();


}


return 0;


}





void searchUpdate()


{


char answer,target[5];


int found,size,len;





displayTitle();





do{


do{


printf("record to be update\n");


printf("transaction code");


scanf("%s",target);


len=strlen(target);





}


while(len!=4);


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


if(!found)


printf("the record does not exist");


else


{


displayRec();


do{


printf("update this record [Y/N]?");


fflush(stdin);


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


answer=toupper(answer);





}


while(answer!='Y'%26amp;%26amp; answer!='N');





if(answer=='Y')


updateRec(size);





do


{


printf("update this record [Y/N]?");


fflush(stdin);


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


answer=toupper(answer);


}


while(answer!='Y'%26amp;%26amp; answer!='N');





}


}


while(answer=='Y');


}





void displayTitle()


{


printf("\n\n");


printf("USJR\n");


printf("\tlibrary");


printf("\trecords of books borrowed");





}





void search(char tar[],int*fnd,int*sz)


{


int cnt=0;





trHeaderFile=fopen("TRHEADER.DAT","r"...





fread(%26amp;trHeaderRec,sizeof(trHeaderRec...


*fnd=0;


*sz=0;





while(!feof(trHeaderFile)%26amp;%26amp;*fnd!=1)


{


if(tar==trHeaderRec.transNum)


*fnd=1;


else


{


cnt++;


fread(%26amp;trHeaderRec,sizeof(trHeaderR...





*sz=cnt*sizeof(trHeaderRec);


}


}


}


void displayRec(){


printf("transaction number: %s\n",trHeaderRec.transNum);


printf("studID: %s\n",trHeaderRec.studID);


printf("dateBorrowed: %s\n",trHeaderRec.dateBorrowed);


}





void updateRec(int size)


{


char reply;


int choice;





do{


clearScrn();





do{





printf("which field you want to update?\n");


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


}


while(choice%26lt;4);





switch (choice)


{


case 1:


printf("transaction #\n");


scanf("%s",trHeaderRec.transNum);


break;


case 2:


printf("dateBorrowed\n");


scanf("%s",trHeaderRec.dateBorrowed);


break;


case 3:


printf("studID\n");


scanf("%s",trHeaderRec.studID);


break;


default:printf("unexpected choice of field\n");


}


do{


printf("do you want to update another?/n");


fflush(stdin);


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


}


while(reply!='Y'%26amp;%26amp;reply!='N');


}


while(reply=='Y');





fseek(trHeaderFile,size,SEEK_SET);


fwrite(%26amp;trHeaderRec,sizeof(trHeaderRec),...


fclose(trHeaderFile);


}

What's wrong with my vb C program???
I guess you didn't read the other post... Y!Answers isn't a good place to post code because it truncates lines. And, a program that large posted here isn't an easy thing to spot the problem.








What happens when you compile it?

flowering plum

No comments:

Post a Comment