Board index » Visual Studio » simple program attached

simple program attached

Visual Studio9
I'm a new user of Visual C++, I face the problem to make use of the rewind() function. Hereby I list down my simple program source code for you who can trace out my program's bugs as below:



//*program that search repor

#include <stdio.h

#include <conio.h

#include <ctype.h

#include <string.h

#include <stdlib.h

//===========================================================================

#define MAX 500//defining constan



typedef struct

char day[3]

char month[3]

char year[5]

}mydate



int checkDate (mydate)



void main(



char name[20], answer='Y',sfile='Y',smodify='Y'

char company[40],activity[MAX]

int id, target

char contact[15]

char address[30]

char tel[10]

int found = 0, valid=1

mydate dob



FILE *search_report,*temp; //*pointers to pre-defined structure FIL



/

if((search_report=fopen("c:\\addreport.txt","r")) == NULL)//*and assign to file point

printf("File open Error....")

else



temp=fopen("c:\\temp.txt","w+")



do

system("cls")

printf("\t\t\tTOWA BUSINESS MACHINE SDN. BHD.\t\t\n")

printf("\t\t\t SALES REPORTS SYSTEM\t\t\n\n")

printf("\t\t\t\tSEARCH REPORT\t\t\n\n")



fflush(stdin)

printf("ID to Find :")

scanf("%d",&target)



rewind(search_report)



while(1



if (feof(search_report)



printf("Eof.....")

break



}

do



fflush(stdin)

printf("DATE(dd/mm/yyyy) :")

scanf("%s %s %s",dob.day,dob.month,dob.year)

valid = checkDate(dob)



if(valid != 1



printf("Error in date....\n")

getch()



}while(valid == 0)



fflush(stdin); fflush(stdin)

fscanf(search_report,"%d\n",&id)

fscanf(search_report,"%s\n",dob.day)

fscanf(search_report,"%s\n",dob.month)

fscanf(search_report,"%s\n",dob.year)

fscanf(search_report,"%s\n",name)

fscanf(search_report,"%s\n",company)

fscanf(search_report,"%s\n",address)

fscanf(search_report,"%s\n",tel)

fscanf(search_report,"%s\n",contact)

fscanf(search_report,"%s\n",activity)



if(id == target && valid == checkDate(dob)



printf("\n\tRecord Found:\n")

printf("\t------------\n")

printf("Id = %d\n",id)

printf("DATE :%s/%s/%s\n",dob.day,dob.month,dob.year)

printf("NAME :%s\n",name)

printf("COMPANY:%s\n",company)

printf("TEL :%s\n",tel)

printf("CONTACT:%s\n",contact)

printf("ACTIVITY:%s\n\n",activity)





printf("Do You Want To Modify[Y/N]:")

smodify=getchar()



if(smodify=='Y'||smodify=='y'



fflush(stdin); fflush(stdin)

printf("NAME :")

gets(name)



printf("COMPANY :")

scanf("%s",company)



printf("ADDRESS :")

scanf("%s",address)



printf("TEL :")

scanf("%s",tel)



printf("CONTACT :")

scanf("%s",contact)



printf("ACTIVITY :")

scanf("%s",activity)

fflush(stdin)





printf("DO YOU WANT SAVE[Y/N]:")

sfile=getchar()



if (sfile=='Y'||sfile=='y'



fprintf(temp,"%s %s/ %s/ %s/ %s/ %s\n"

name,company,address,tel,contact,activity);





fflush(stdin);fflush(stdin);fflush(stdin);fflush(stdin)

printf("\n\nContinue NEXT [Y/N]:")

answer=toupper(getchar())

fflush(stdin)

}

}while(answer!='N')



printf("\t\t\t THANK YOU!!\n\n")



fclose(search_report);//close fil



remove("c:\\add_report.dat")

rename("c:\\temp.dat","c:\\add_report.dat")





int checkDate(mydate newDate



int dd=0, mm=0, yy=0

int valid = 1; // true before checkin



dd = atoi(newDate.day)

mm = atoi(newDate.month)

yy = atoi(newDate.year)



// printf("The dd %d mm %d yy %d\n",dd,mm,yy)

// getch()



if(yy ==0 || yy < 1990 || yy>2200

valid = 0

if(mm <=0 || mm>12

valid = 0

if (dd <= 0 || dd>31

valid = 0



return valid





The simple program above can be compiled without errors and warnings but just cannot do the searching correctly. It prompt up message to terminate the program after i key in the 'DATE', then the whole program cannot proceed. Please look through and give any suggestion for the solution. Thanks


-
 

Re:simple program attached

hi,

I dunno what is the format you are writing the data.

But I suggest a small change,



if((search_report=fopen("c:\\addreport.txt","r")) == NULL)

{

printf("File open Error....");

return;

}

you were continuing with the operation even after the file open failed.

And how did u enter the date ? did u enter it as 11/12/2003 (or

something like that). Then scanf would not return. Try with entering the

date as 11 12 2003 i.e. seperated by spaces and check

hth

gangadhar





nmp wrote:

Quote
I'm a new user of Visual C++, I face the problem to make use of the rewind() function. Hereby I list down my simple program source code for you who can trace out my program's bugs as below:-



//*program that search report

#include <stdio.h>

#include <conio.h>

#include <ctype.h>

#include <string.h>

#include <stdlib.h>

//============================================================================

#define MAX 500//defining constant



typedef struct{

char day[3];

char month[3];

char year[5];

}mydate;





int checkDate (mydate);



void main()

{

char name[20], answer='Y',sfile='Y',smodify='Y';

char company[40],activity[MAX];

int id, target;

char contact[15];

char address[30];

char tel[10];

int found = 0, valid=1;

mydate dob;



FILE *search_report,*temp; //*pointers to pre-defined structure FILE



//

if((search_report=fopen("c:\\addreport.txt","r")) == NULL)//*and assign to file pointr

printf("File open Error....");

else



temp=fopen("c:\\temp.txt","w+");



do{

system("cls");

printf("\t\t\tTOWA BUSINESS MACHINE SDN. BHD.\t\t\n");

printf("\t\t\t SALES REPORTS SYSTEM\t\t\n\n");

printf("\t\t\t\tSEARCH REPORT\t\t\n\n");





fflush(stdin);

printf("ID to Find :");

scanf("%d",&target);



rewind(search_report);



while(1)

{

if (feof(search_report))

{

printf("Eof.....");

break;



}

do

{

fflush(stdin);

printf("DATE(dd/mm/yyyy) :");

scanf("%s %s %s",dob.day,dob.month,dob.year);

valid = checkDate(dob);



if(valid != 1)

{

printf("Error in date....\n");

getch();

}

}while(valid == 0);



fflush(stdin); fflush(stdin);

fscanf(search_report,"%d\n",&id);

fscanf(search_report,"%s\n",dob.day);

fscanf(search_report,"%s\n",dob.month);

fscanf(search_report,"%s\n",dob.year);

fscanf(search_report,"%s\n",name);

fscanf(search_report,"%s\n",company);

fscanf(search_report,"%s\n",address);

fscanf(search_report,"%s\n",tel);

fscanf(search_report,"%s\n",contact);

fscanf(search_report,"%s\n",activity);



if(id == target && valid == checkDate(dob))

{

printf("\n\tRecord Found:\n");

printf("\t------------\n");

printf("Id = %d\n",id);

printf("DATE :%s/%s/%s\n",dob.day,dob.month,dob.year);

printf("NAME :%s\n",name);

printf("COMPANY:%s\n",company);

printf("TEL :%s\n",tel);

printf("CONTACT:%s\n",contact);

printf("ACTIVITY:%s\n\n",activity);

}



printf("Do You Want To Modify[Y/N]:");

smodify=getchar();



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

{

fflush(stdin); fflush(stdin);

printf("NAME :");

gets(name);



printf("COMPANY :");

scanf("%s",company);



printf("ADDRESS :");

scanf("%s",address);



printf("TEL :");

scanf("%s",tel);



printf("CONTACT :");

scanf("%s",contact);



printf("ACTIVITY :");

scanf("%s",activity);

fflush(stdin);



}

printf("DO YOU WANT SAVE[Y/N]:");

sfile=getchar();



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



fprintf(temp,"%s %s/ %s/ %s/ %s/ %s\n",

name,company,address,tel,contact,activity);





fflush(stdin);fflush(stdin);fflush(stdin);fflush(stdin);

printf("\n\nContinue NEXT [Y/N]:");

answer=toupper(getchar());

fflush(stdin);

}

}while(answer!='N');



printf("\t\t\t THANK YOU!!\n\n");



fclose(search_report);//close file



remove("c:\\add_report.dat");

rename("c:\\temp.dat","c:\\add_report.dat");

}



int checkDate(mydate newDate)

{

int dd=0, mm=0, yy=0;

int valid = 1; // true before checking



dd = atoi(newDate.day);

mm = atoi(newDate.month);

yy = atoi(newDate.year);



// printf("The dd %d mm %d yy %d\n",dd,mm,yy);

// getch();



if(yy ==0 || yy < 1990 || yy>2200)

valid = 0;

if(mm <=0 || mm>12)

valid = 0;

if (dd <= 0 || dd>31)

valid = 0;



return valid;

}



The simple program above can be compiled without errors and warnings but just cannot do the searching correctly. It prompt up message to terminate the program after i key in the 'DATE', then the whole program cannot proceed. Please look through and give any suggestion for the solution. Thanks!



-