-
C Nested Data Structure
Hi I have a small database program which works well, except that it gives
a weird number when I display the 'date of birth' with the printf function.
I use a nested data structure to create the main structure (Particular).
I have debugged it again and again, but I couldn't find anything wrong. Can
anyone help me?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
#define MAX_CHRS 30
#define MED_CHRS 20
#define MIN_CHRS 10
#define PASS_CHRS 9
struct login {
char id[PASS_CHRS];
char password[PASS_CHRS];
};
struct login user;
struct birthdate {
int day[2];
int month[2];
int year[4];
};
struct particulars {
char surname[MED_CHRS];
char given_name[MED_CHRS];
birthdate birth;
char address[MAX_CHRS];
char phone[MED_CHRS];
char email[MAX_CHRS];
};
struct particulars data;
void Add(void);
void Del(void);
void Update(void);
void Search(void);
void Display(void);
void pause(int);
void menu(void);
void main(void) /************************************ start of Password
***/
{
char Password[9];
char input[9],ans;
int ok=1,root,x,choice;
do
{
menu();
gotoxy(23,8);
printf("L O G I N M E N U");
int count;
gotoxy(23,12);
printf("ENTER LOGIN ID: ");
gotoxy(23,14);
printf("ENTER PASSWORD: ");
gotoxy(39,12);
gets(input);
gotoxy(39,14);
for(count=0;count<80;count++){
Password[count]=getch();
printf("*");
if(Password[count] =='\r') break;
}
Password[count]=NULL;
if(!strcmp(input,"zoe") && !strcmp(Password,"1111"))
{ gotoxy(25,20);
puts("*** Access Granted ***");
pause(1);
ok=0;
root=1;
}
else
{ gotoxy(25,20);
puts("*** Invalid Password ***");
}
}
while(ok!=0);
do
{
menu();
gotoxy(28,6);
printf("MAIN MENU");
gotoxy(13,9);
printf("1) Add New Record");
gotoxy(13,11);
printf("2) Delete Record");
gotoxy(13,13);
printf("3) Update Record");
gotoxy(13,15);
printf("4) Search Record");
gotoxy(13,17);
printf("5) Display Records");
gotoxy(13,19);
printf("6) Exit ");
gotoxy(13,22);
printf("Enter your Option: ");
choice=getchar();
fflush(stdin);
switch(choice)
{
case '1':
Add();
break;
case '2':
Del();
break;
case '3':
Update();
break;
case '4':
Search();
break;
case '5':
Display();
break;
case '6':
gotoxy(12,24);
printf("See You!");
pause(1);
break;
default:
gotoxy(32,24);
puts("Invalid Choice...");
gotoxy(32,25);
puts("<Enter 1,2,3,4,5,6 only>");
}
}
while(choice!='6');
}
/*** end of Password Check ***/
/**** M E N U ****/
void menu(void) /************************************ start of Main Menu
***/
{
int i;
clrscr();
printf("\xDA");
for(i=0;i<=70;i++)
printf("\xC4");
printf("\xBF\n");
gotoxy(1,2);
printf("\xB3");
gotoxy(23,2);
printf("WELCOME TO ZOE'S ADDRESS BOOK");
gotoxy(73,2);
printf("\xB3\n");
printf("\xC3");
for(i=0;i<=70;i++)
printf("\xC4");
printf("\xB4\n");
for(i=3;i<=23;i++)
{
gotoxy(1,i);
printf("\xB3");
gotoxy(73,i);
printf("\xB3");
}
printf("\n\xC0");
for(i=0;i<=70;i++)
printf("\xC4");
printf("\xD9\n");
}
void pause(int seconds)
{
delay(seconds*1000); //Borland compilers
}
/**** PARTICULARS ****/
void Add(void)
{ FILE *particulars;
char input[12],ans;
particulars=fopen("Particul.dat","a");
if(particulars==NULL)
{
puts("Error opening file");
}
do
{
menu();
gotoxy(13,9);
printf("Surname: ");
scanf("%s",&data.surname);
gotoxy(13,11);
printf("Given Name: ");
scanf("%s",&data.given_name);
gotoxy(13,13);
printf("Date of birth date: ");
scanf("%d",&data.birth.day);
gotoxy(38,13);
printf("month: ");
scanf("%d",&data.birth.month);
gotoxy(49,13);
printf("year: ");
scanf("%d",&data.birth.year);
gotoxy(13,15);
printf("Address: ");
scanf("%s",&data.address);
gotoxy(13,17);
printf("Phone number: ");
scanf("%s",&data.phone);
gotoxy(13,19);
printf("E-mail address: ");
scanf("%s",&data.email);
fwrite(&data,sizeof(data),1,particulars);
choice:
gotoxy(13,21);
printf("Another entry (y/n) : ");
ans=toupper(getchar());
fflush(stdin);
if (ans !='Y' && ans !='N')
{ gotoxy(13,22);
puts("Press Y or N only");
getch();
gotoxy(13,21);
printf(" ");
gotoxy(13,22);
printf(" ");
goto choice;
}
}
while (ans != 'N' );
fclose(particulars);
} /* end of add */
void Display(void) /**** start of Display Module ****/
{
FILE *particulars;
int x;
particulars=fopen("Particul.dat","r");
if(particulars==NULL)
{
puts("Error opening file");
}
x=fread(&data,sizeof(data),1,particulars);
while(x!=0)
{
menu();
gotoxy(13,9);
printf("Surname: %s",data.surname);
gotoxy(13,11);
printf("Given Name: %s",data.given_name);
gotoxy(13,13);
printf("Date of birth date: %d",data.birth.day);
gotoxy(38,13);
printf("month: %d",data.birth.month);
gotoxy(49,13);
printf("year: %d",data.birth.year);
gotoxy(13,15);
printf("Address: %s",data.address);
gotoxy(13,17);
printf("Phone Number: %s",data.phone);
gotoxy(13,19);
printf("E-mail Address: %s",data.email);
gotoxy(13,22);
printf("Press <Enter> to go to the next record");
getch();
x=fread(&data,sizeof(data),1,particulars);
}
fclose(particulars);
} /*** end of display ***/
void Search(void)
{
FILE *particulars;
int x;
char input[25];
particulars=fopen("Particul.dat","r");
if(particulars==NULL)
{
puts("Error opening file");
}
menu();
gotoxy(13,9);
printf("Surname to search: ");
gets(input);
x=fread(&data,sizeof(data),1,particulars);
while(x!=0)
{
if(!strcmp(input,data.surname))
{
menu();
gotoxy(13,9);
printf("Surname: %s",data.surname);
gotoxy(13,11);
printf("Given Name: %s",data.given_name);
gotoxy(13,13);
printf("Date of birth date: %d",data.birth.day);
gotoxy(38,13);
printf("month: %d",data.birth.month);
gotoxy(49,13);
printf("year: %d",data.birth.year);
gotoxy(13,15);
printf("Address: %s",data.address);
gotoxy(13,17);
printf("Phone Number: %s",data.phone);
gotoxy(13,19);
printf("E-mail Address: %s",data.email);
}
getch();
x=fread(&data,sizeof(data),1,particulars);
}
fclose(particulars);
}
void Update(void)
{
FILE *particulars,*temp;
int x;
char input[25],ans;
particulars=fopen("Particul.dat","r");
if(particulars==NULL)
{
puts("Error opening file");
}
temp=fopen("temp.dat","w");
if(temp==NULL)
{
puts("Error opening file");
}
menu();
gotoxy(13,9);
printf("Surname to edit: ");
gets(input);
x=fread(&data,sizeof(data),1,particulars);
while(x!=0)
{
if(!strcmp(input,data.surname))
{
menu();
gotoxy(13,9);
printf("Surname: %s",data.surname);
gotoxy(13,11);
printf("Given Name: %s",data.given_name);
gotoxy(13,13);
printf("Date of birth date: %d",data.birth.day);
gotoxy(38,13);
printf("month: %d",data.birth.month);
gotoxy(49,13);
printf("year: %d",data.birth.year);
gotoxy(13,15);
printf("Address: %s",data.address);
gotoxy(13,17);
printf("Phone Number: %s",data.phone);
gotoxy(13,19);
printf("E-mail Address: %s",data.email);
do
{
menu();
gotoxy(13,9);
printf("Edit <S>urname");
gotoxy(13,11);
printf(" <G>iven Name");
gotoxy(13,13);
printf(" <D>ate of birth");
gotoxy(13,15);
printf(" <A>ddress");
gotoxy(13,17);
printf(" <P>hone number");
gotoxy(13,19);
printf(" <E>mail");
ans=toupper(getchar());
fflush(stdin);
switch(ans)
{
case 'S':
gotoxy(13,21);
printf("Surname: ");
gets(data.surname);
fwrite(&data,sizeof(data),1,temp);
break;
case 'G':
gotoxy(13,21);
printf("Given Name: ");
gets(data.given_name);
fwrite(&data,sizeof(data),1,temp);
break;
case 'D':
gotoxy(13,21);
printf("Date of birth date: ");
scanf("%d",data.birth.day);
gotoxy(38,21);
printf("month: ");
scanf("%d",data.birth.month);
gotoxy(49,21);
printf("year: ");
scanf("%d",data.birth.year);
fwrite(&data,sizeof(data),1,temp);
break;
case 'A':
gotoxy(13,21);
printf("Address: ");
gets(data.address);
fwrite(&data,sizeof(data),1,temp);
break;
case 'P':
gotoxy(13,21);
printf("Phone: ");
gets(data.phone);
fwrite(&data,sizeof(data),1,temp);
break;
case 'E':
gotoxy(13,21);
printf("E-mail: ");
gets(data.email);
fwrite(&data,sizeof(data),1,temp);
break;
default:
gotoxy(13,21);
puts("Press S, G, D, A, P or E only");
getch();
gotoxy(13,20);
printf(" ");
gotoxy(13,21);
printf(" ");
break;
}
}
while(ans !='S' && ans !='G' && ans !='D' && ans !='A' && ans!='P' && ans!='E');
}
fwrite(&data,sizeof(data),1,temp);
x=fread(&data,sizeof(data),1,particulars);
pause(1);
}
fclose(temp);
fclose(particulars);
remove("Particul.dat");
rename("temp.dat","particulars.dat");
}
void Del(void)
{
FILE *particulars,*temp;
int x;
char input[25],ans;
particulars=fopen("Particul.dat","r");
if(particulars==NULL)
{
puts("Error opening file");
}
temp=fopen("temp.dat","w");
if(temp==NULL)
{
puts("Error opening file");
}
menu();
gotoxy(13,9);
printf("Surname to Delete: ");
gets(input);
x=fread(&data,sizeof(data),1,particulars);
while(x!=0)
{
if(!strcmp(input,data.surname))
{
gotoxy(13,11);
printf("Surname: %s",data.surname);
gotoxy(13,13);
printf("Given Name: %s",data.given_name);
gotoxy(13,15);
printf("Date of birth date: %d",data.birth.day);
gotoxy(38,13);
printf("month: %d",data.birth.month);
gotoxy(49,13);
printf("year: %d",data.birth.year);
gotoxy(13,17);
printf("Address: %s",data.address);
gotoxy(13,19);
printf("Phone Number: %s",data.phone);
gotoxy(13,21);
printf("E-mail Address: %s",data.email);
do
{
gotoxy(13,23);
printf("Confirm Delete (Y/N) : ");
ans=toupper(getchar());
fflush(stdin);
if (ans !='Y' && ans !='N')
{ gotoxy(13,24);
puts("Press Y or N only");
getch();
gotoxy(13,23);
printf(" ");
gotoxy(13,24);
printf(" ");
}
}
while (ans!='Y');
x=fread(&data,sizeof(data),1,particulars);
}
if(strcmp(input,data.surname))
{ fwrite(&data,sizeof(data),1,temp);
x=fread(&data,sizeof(data),1,particulars);
}
pause(1);
}
fclose(temp);
fclose(particulars);
remove("Particul.dat");
rename("temp.dat","Particul.dat");
}
-
Re: C Nested Data Structure
"Zoenaedy" <zoen@lycosasia.com> wrote:
>Hi there,
If you want to keep the date information as intergers remove the [2] from
Day and month, and [4] from Year.This is not required for intergers and does
not show up as an error during compliling.So you should now have.
int day;
int month;
int year;
try that first.
The other option is to change these 3 inputs to character arrays like
char day[2];
char month[2];
char year[4];
and print out using printf("%s",data.birth.year);
I have not tried any of these on a pc but these suggestions should help you...
Cheers,
Scott..
PS. you don't know anything about serial communications do you ?
>Hi I have a small database program which works well, except that it gives
>a weird number when I display the 'date of birth' with the printf function.
>I use a nested data structure to create the main structure (Particular).
>I have debugged it again and again, but I couldn't find anything wrong.
Can
>anyone help me?
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <conio.h>
>#include <ctype.h>
>#include <string.h>
>#include <dos.h>
>
>#define MAX_CHRS 30
>#define MED_CHRS 20
>#define MIN_CHRS 10
>#define PASS_CHRS 9
>
>struct login {
> char id[PASS_CHRS];
> char password[PASS_CHRS];
> };
>struct login user;
>
>struct birthdate {
> int day[2];
> int month[2];
> int year[4];
> };
>
>struct particulars {
> char surname[MED_CHRS];
> char given_name[MED_CHRS];
> birthdate birth;
> char address[MAX_CHRS];
> char phone[MED_CHRS];
> char email[MAX_CHRS];
>
> };
>
>struct particulars data;
>
>
>
>void Add(void);
>void Del(void);
>void Update(void);
>void Search(void);
>void Display(void);
>void pause(int);
>void menu(void);
>
>
>void main(void) /************************************ start of Password
>***/
>{
>
> char Password[9];
> char input[9],ans;
> int ok=1,root,x,choice;
>
>
> do
> {
> menu();
> gotoxy(23,8);
> printf("L O G I N M E N U");
> int count;
> gotoxy(23,12);
> printf("ENTER LOGIN ID: ");
>
> gotoxy(23,14);
> printf("ENTER PASSWORD: ");
> gotoxy(39,12);
> gets(input);
> gotoxy(39,14);
>
> for(count=0;count<80;count++){
> Password[count]=getch();
> printf("*");
> if(Password[count] =='\r') break;
> }
>
> Password[count]=NULL;
> if(!strcmp(input,"zoe") && !strcmp(Password,"1111"))
> { gotoxy(25,20);
> puts("*** Access Granted ***");
> pause(1);
> ok=0;
> root=1;
> }
> else
> { gotoxy(25,20);
> puts("*** Invalid Password ***");
>
> }
>
>
>
> }
> while(ok!=0);
> do
> {
> menu();
> gotoxy(28,6);
> printf("MAIN MENU");
> gotoxy(13,9);
> printf("1) Add New Record");
> gotoxy(13,11);
> printf("2) Delete Record");
> gotoxy(13,13);
> printf("3) Update Record");
> gotoxy(13,15);
> printf("4) Search Record");
> gotoxy(13,17);
> printf("5) Display Records");
> gotoxy(13,19);
> printf("6) Exit ");
> gotoxy(13,22);
> printf("Enter your Option: ");
>
> choice=getchar();
> fflush(stdin);
> switch(choice)
> {
> case '1':
> Add();
> break;
>
> case '2':
> Del();
> break;
>
> case '3':
> Update();
> break;
>
> case '4':
> Search();
> break;
>
> case '5':
> Display();
> break;
>
> case '6':
> gotoxy(12,24);
> printf("See You!");
> pause(1);
> break;
>
> default:
> gotoxy(32,24);
> puts("Invalid Choice...");
> gotoxy(32,25);
> puts("<Enter 1,2,3,4,5,6 only>");
>
>
> }
> }
> while(choice!='6');
>
>}
>
> /*** end of Password Check ***/
>
>/**** M E N U ****/
>
> void menu(void) /************************************ start of Main
Menu
>***/
> {
> int i;
> clrscr();
> printf("\xDA");
> for(i=0;i<=70;i++)
> printf("\xC4");
> printf("\xBF\n");
> gotoxy(1,2);
> printf("\xB3");
> gotoxy(23,2);
> printf("WELCOME TO ZOE'S ADDRESS BOOK");
> gotoxy(73,2);
> printf("\xB3\n");
> printf("\xC3");
> for(i=0;i<=70;i++)
> printf("\xC4");
> printf("\xB4\n");
>
> for(i=3;i<=23;i++)
> {
> gotoxy(1,i);
> printf("\xB3");
> gotoxy(73,i);
> printf("\xB3");
> }
> printf("\n\xC0");
> for(i=0;i<=70;i++)
> printf("\xC4");
> printf("\xD9\n");
>
> }
>
>void pause(int seconds)
> {
> delay(seconds*1000); //Borland compilers
> }
>
>
>/**** PARTICULARS ****/
>void Add(void)
>{ FILE *particulars;
>
> char input[12],ans;
>
> particulars=fopen("Particul.dat","a");
> if(particulars==NULL)
> {
> puts("Error opening file");
> }
>
>
>
>do
>{
> menu();
> gotoxy(13,9);
> printf("Surname: ");
> scanf("%s",&data.surname);
> gotoxy(13,11);
> printf("Given Name: ");
> scanf("%s",&data.given_name);
> gotoxy(13,13);
> printf("Date of birth date: ");
> scanf("%d",&data.birth.day);
> gotoxy(38,13);
> printf("month: ");
> scanf("%d",&data.birth.month);
> gotoxy(49,13);
> printf("year: ");
> scanf("%d",&data.birth.year);
> gotoxy(13,15);
> printf("Address: ");
> scanf("%s",&data.address);
> gotoxy(13,17);
> printf("Phone number: ");
> scanf("%s",&data.phone);
> gotoxy(13,19);
> printf("E-mail address: ");
> scanf("%s",&data.email);
>
>
>
> fwrite(&data,sizeof(data),1,particulars);
>
> choice:
> gotoxy(13,21);
> printf("Another entry (y/n) : ");
> ans=toupper(getchar());
> fflush(stdin);
> if (ans !='Y' && ans !='N')
> { gotoxy(13,22);
> puts("Press Y or N only");
> getch();
> gotoxy(13,21);
> printf(" ");
> gotoxy(13,22);
> printf(" ");
> goto choice;
>
> }
>}
>
> while (ans != 'N' );
> fclose(particulars);
>} /* end of add */
>
>void Display(void) /**** start of Display Module ****/
>{
> FILE *particulars;
>
> int x;
> particulars=fopen("Particul.dat","r");
> if(particulars==NULL)
> {
> puts("Error opening file");
> }
>
> x=fread(&data,sizeof(data),1,particulars);
> while(x!=0)
> {
> menu();
> gotoxy(13,9);
> printf("Surname: %s",data.surname);
> gotoxy(13,11);
> printf("Given Name: %s",data.given_name);
> gotoxy(13,13);
> printf("Date of birth date: %d",data.birth.day);
> gotoxy(38,13);
> printf("month: %d",data.birth.month);
> gotoxy(49,13);
> printf("year: %d",data.birth.year);
> gotoxy(13,15);
> printf("Address: %s",data.address);
> gotoxy(13,17);
> printf("Phone Number: %s",data.phone);
> gotoxy(13,19);
> printf("E-mail Address: %s",data.email);
> gotoxy(13,22);
> printf("Press <Enter> to go to the next record");
> getch();
> x=fread(&data,sizeof(data),1,particulars);
>
>}
>fclose(particulars);
>} /*** end of display ***/
>
>void Search(void)
>{
> FILE *particulars;
>
> int x;
> char input[25];
>
> particulars=fopen("Particul.dat","r");
> if(particulars==NULL)
> {
> puts("Error opening file");
> }
> menu();
> gotoxy(13,9);
> printf("Surname to search: ");
> gets(input);
> x=fread(&data,sizeof(data),1,particulars);
> while(x!=0)
> {
> if(!strcmp(input,data.surname))
> {
> menu();
>
> gotoxy(13,9);
> printf("Surname: %s",data.surname);
> gotoxy(13,11);
> printf("Given Name: %s",data.given_name);
> gotoxy(13,13);
> printf("Date of birth date: %d",data.birth.day);
> gotoxy(38,13);
> printf("month: %d",data.birth.month);
> gotoxy(49,13);
> printf("year: %d",data.birth.year);
> gotoxy(13,15);
> printf("Address: %s",data.address);
> gotoxy(13,17);
> printf("Phone Number: %s",data.phone);
> gotoxy(13,19);
> printf("E-mail Address: %s",data.email);
> }
> getch();
> x=fread(&data,sizeof(data),1,particulars);
>
>}
>fclose(particulars);
>}
>void Update(void)
>{
> FILE *particulars,*temp;
>
> int x;
> char input[25],ans;
>
> particulars=fopen("Particul.dat","r");
> if(particulars==NULL)
> {
> puts("Error opening file");
> }
> temp=fopen("temp.dat","w");
> if(temp==NULL)
> {
> puts("Error opening file");
> }
> menu();
> gotoxy(13,9);
> printf("Surname to edit: ");
> gets(input);
> x=fread(&data,sizeof(data),1,particulars);
> while(x!=0)
> {
> if(!strcmp(input,data.surname))
> {
> menu();
> gotoxy(13,9);
> printf("Surname: %s",data.surname);
> gotoxy(13,11);
> printf("Given Name: %s",data.given_name);
> gotoxy(13,13);
> printf("Date of birth date: %d",data.birth.day);
> gotoxy(38,13);
> printf("month: %d",data.birth.month);
> gotoxy(49,13);
> printf("year: %d",data.birth.year);
> gotoxy(13,15);
> printf("Address: %s",data.address);
> gotoxy(13,17);
> printf("Phone Number: %s",data.phone);
> gotoxy(13,19);
> printf("E-mail Address: %s",data.email);
> do
> {
> menu();
> gotoxy(13,9);
> printf("Edit <S>urname");
> gotoxy(13,11);
> printf(" <G>iven Name");
> gotoxy(13,13);
> printf(" <D>ate of birth");
> gotoxy(13,15);
> printf(" <A>ddress");
> gotoxy(13,17);
> printf(" <P>hone number");
> gotoxy(13,19);
> printf(" <E>mail");
> ans=toupper(getchar());
> fflush(stdin);
> switch(ans)
> {
> case 'S':
> gotoxy(13,21);
> printf("Surname: ");
> gets(data.surname);
> fwrite(&data,sizeof(data),1,temp);
> break;
> case 'G':
> gotoxy(13,21);
> printf("Given Name: ");
> gets(data.given_name);
> fwrite(&data,sizeof(data),1,temp);
> break;
> case 'D':
> gotoxy(13,21);
> printf("Date of birth date: ");
> scanf("%d",data.birth.day);
> gotoxy(38,21);
> printf("month: ");
> scanf("%d",data.birth.month);
> gotoxy(49,21);
> printf("year: ");
> scanf("%d",data.birth.year);
> fwrite(&data,sizeof(data),1,temp);
> break;
> case 'A':
> gotoxy(13,21);
> printf("Address: ");
> gets(data.address);
> fwrite(&data,sizeof(data),1,temp);
> break;
> case 'P':
> gotoxy(13,21);
> printf("Phone: ");
> gets(data.phone);
> fwrite(&data,sizeof(data),1,temp);
> break;
> case 'E':
> gotoxy(13,21);
> printf("E-mail: ");
> gets(data.email);
> fwrite(&data,sizeof(data),1,temp);
> break;
>
>
> default:
> gotoxy(13,21);
> puts("Press S, G, D, A, P or E only");
> getch();
> gotoxy(13,20);
> printf(" ");
> gotoxy(13,21);
> printf(" ");
> break;
> }
> }
>
> while(ans !='S' && ans !='G' && ans !='D' && ans !='A' && ans!='P' && ans!='E');
> }
>
> fwrite(&data,sizeof(data),1,temp);
> x=fread(&data,sizeof(data),1,particulars);
> pause(1);
> }
>
> fclose(temp);
> fclose(particulars);
> remove("Particul.dat");
> rename("temp.dat","particulars.dat");
>
>}
>void Del(void)
>{
> FILE *particulars,*temp;
>
> int x;
> char input[25],ans;
>
> particulars=fopen("Particul.dat","r");
> if(particulars==NULL)
> {
> puts("Error opening file");
> }
> temp=fopen("temp.dat","w");
> if(temp==NULL)
> {
> puts("Error opening file");
> }
> menu();
> gotoxy(13,9);
> printf("Surname to Delete: ");
> gets(input);
> x=fread(&data,sizeof(data),1,particulars);
> while(x!=0)
> {
> if(!strcmp(input,data.surname))
> {
> gotoxy(13,11);
> printf("Surname: %s",data.surname);
> gotoxy(13,13);
> printf("Given Name: %s",data.given_name);
> gotoxy(13,15);
> printf("Date of birth date: %d",data.birth.day);
> gotoxy(38,13);
> printf("month: %d",data.birth.month);
> gotoxy(49,13);
> printf("year: %d",data.birth.year);
> gotoxy(13,17);
> printf("Address: %s",data.address);
> gotoxy(13,19);
> printf("Phone Number: %s",data.phone);
> gotoxy(13,21);
> printf("E-mail Address: %s",data.email);
>do
> {
> gotoxy(13,23);
> printf("Confirm Delete (Y/N) : ");
> ans=toupper(getchar());
> fflush(stdin);
> if (ans !='Y' && ans !='N')
> { gotoxy(13,24);
> puts("Press Y or N only");
> getch();
> gotoxy(13,23);
> printf(" ");
> gotoxy(13,24);
> printf(" ");
> }
> }
>while (ans!='Y');
> x=fread(&data,sizeof(data),1,particulars);
> }
> if(strcmp(input,data.surname))
> { fwrite(&data,sizeof(data),1,temp);
> x=fread(&data,sizeof(data),1,particulars);
> }
> pause(1);
> }
>
> fclose(temp);
> fclose(particulars);
> remove("Particul.dat");
> rename("temp.dat","Particul.dat");
> }
>
>
-
Re: C Nested Data Structure
Thanks for the advise, but when I use 'char' there is an overflow of the address
string on the date field, ie
date: 12 month:1 year: 1974Los Angeles
address: Los Angeles
The same thing happen when I removed the integer size.
NB: Sorry, I don't know about serial communications.
"Scott" <scott.rose@nmb.co.uk> wrote:
>
>"Zoenaedy" <zoen@lycosasia.com> wrote:
>>Hi there,
>
>If you want to keep the date information as intergers remove the [2] from
>Day and month, and [4] from Year.This is not required for intergers and
does
>not show up as an error during compliling.So you should now have.
>
>int day;
>int month;
>int year;
> try that first.
>
>The other option is to change these 3 inputs to character arrays like
>char day[2];
>char month[2];
>char year[4];
>
>and print out using printf("%s",data.birth.year);
>
>I have not tried any of these on a pc but these suggestions should help
you...
>
>Cheers,
> Scott..
>
>PS. you don't know anything about serial communications do you ?
>
>
>
>
>
>>Hi I have a small database program which works well, except that it gives
>>a weird number when I display the 'date of birth' with the printf function.
>>I use a nested data structure to create the main structure (Particular).
>>I have debugged it again and again, but I couldn't find anything wrong.
>Can
>>anyone help me?
>>
>>#include <stdio.h>
>>#include <stdlib.h>
>>#include <conio.h>
>>#include <ctype.h>
>>#include <string.h>
>>#include <dos.h>
>>
>>#define MAX_CHRS 30
>>#define MED_CHRS 20
>>#define MIN_CHRS 10
>>#define PASS_CHRS 9
>>
>>struct login {
>> char id[PASS_CHRS];
>> char password[PASS_CHRS];
>> };
>>struct login user;
>>
>>struct birthdate {
>> int day[2];
>> int month[2];
>> int year[4];
>> };
>>
>>struct particulars {
>> char surname[MED_CHRS];
>> char given_name[MED_CHRS];
>> birthdate birth;
>> char address[MAX_CHRS];
>> char phone[MED_CHRS];
>> char email[MAX_CHRS];
>>
>> };
>>
>>struct particulars data;
>>
>>
>>
>>void Add(void);
>>void Del(void);
>>void Update(void);
>>void Search(void);
>>void Display(void);
>>void pause(int);
>>void menu(void);
>>
>>
>>void main(void) /************************************ start of Password
>>***/
>>{
>>
>> char Password[9];
>> char input[9],ans;
>> int ok=1,root,x,choice;
>>
>>
>> do
>> {
>> menu();
>> gotoxy(23,8);
>> printf("L O G I N M E N U");
>> int count;
>> gotoxy(23,12);
>> printf("ENTER LOGIN ID: ");
>>
>> gotoxy(23,14);
>> printf("ENTER PASSWORD: ");
>> gotoxy(39,12);
>> gets(input);
>> gotoxy(39,14);
>>
>> for(count=0;count<80;count++){
>> Password[count]=getch();
>> printf("*");
>> if(Password[count] =='\r') break;
>> }
>>
>> Password[count]=NULL;
>> if(!strcmp(input,"zoe") && !strcmp(Password,"1111"))
>> { gotoxy(25,20);
>> puts("*** Access Granted ***");
>> pause(1);
>> ok=0;
>> root=1;
>> }
>> else
>> { gotoxy(25,20);
>> puts("*** Invalid Password ***");
>>
>> }
>>
>>
>>
>> }
>> while(ok!=0);
>> do
>> {
>> menu();
>> gotoxy(28,6);
>> printf("MAIN MENU");
>> gotoxy(13,9);
>> printf("1) Add New Record");
>> gotoxy(13,11);
>> printf("2) Delete Record");
>> gotoxy(13,13);
>> printf("3) Update Record");
>> gotoxy(13,15);
>> printf("4) Search Record");
>> gotoxy(13,17);
>> printf("5) Display Records");
>> gotoxy(13,19);
>> printf("6) Exit ");
>> gotoxy(13,22);
>> printf("Enter your Option: ");
>>
>> choice=getchar();
>> fflush(stdin);
>> switch(choice)
>> {
>> case '1':
>> Add();
>> break;
>>
>> case '2':
>> Del();
>> break;
>>
>> case '3':
>> Update();
>> break;
>>
>> case '4':
>> Search();
>> break;
>>
>> case '5':
>> Display();
>> break;
>>
>> case '6':
>> gotoxy(12,24);
>> printf("See You!");
>> pause(1);
>> break;
>>
>> default:
>> gotoxy(32,24);
>> puts("Invalid Choice...");
>> gotoxy(32,25);
>> puts("<Enter 1,2,3,4,5,6 only>");
>>
>>
>> }
>> }
>> while(choice!='6');
>>
>>}
>>
>> /*** end of Password Check ***/
>>
>>/**** M E N U ****/
>>
>> void menu(void) /************************************ start of Main
>Menu
>>***/
>> {
>> int i;
>> clrscr();
>> printf("\xDA");
>> for(i=0;i<=70;i++)
>> printf("\xC4");
>> printf("\xBF\n");
>> gotoxy(1,2);
>> printf("\xB3");
>> gotoxy(23,2);
>> printf("WELCOME TO ZOE'S ADDRESS BOOK");
>> gotoxy(73,2);
>> printf("\xB3\n");
>> printf("\xC3");
>> for(i=0;i<=70;i++)
>> printf("\xC4");
>> printf("\xB4\n");
>>
>> for(i=3;i<=23;i++)
>> {
>> gotoxy(1,i);
>> printf("\xB3");
>> gotoxy(73,i);
>> printf("\xB3");
>> }
>> printf("\n\xC0");
>> for(i=0;i<=70;i++)
>> printf("\xC4");
>> printf("\xD9\n");
>>
>> }
>>
>>void pause(int seconds)
>> {
>> delay(seconds*1000); //Borland compilers
>> }
>>
>>
>>/**** PARTICULARS ****/
>>void Add(void)
>>{ FILE *particulars;
>>
>> char input[12],ans;
>>
>> particulars=fopen("Particul.dat","a");
>> if(particulars==NULL)
>> {
>> puts("Error opening file");
>> }
>>
>>
>>
>>do
>>{
>> menu();
>> gotoxy(13,9);
>> printf("Surname: ");
>> scanf("%s",&data.surname);
>> gotoxy(13,11);
>> printf("Given Name: ");
>> scanf("%s",&data.given_name);
>> gotoxy(13,13);
>> printf("Date of birth date: ");
>> scanf("%d",&data.birth.day);
>> gotoxy(38,13);
>> printf("month: ");
>> scanf("%d",&data.birth.month);
>> gotoxy(49,13);
>> printf("year: ");
>> scanf("%d",&data.birth.year);
>> gotoxy(13,15);
>> printf("Address: ");
>> scanf("%s",&data.address);
>> gotoxy(13,17);
>> printf("Phone number: ");
>> scanf("%s",&data.phone);
>> gotoxy(13,19);
>> printf("E-mail address: ");
>> scanf("%s",&data.email);
>>
>>
>>
>> fwrite(&data,sizeof(data),1,particulars);
>>
>> choice:
>> gotoxy(13,21);
>> printf("Another entry (y/n) : ");
>> ans=toupper(getchar());
>> fflush(stdin);
>> if (ans !='Y' && ans !='N')
>> { gotoxy(13,22);
>> puts("Press Y or N only");
>> getch();
>> gotoxy(13,21);
>> printf(" ");
>> gotoxy(13,22);
>> printf(" ");
>> goto choice;
>>
>> }
>>}
>>
>> while (ans != 'N' );
>> fclose(particulars);
>>} /* end of add */
>>
>>void Display(void) /**** start of Display Module ****/
>>{
>> FILE *particulars;
>>
>> int x;
>> particulars=fopen("Particul.dat","r");
>> if(particulars==NULL)
>> {
>> puts("Error opening file");
>> }
>>
>> x=fread(&data,sizeof(data),1,particulars);
>> while(x!=0)
>> {
>> menu();
>> gotoxy(13,9);
>> printf("Surname: %s",data.surname);
>> gotoxy(13,11);
>> printf("Given Name: %s",data.given_name);
>> gotoxy(13,13);
>> printf("Date of birth date: %d",data.birth.day);
>> gotoxy(38,13);
>> printf("month: %d",data.birth.month);
>> gotoxy(49,13);
>> printf("year: %d",data.birth.year);
>> gotoxy(13,15);
>> printf("Address: %s",data.address);
>> gotoxy(13,17);
>> printf("Phone Number: %s",data.phone);
>> gotoxy(13,19);
>> printf("E-mail Address: %s",data.email);
>> gotoxy(13,22);
>> printf("Press <Enter> to go to the next record");
>> getch();
>> x=fread(&data,sizeof(data),1,particulars);
>>
>>}
>>fclose(particulars);
>>} /*** end of display ***/
>>
>>void Search(void)
>>{
>> FILE *particulars;
>>
>> int x;
>> char input[25];
>>
>> particulars=fopen("Particul.dat","r");
>> if(particulars==NULL)
>> {
>> puts("Error opening file");
>> }
>> menu();
>> gotoxy(13,9);
>> printf("Surname to search: ");
>> gets(input);
>> x=fread(&data,sizeof(data),1,particulars);
>> while(x!=0)
>> {
>> if(!strcmp(input,data.surname))
>> {
>> menu();
>>
>> gotoxy(13,9);
>> printf("Surname: %s",data.surname);
>> gotoxy(13,11);
>> printf("Given Name: %s",data.given_name);
>> gotoxy(13,13);
>> printf("Date of birth date: %d",data.birth.day);
>> gotoxy(38,13);
>> printf("month: %d",data.birth.month);
>> gotoxy(49,13);
>> printf("year: %d",data.birth.year);
>> gotoxy(13,15);
>> printf("Address: %s",data.address);
>> gotoxy(13,17);
>> printf("Phone Number: %s",data.phone);
>> gotoxy(13,19);
>> printf("E-mail Address: %s",data.email);
>> }
>> getch();
>> x=fread(&data,sizeof(data),1,particulars);
>>
>>}
>>fclose(particulars);
>>}
>>void Update(void)
>>{
>> FILE *particulars,*temp;
>>
>> int x;
>> char input[25],ans;
>>
>> particulars=fopen("Particul.dat","r");
>> if(particulars==NULL)
>> {
>> puts("Error opening file");
>> }
>> temp=fopen("temp.dat","w");
>> if(temp==NULL)
>> {
>> puts("Error opening file");
>> }
>> menu();
>> gotoxy(13,9);
>> printf("Surname to edit: ");
>> gets(input);
>> x=fread(&data,sizeof(data),1,particulars);
>> while(x!=0)
>> {
>> if(!strcmp(input,data.surname))
>> {
>> menu();
>> gotoxy(13,9);
>> printf("Surname: %s",data.surname);
>> gotoxy(13,11);
>> printf("Given Name: %s",data.given_name);
>> gotoxy(13,13);
>> printf("Date of birth date: %d",data.birth.day);
>> gotoxy(38,13);
>> printf("month: %d",data.birth.month);
>> gotoxy(49,13);
>> printf("year: %d",data.birth.year);
>> gotoxy(13,15);
>> printf("Address: %s",data.address);
>> gotoxy(13,17);
>> printf("Phone Number: %s",data.phone);
>> gotoxy(13,19);
>> printf("E-mail Address: %s",data.email);
>> do
>> {
>> menu();
>> gotoxy(13,9);
>> printf("Edit <S>urname");
>> gotoxy(13,11);
>> printf(" <G>iven Name");
>> gotoxy(13,13);
>> printf(" <D>ate of birth");
>> gotoxy(13,15);
>> printf(" <A>ddress");
>> gotoxy(13,17);
>> printf(" <P>hone number");
>> gotoxy(13,19);
>> printf(" <E>mail");
>> ans=toupper(getchar());
>> fflush(stdin);
>> switch(ans)
>> {
>> case 'S':
>> gotoxy(13,21);
>> printf("Surname: ");
>> gets(data.surname);
>> fwrite(&data,sizeof(data),1,temp);
>> break;
>> case 'G':
>> gotoxy(13,21);
>> printf("Given Name: ");
>> gets(data.given_name);
>> fwrite(&data,sizeof(data),1,temp);
>> break;
>> case 'D':
>> gotoxy(13,21);
>> printf("Date of birth date: ");
>> scanf("%d",data.birth.day);
>> gotoxy(38,21);
>> printf("month: ");
>> scanf("%d",data.birth.month);
>> gotoxy(49,21);
>> printf("year: ");
>> scanf("%d",data.birth.year);
>> fwrite(&data,sizeof(data),1,temp);
>> break;
>> case 'A':
>> gotoxy(13,21);
>> printf("Address: ");
>> gets(data.address);
>> fwrite(&data,sizeof(data),1,temp);
>> break;
>> case 'P':
>> gotoxy(13,21);
>> printf("Phone: ");
>> gets(data.phone);
>> fwrite(&data,sizeof(data),1,temp);
>> break;
>> case 'E':
>> gotoxy(13,21);
>> printf("E-mail: ");
>> gets(data.email);
>> fwrite(&data,sizeof(data),1,temp);
>> break;
>>
>>
>> default:
>> gotoxy(13,21);
>> puts("Press S, G, D, A, P or E only");
>> getch();
>> gotoxy(13,20);
>> printf(" ");
>> gotoxy(13,21);
>> printf(" ");
>> break;
>> }
>> }
>>
>> while(ans !='S' && ans !='G' && ans !='D' && ans !='A' && ans!='P' &&
ans!='E');
>> }
>>
>> fwrite(&data,sizeof(data),1,temp);
>> x=fread(&data,sizeof(data),1,particulars);
>> pause(1);
>> }
>>
>> fclose(temp);
>> fclose(particulars);
>> remove("Particul.dat");
>> rename("temp.dat","particulars.dat");
>>
>>}
>>void Del(void)
>>{
>> FILE *particulars,*temp;
>>
>> int x;
>> char input[25],ans;
>>
>> particulars=fopen("Particul.dat","r");
>> if(particulars==NULL)
>> {
>> puts("Error opening file");
>> }
>> temp=fopen("temp.dat","w");
>> if(temp==NULL)
>> {
>> puts("Error opening file");
>> }
>> menu();
>> gotoxy(13,9);
>> printf("Surname to Delete: ");
>> gets(input);
>> x=fread(&data,sizeof(data),1,particulars);
>> while(x!=0)
>> {
>> if(!strcmp(input,data.surname))
>> {
>> gotoxy(13,11);
>> printf("Surname: %s",data.surname);
>> gotoxy(13,13);
>> printf("Given Name: %s",data.given_name);
>> gotoxy(13,15);
>> printf("Date of birth date: %d",data.birth.day);
>> gotoxy(38,13);
>> printf("month: %d",data.birth.month);
>> gotoxy(49,13);
>> printf("year: %d",data.birth.year);
>> gotoxy(13,17);
>> printf("Address: %s",data.address);
>> gotoxy(13,19);
>> printf("Phone Number: %s",data.phone);
>> gotoxy(13,21);
>> printf("E-mail Address: %s",data.email);
>>do
>> {
>> gotoxy(13,23);
>> printf("Confirm Delete (Y/N) : ");
>> ans=toupper(getchar());
>> fflush(stdin);
>> if (ans !='Y' && ans !='N')
>> { gotoxy(13,24);
>> puts("Press Y or N only");
>> getch();
>> gotoxy(13,23);
>> printf(" ");
>> gotoxy(13,24);
>> printf(" ");
>> }
>> }
>>while (ans!='Y');
>> x=fread(&data,sizeof(data),1,particulars);
>> }
>> if(strcmp(input,data.surname))
>> { fwrite(&data,sizeof(data),1,temp);
>> x=fread(&data,sizeof(data),1,particulars);
>> }
>> pause(1);
>> }
>>
>> fclose(temp);
>> fclose(particulars);
>> remove("Particul.dat");
>> rename("temp.dat","Particul.dat");
>> }
>>
>>
>
-
Re: C Nested Data Structure
Hi Zoe,
I'm not giving up on this.I have typed some of your program into turbo c++
which is what i use and found several problems .On my machine your program
would not complile.It gave about 9 errors...
Ok .i have made some changes as below and the printing of the date string
is now ok.
Add and change the lines indicated below.
Struct birthdate
{
int day;
int month;
int year;
};
struct birthdate birth /* NEW LINE ADDED TO YOUR PROGRAM*/
struct particulars
{
Char surname[MED_CHRS];
char given_name[MED_CHRS];
birthdate birth /* REMOVE THIS LINE FROM YOUR PROGRAM*/
char address[MAX_CHRS];
char phone[MED_CHRS];
char email[MAX_CHRS];
};
printf("Date of birth date;");
scanf("%d",&birth.day); /* Remove the "data." from your program*/
gotoxy(38,13);
printf("month: ");
scanf("%d",&birth.month); /* Remove the "data." from your program */
gotoxy(49,13);
printf("year;");
scanf("%d",&birth.year); /* Remove the "data." from your program */
printf("Date of birth date :%d",birth.day); /* Remove "data." */
gotoxy(38,13)
printf("month;;%d",birth.month); /* Remove "data." */
gotoxy(49,13);
printf("year; %d",birth.year); /*Remove "data." */
These are the changes required.Should be ok now !
Let me know how you get on.Best of luck.
Cheers,
Scott..
"Zoenaedy" <zoen@lycosasia.com> wrote:
>
>Thanks for the advise, but when I use 'char' there is an overflow of the
address
>string on the date field, ie
>
>date: 12 month:1 year: 1974Los Angeles
>address: Los Angeles
>
>The same thing happen when I removed the integer size.
>
>NB: Sorry, I don't know about serial communications.
>
>"Scott" <scott.rose@nmb.co.uk> wrote:
>>
>>"Zoenaedy" <zoen@lycosasia.com> wrote:
>>>Hi there,
>>
>>If you want to keep the date information as intergers remove the [2] from
>>Day and month, and [4] from Year.This is not required for intergers and
>does
>>not show up as an error during compliling.So you should now have.
>>
>>int day;
>>int month;
>>int year;
>> try that first.
>>
>>The other option is to change these 3 inputs to character arrays like
>>char day[2];
>>char month[2];
>>char year[4];
>>
>>and print out using printf("%s",data.birth.year);
>>
>>I have not tried any of these on a pc but these suggestions should help
>you...
>>
>>Cheers,
>> Scott..
>>
>>PS. you don't know anything about serial communications do you ?
>>
>>
>>
>>
>>
>>>Hi I have a small database program which works well, except that it gives
>>>a weird number when I display the 'date of birth' with the printf function.
>>>I use a nested data structure to create the main structure (Particular).
>>>I have debugged it again and again, but I couldn't find anything wrong.
>>Can
>>>anyone help me?
>>>
>>>#include <stdio.h>
>>>#include <stdlib.h>
>>>#include <conio.h>
>>>#include <ctype.h>
>>>#include <string.h>
>>>#include <dos.h>
>>>
>>>#define MAX_CHRS 30
>>>#define MED_CHRS 20
>>>#define MIN_CHRS 10
>>>#define PASS_CHRS 9
>>>
>>>struct login {
>>> char id[PASS_CHRS];
>>> char password[PASS_CHRS];
>>> };
>>>struct login user;
>>>
>>>struct birthdate {
>>> int day[2];
>>> int month[2];
>>> int year[4];
>>> };
>>>
>>>struct particulars {
>>> char surname[MED_CHRS];
>>> char given_name[MED_CHRS];
>>> birthdate birth;
>>> char address[MAX_CHRS];
>>> char phone[MED_CHRS];
>>> char email[MAX_CHRS];
>>>
>>> };
>>>
>>>struct particulars data;
>>>
>>>
>>>
>>>void Add(void);
>>>void Del(void);
>>>void Update(void);
>>>void Search(void);
>>>void Display(void);
>>>void pause(int);
>>>void menu(void);
>>>
>>>
>>>void main(void) /************************************ start of Password
>>>***/
>>>{
>>>
>>> char Password[9];
>>> char input[9],ans;
>>> int ok=1,root,x,choice;
>>>
>>>
>>> do
>>> {
>>> menu();
>>> gotoxy(23,8);
>>> printf("L O G I N M E N U");
>>> int count;
>>> gotoxy(23,12);
>>> printf("ENTER LOGIN ID: ");
>>>
>>> gotoxy(23,14);
>>> printf("ENTER PASSWORD: ");
>>> gotoxy(39,12);
>>> gets(input);
>>> gotoxy(39,14);
>>>
>>> for(count=0;count<80;count++){
>>> Password[count]=getch();
>>> printf("*");
>>> if(Password[count] =='\r') break;
>>> }
>>>
>>> Password[count]=NULL;
>>> if(!strcmp(input,"zoe") && !strcmp(Password,"1111"))
>>> { gotoxy(25,20);
>>> puts("*** Access Granted ***");
>>> pause(1);
>>> ok=0;
>>> root=1;
>>> }
>>> else
>>> { gotoxy(25,20);
>>> puts("*** Invalid Password ***");
>>>
>>> }
>>>
>>>
>>>
>>> }
>>> while(ok!=0);
>>> do
>>> {
>>> menu();
>>> gotoxy(28,6);
>>> printf("MAIN MENU");
>>> gotoxy(13,9);
>>> printf("1) Add New Record");
>>> gotoxy(13,11);
>>> printf("2) Delete Record");
>>> gotoxy(13,13);
>>> printf("3) Update Record");
>>> gotoxy(13,15);
>>> printf("4) Search Record");
>>> gotoxy(13,17);
>>> printf("5) Display Records");
>>> gotoxy(13,19);
>>> printf("6) Exit ");
>>> gotoxy(13,22);
>>> printf("Enter your Option: ");
>>>
>>> choice=getchar();
>>> fflush(stdin);
>>> switch(choice)
>>> {
>>> case '1':
>>> Add();
>>> break;
>>>
>>> case '2':
>>> Del();
>>> break;
>>>
>>> case '3':
>>> Update();
>>> break;
>>>
>>> case '4':
>>> Search();
>>> break;
>>>
>>> case '5':
>>> Display();
>>> break;
>>>
>>> case '6':
>>> gotoxy(12,24);
>>> printf("See You!");
>>> pause(1);
>>> break;
>>>
>>> default:
>>> gotoxy(32,24);
>>> puts("Invalid Choice...");
>>> gotoxy(32,25);
>>> puts("<Enter 1,2,3,4,5,6 only>");
>>>
>>>
>>> }
>>> }
>>> while(choice!='6');
>>>
>>>}
>>>
>>> /*** end of Password Check ***/
>>>
>>>/**** M E N U ****/
>>>
>>> void menu(void) /************************************ start of Main
>>Menu
>>>***/
>>> {
>>> int i;
>>> clrscr();
>>> printf("\xDA");
>>> for(i=0;i<=70;i++)
>>> printf("\xC4");
>>> printf("\xBF\n");
>>> gotoxy(1,2);
>>> printf("\xB3");
>>> gotoxy(23,2);
>>> printf("WELCOME TO ZOE'S ADDRESS BOOK");
>>> gotoxy(73,2);
>>> printf("\xB3\n");
>>> printf("\xC3");
>>> for(i=0;i<=70;i++)
>>> printf("\xC4");
>>> printf("\xB4\n");
>>>
>>> for(i=3;i<=23;i++)
>>> {
>>> gotoxy(1,i);
>>> printf("\xB3");
>>> gotoxy(73,i);
>>> printf("\xB3");
>>> }
>>> printf("\n\xC0");
>>> for(i=0;i<=70;i++)
>>> printf("\xC4");
>>> printf("\xD9\n");
>>>
>>> }
>>>
>>>void pause(int seconds)
>>> {
>>> delay(seconds*1000); //Borland compilers
>>> }
>>>
>>>
>>>/**** PARTICULARS ****/
>>>void Add(void)
>>>{ FILE *particulars;
>>>
>>> char input[12],ans;
>>>
>>> particulars=fopen("Particul.dat","a");
>>> if(particulars==NULL)
>>> {
>>> puts("Error opening file");
>>> }
>>>
>>>
>>>
>>>do
>>>{
>>> menu();
>>> gotoxy(13,9);
>>> printf("Surname: ");
>>> scanf("%s",&data.surname);
>>> gotoxy(13,11);
>>> printf("Given Name: ");
>>> scanf("%s",&data.given_name);
>>> gotoxy(13,13);
>>> printf("Date of birth date: ");
>>> scanf("%d",&data.birth.day);
>>> gotoxy(38,13);
>>> printf("month: ");
>>> scanf("%d",&data.birth.month);
>>> gotoxy(49,13);
>>> printf("year: ");
>>> scanf("%d",&data.birth.year);
>>> gotoxy(13,15);
>>> printf("Address: ");
>>> scanf("%s",&data.address);
>>> gotoxy(13,17);
>>> printf("Phone number: ");
>>> scanf("%s",&data.phone);
>>> gotoxy(13,19);
>>> printf("E-mail address: ");
>>> scanf("%s",&data.email);
>>>
>>>
>>>
>>> fwrite(&data,sizeof(data),1,particulars);
>>>
>>> choice:
>>> gotoxy(13,21);
>>> printf("Another entry (y/n) : ");
>>> ans=toupper(getchar());
>>> fflush(stdin);
>>> if (ans !='Y' && ans !='N')
>>> { gotoxy(13,22);
>>> puts("Press Y or N only");
>>> getch();
>>> gotoxy(13,21);
>>> printf(" ");
>>> gotoxy(13,22);
>>> printf(" ");
>>> goto choice;
>>>
>>> }
>>>}
>>>
>>> while (ans != 'N' );
>>> fclose(particulars);
>>>} /* end of add */
>>>
>>>void Display(void) /**** start of Display Module ****/
>>>{
>>> FILE *particulars;
>>>
>>> int x;
>>> particulars=fopen("Particul.dat","r");
>>> if(particulars==NULL)
>>> {
>>> puts("Error opening file");
>>> }
>>>
>>> x=fread(&data,sizeof(data),1,particulars);
>>> while(x!=0)
>>> {
>>> menu();
>>> gotoxy(13,9);
>>> printf("Surname: %s",data.surname);
>>> gotoxy(13,11);
>>> printf("Given Name: %s",data.given_name);
>>> gotoxy(13,13);
>>> printf("Date of birth date: %d",data.birth.day);
>>> gotoxy(38,13);
>>> printf("month: %d",data.birth.month);
>>> gotoxy(49,13);
>>> printf("year: %d",data.birth.year);
>>> gotoxy(13,15);
>>> printf("Address: %s",data.address);
>>> gotoxy(13,17);
>>> printf("Phone Number: %s",data.phone);
>>> gotoxy(13,19);
>>> printf("E-mail Address: %s",data.email);
>>> gotoxy(13,22);
>>> printf("Press <Enter> to go to the next record");
>>> getch();
>>> x=fread(&data,sizeof(data),1,particulars);
>>>
>>>}
>>>fclose(particulars);
>>>} /*** end of display ***/
>>>
>>>void Search(void)
>>>{
>>> FILE *particulars;
>>>
>>> int x;
>>> char input[25];
>>>
>>> particulars=fopen("Particul.dat","r");
>>> if(particulars==NULL)
>>> {
>>> puts("Error opening file");
>>> }
>>> menu();
>>> gotoxy(13,9);
>>> printf("Surname to search: ");
>>> gets(input);
>>> x=fread(&data,sizeof(data),1,particulars);
>>> while(x!=0)
>>> {
>>> if(!strcmp(input,data.surname))
>>> {
>>> menu();
>>>
>>> gotoxy(13,9);
>>> printf("Surname: %s",data.surname);
>>> gotoxy(13,11);
>>> printf("Given Name: %s",data.given_name);
>>> gotoxy(13,13);
>>> printf("Date of birth date: %d",data.birth.day);
>>> gotoxy(38,13);
>>> printf("month: %d",data.birth.month);
>>> gotoxy(49,13);
>>> printf("year: %d",data.birth.year);
>>> gotoxy(13,15);
>>> printf("Address: %s",data.address);
>>> gotoxy(13,17);
>>> printf("Phone Number: %s",data.phone);
>>> gotoxy(13,19);
>>> printf("E-mail Address: %s",data.email);
>>> }
>>> getch();
>>> x=fread(&data,sizeof(data),1,particulars);
>>>
>>>}
>>>fclose(particulars);
>>>}
>>>void Update(void)
>>>{
>>> FILE *particulars,*temp;
>>>
>>> int x;
>>> char input[25],ans;
>>>
>>> particulars=fopen("Particul.dat","r");
>>> if(particulars==NULL)
>>> {
>>> puts("Error opening file");
>>> }
>>> temp=fopen("temp.dat","w");
>>> if(temp==NULL)
>>> {
>>> puts("Error opening file");
>>> }
>>> menu();
>>> gotoxy(13,9);
>>> printf("Surname to edit: ");
>>> gets(input);
>>> x=fread(&data,sizeof(data),1,particulars);
>>> while(x!=0)
>>> {
>>> if(!strcmp(input,data.surname))
>>> {
>>> menu();
>>> gotoxy(13,9);
>>> printf("Surname: %s",data.surname);
>>> gotoxy(13,11);
>>> printf("Given Name: %s",data.given_name);
>>> gotoxy(13,13);
>>> printf("Date of birth date: %d",data.birth.day);
>>> gotoxy(38,13);
>>> printf("month: %d",data.birth.month);
>>> gotoxy(49,13);
>>> printf("year: %d",data.birth.year);
>>> gotoxy(13,15);
>>> printf("Address: %s",data.address);
>>> gotoxy(13,17);
>>> printf("Phone Number: %s",data.phone);
>>> gotoxy(13,19);
>>> printf("E-mail Address: %s",data.email);
>>> do
>>> {
>>> menu();
>>> gotoxy(13,9);
>>> printf("Edit <S>urname");
>>> gotoxy(13,11);
>>> printf(" <G>iven Name");
>>> gotoxy(13,13);
>>> printf(" <D>ate of birth");
>>> gotoxy(13,15);
>>> printf(" <A>ddress");
>>> gotoxy(13,17);
>>> printf(" <P>hone number");
>>> gotoxy(13,19);
>>> printf(" <E>mail");
>>> ans=toupper(getchar());
>>> fflush(stdin);
>>> switch(ans)
>>> {
>>> case 'S':
>>> gotoxy(13,21);
>>> printf("Surname: ");
>>> gets(data.surname);
>>> fwrite(&data,sizeof(data),1,temp);
>>> break;
>>> case 'G':
>>> gotoxy(13,21);
>>> printf("Given Name: ");
>>> gets(data.given_name);
>>> fwrite(&data,sizeof(data),1,temp);
>>> break;
>>> case 'D':
>>> gotoxy(13,21);
>>> printf("Date of birth date: ");
>>> scanf("%d",data.birth.day);
>>> gotoxy(38,21);
>>> printf("month: ");
>>> scanf("%d",data.birth.month);
>>> gotoxy(49,21);
>>> printf("year: ");
>>> scanf("%d",data.birth.year);
>>> fwrite(&data,sizeof(data),1,temp);
>>> break;
>>> case 'A':
>>> gotoxy(13,21);
>>> printf("Address: ");
>>> gets(data.address);
>>> fwrite(&data,sizeof(data),1,temp);
>>> break;
>>> case 'P':
>>> gotoxy(13,21);
>>> printf("Phone: ");
>>> gets(data.phone);
>>> fwrite(&data,sizeof(data),1,temp);
>>> break;
>>> case 'E':
>>> gotoxy(13,21);
>>> printf("E-mail: ");
>>> gets(data.email);
>>> fwrite(&data,sizeof(data),1,temp);
>>> break;
>>>
>>>
>>> default:
>>> gotoxy(13,21);
>>> puts("Press S, G, D, A, P or E only");
>>> getch();
>>> gotoxy(13,20);
>>> printf(" ");
>>> gotoxy(13,21);
>>> printf(" ");
>>> break;
>>> }
>>> }
>>>
>>> while(ans !='S' && ans !='G' && ans !='D' && ans !='A' && ans!='P' &&
>ans!='E');
>>> }
>>>
>>> fwrite(&data,sizeof(data),1,temp);
>>> x=fread(&data,sizeof(data),1,particulars);
>>> pause(1);
>>> }
>>>
>>> fclose(temp);
>>> fclose(particulars);
>>> remove("Particul.dat");
>>> rename("temp.dat","particulars.dat");
>>>
>>>}
>>>void Del(void)
>>>{
>>> FILE *particulars,*temp;
>>>
>>> int x;
>>> char input[25],ans;
>>>
>>> particulars=fopen("Particul.dat","r");
>>> if(particulars==NULL)
>>> {
>>> puts("Error opening file");
>>> }
>>> temp=fopen("temp.dat","w");
>>> if(temp==NULL)
>>> {
>>> puts("Error opening file");
>>> }
>>> menu();
>>> gotoxy(13,9);
>>> printf("Surname to Delete: ");
>>> gets(input);
>>> x=fread(&data,sizeof(data),1,particulars);
>>> while(x!=0)
>>> {
>>> if(!strcmp(input,data.surname))
>>> {
>>> gotoxy(13,11);
>>> printf("Surname: %s",data.surname);
>>> gotoxy(13,13);
>>> printf("Given Name: %s",data.given_name);
>>> gotoxy(13,15);
>>> printf("Date of birth date: %d",data.birth.day);
>>> gotoxy(38,13);
>>> printf("month: %d",data.birth.month);
>>> gotoxy(49,13);
>>> printf("year: %d",data.birth.year);
>>> gotoxy(13,17);
>>> printf("Address: %s",data.address);
>>> gotoxy(13,19);
>>> printf("Phone Number: %s",data.phone);
>>> gotoxy(13,21);
>>> printf("E-mail Address: %s",data.email);
>>>do
>>> {
>>> gotoxy(13,23);
>>> printf("Confirm Delete (Y/N) : ");
>>> ans=toupper(getchar());
>>> fflush(stdin);
>>> if (ans !='Y' && ans !='N')
>>> { gotoxy(13,24);
>>> puts("Press Y or N only");
>>> getch();
>>> gotoxy(13,23);
>>> printf(" ");
>>> gotoxy(13,24);
>>> printf(" ");
>>> }
>>> }
>>>while (ans!='Y');
>>> x=fread(&data,sizeof(data),1,particulars);
>>> }
>>> if(strcmp(input,data.surname))
>>> { fwrite(&data,sizeof(data),1,temp);
>>> x=fread(&data,sizeof(data),1,particulars);
>>> }
>>> pause(1);
>>> }
>>>
>>> fclose(temp);
>>> fclose(particulars);
>>> remove("Particul.dat");
>>> rename("temp.dat","Particul.dat");
>>> }
>>>
>>>
>>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks