i have this program on C, can you check is this is correct???

#include
#include
#include
#define max 30
#define min 10
typedef char fname[max];
typedef char mname[max];
typedef char lname[max];
typedef char i_d[min];
struct student_record
{
i_d id;
fname firstname;
mname middlename;
lname lastname;
char age[min];
char birthdate[max];
char address[max];
char course[max];
char year[min];
char section[min];

struct student_record *next; /* pointer to next entry */
struct student_record *prior; /* pointer to previous record */
};

struct student_record *start; /* pointer to first entry in list */
struct student_record *last; /* pointer to last entry */
struct student_record *find(char *);

void enter(void), search(void), save(void);
void load(void), list(void);
void mldelete(struct student_record **, struct student_record **);
void dls_store(struct student_record *i, struct student_record **start,
struct student_record **last);
void inputs(char *, char *, int), display(struct student_record *);
int menu_select(void);

int main(void)
{
start = last = NULL; /* initialize start and end pointers */
clrscr();

for(;;) {
switch(menu_select()) {
case 1: enter(); /* enter an address */
break;
case 2: mldelete(&start, &last); /* remove an address */
break;
case 3: search(); /* search */
break;
case 4: list(); /* display the list */
break;
case 5: save(); /* save list to disk */
break;
case 6: load(); /* read from disk */
break;
case 7: clrscr();
case 8: exit(0);
}
}
return 0;
}

