Code:
#include <stdio.h>
#include <string.h>
#define SIZE 30
typedef struct
{
char firstname[30],lastname[30],ssno[30],city[30],state[30],year[30],sex[30];
}student_record;
/*Function Prototypes*/
void initialize(student_record DB[30],int &count);
int IsEmpty(int); /*prototype for IsEmpty*/
int IsFull(int) ; /*prototype for isFull*/
void search(student_record DB[30],int count) ; /* prototype for search*/
void print(student_record DB[30], int count) ; /*prototype for print*/
void insert(student_record DB[30],int *count) ; /* for insert*/
void remove (student_record DB[30],int *count) ; /* prototype for remove*/
/********************************************************
*********************************************************/
void initialize(student_record database[30],int &count)
{
FILE *input;
char temp_str[30];
input = fopen("mydata.txt","r"); /*open file*/
while(!feof(input) && count<SIZE)
{
fscanf(input,"%s",temp_str);
strcpy(database[30].lastname,temp_str);
fscanf(input,"%s",temp_str);
strcpy(database[30].firstname,temp_str);
fscanf(input,"%lf",temp_str);
strcpy(database[30].ssno,temp_str);
fscanf(input,"%s",temp_str);
strcpy(database[30].city,temp_str);
fscanf(input,"%s",temp_str);
strcpy(database[30].state,temp_str);
fscanf(input,"%lf",temp_str);
strcpy(database[30].year,temp_str);
fscanf(input,"%s",temp_str);
strcpy(database[30].sex,temp_str);
fclose(input);
}
}
/********************************************************
*********************************************************/
int Is_Empty(int count)
{
return count==0;
}
/********************************************************
*********************************************************/
int Is_Full(int count)
{
return count==SIZE;
}
/********************************************************
*********************************************************/
int search(student_record DB[], int count, char key[])
{
int i;
for(i=0; i<count; i++)
if(strcpy(key,DB[i].firstname,DB[i].lastname)==0)
return i;
return -1;
}
/********************************************************
*********************************************************/
void print(student_record DB[], int count)
{
int i=0;
while(i<count)
{
printf("%S %s %lf %s %s %lf %c \n",DB[i].lastname,DB[i].firstname,DB[i].ssno,DB[i].city,DB[i].state,DB[i].sex);
i++;
}
/********************************************************
*********************************************************/
void remove_record(student_record DB[], int * count, char key[])
{
int i = search(DB, *count, key); //***
if (i != -1)
{
((*count)--);
for(; i<*count; i++)
DB[i] = DB[i+1];
}
}
/********************************************************
*********************************************************/
void insert(student_record DB[], int * count)
{ if(*count < SIZE) // ****
{
printf("Enter first name: ");
scanf("%s", &DB[*count].firstname);
printf("Enter last name: ");
scanf("%s", &DB[*count].lastname);
printf("Enter ssno: ");
scanf("%lf", DB[*count].ssno);
printf("Enter city: ");
scanf("%s", DB[*count].city);
printf("Enter state: ");
scanf("%s", DB[*count].state);
printf("Enter year: ");
scanf("%lf", DB[*count].year);
(*count)++;
}
else
printf("Cannot add item: database is full.");
}
int main()
{
student_record database[SIZE]; //database for student records
char command; //holds the command to execute
int count=0; //contains count of actual number of items in database
initialize(database,count);
printf("Enter a command: s=search, p=print, r=remove,i=insert, e=exit\n");
scanf(" %c", &command);
while (command!='e')
switch (command)
case 's' :printf("Enter s if you want to search");
search() ;
break;
case 'p' : printf("Enter p if you want to print");
print_array();
break;
case 'r': printf("Enter r if you want to remove");
remove_record ();
break;
case 'i': printf("Enter i if you want to insert");
insert();
break;
printf("Enter acommand:s=search,p=print,r=remove,i=insert,e=exit\n");
scanf(" &c", &command);
return 0;
}
}
any help would be appreciated