/* Select an operation. */
int menu_select(void)
{
char s[80];
int c;

printf("1. Add record
");
printf("2. Delete record
");
printf("3. Search record
");
printf("4. List the file
");
printf("5. Save the file
");
printf("6. Load the file
");
printf("7. Clear screen
");
printf("8. Quit
");
do {
printf("
Enter your choice: ");
gets(s);
c = atoi(s);
} while(c<0 || c>7);
return c;
}

/* Enter names and addresses. */
void enter(void)
{
struct student_record *info;

for(;;) {
info = (struct student_record *)malloc(sizeof(struct student_record));
if(!info) {
printf("
out of memory");
return;
}

inputs("Enter id: ", info->id, min);
if(!info->id[0]) break; /* stop entering */
inputs("Enter first name: ", info->firstname, max);
inputs("Enter middle name: ", info->middlename, max);
inputs("Enter last name: ", info->lastname, max);
inputs("Enter age: ", info->age, min);
inputs("Enter birth date: ", info->birthdate, max);
inputs("Enter address: ", info->address, max);
inputs("Enter course: ", info->course, max);
inputs("Enter year: ", info->year, min);
inputs("Enter section: ", info->section, min);

dls_store(info, &start, &last);
} /* entry loop */
}

/* This function will input a string up to
the length in count and will prevent
the string from being overrun. It will also
display a prompting message. */
void inputs(char *prompt, char *s, int count)
{
char p[255];

do {
printf(prompt);
fgets(p, 254, stdin);
if(strlen(p) > count) printf("
Too Long
");
} while(strlen(p) > count);

p[strlen(p)-1] = 0; /* remove newline character */
strcpy(s, p);
}

/* Create a doubly linked list in sorted order. */
void dls_store(
struct student_record *i, /* new element */
struct student_record **start, /* first element in list */
struct student_record **last /* last element in list */
)
{
struct student_record *old, *p;

if(*last==NULL) { /* first element in list */
i->next = NULL;
i->prior = NULL;
*last = i;
*start = i;
return;
}
p = *start; /* start at top of list */

old = NULL;
while(p) {
if(strcmp(p->id, i->id)<0){
old = p;
p = p->next;
}
else {
if(p->prior) {
p->prior->next = i;
i->next = p;
i->prior = p->prior;
p->prior = i;
return;
}
i->next = p; /* new first element */
i->prior = NULL;
p->prior = i;
*start = i;
return;
}
}
old->next = i; /* put on end */
i->next = NULL;
i->prior = old;
*last = i;
}

/* Remove an element from the list. */
void mldelete(struct student_record **start, struct student_record **last)
{
struct student_record *info;
char s[min];

inputs("Enter id: ", s, min);
info = find(s);
if(info) {
if(*start==info) {
*start=info->next;
if(*start) (*start)->prior = NULL;
else *last = NULL;
}
else {
info->prior->next = info->next;
if(info!=*last)
info->next->prior = info->prior;
else
*last = info->prior;
}
free(info); /* return memory to system */
}
}

/* Find an address. */
struct student_record *find( char *id)
{
struct student_record *info;

info = start;
while(info) {
if(!strcmp(id, info->id)) return info;
info = info->next; /* get next id */
}
printf("ID not found.
");
return NULL; /* not found */
}

/* Display the entire list. */
void list(void)
{
struct student_record *info;

info = start;
while(info) {
display(info);
info = info->next; /* get next address */
}
printf("

");
}

/* This function actually prints the fields in each address. */
void display(struct student_record *info)
{
printf("

");
printf("ID: %s ", info->id);
printf("FIRST NAME: %s ", info->firstname);
printf("MIDDLE NAME: %s ", info->middlename);
printf("LAST NAME: %s ", info->lastname);
printf("AGE: %s ", info->age);
printf("BIRTH DATE: %s ", info->birthdate);
printf("ADDRESS: %s ", info->address);
printf("COURSE: %s ", info->course);
printf("YEAR: %s ", info->year);
printf("SECTION: %s ", info->section);

printf("

");
}

/* Look for a name in the list. */
void search(void)
{
char id[min];
struct student_record *info;

printf("Enter id to find: ");
gets(id);
info = find(id);
if(!info) printf("Not Found
");
else display(info);
}

/* Save the file to disk. */
void save(void)
{
struct student_record *info;

FILE *fp;

fp = fopen("mlist", "wb");
if(!fp) {
printf("Cannot open file.
");
exit(1);
}
printf("
Saving File
");

info = start;
while(info) {
fwrite(info, sizeof(struct student_record), 1, fp);
info = info->next; /* get next record */
}
fclose(fp);
}

/* Load the address file. */
void load()
{
struct student_record *info;
FILE *fp;

fp = fopen("mlist", "rb");
if(!fp) {
printf("Cannot open file.
");
exit(1);
}

/* free any previously allocated memory */
while(start) {
info = start->next;
free(info);
start = info;
}

/* reset top and bottom pointers */
start = last = NULL;

printf("
Loading File
");
while(!feof(fp)) {
info = (struct student_record *) malloc(sizeof(struct student_record));
if(!info) {
printf("Out of Memory");
return;
}
if(1 != fread(info, sizeof(struct student_record), 1, fp)) break;
dls_store(info, &start, &last);
}
fclose(fp);
}

Comments

  • >>i have this program on C, can you check is this is correct???

    I have no idea, too much badly formatted code to read. edit your post and put it in code tags to make it readable.
    [code]
    [ code ]
    // your code here
    [ /code ]
    [/code]

    just leave out the spaces in the above


    [b]Next[/b]: compile and run your program. Does it do what you want it to do? If no, then it is not correct. Don't ask other people to do your work for you.





  • VC++6 Pro. Compiler :

    [code]
    Compiling...
    Line 4: warning C4005: 'max' : macro redefinition
    .includestdlib.h : Line 427: see previous definition of 'max'
    Line 5: warning C4005: 'min' : macro redefinition
    .includestdlib.h : Line 428: see previous definition of 'min'
    Line : 42: warning C4013: 'clrscr' undefined; assuming extern returning int
    Line : 126: warning C4018: '>' : signed/unsigned mismatch
    Line : 127: warning C4018: '>' : signed/unsigned mismatch
    Linking...
    .obj : error LNK2001: unresolved external symbol _clrscr
    .exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    .exe - 2 error(s), 5 warning(s)
    [/code]

    You can use 'system( "cls" );' command instead of 'clrscr();' command. ('system' function is declared in or
    ).

    When I used 'system' function, your program was built, and I could run it.
  • [b][red]This message was edited by stober at 2006-9-30 9:1:47[/red][/b][hr]
    Of course he could have discovered by all by himself had he bothered to compile it :-)


  • it is compile in turboC, but the saving record and loading record are not working... but if i search the record i can search it...



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories