• C

Address Book - somewhat challenging

I wrote the code below which is an address book.  It asks the following questions: to search the addressbook, add, list, delete, print the entire address book, and quit.  Can someone look it over since I am getting multiple errors and warnings?  Also, just assume that you have the "addressbook.txt" file on your computer since there is coding where it opens that specific files.

------------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXSTREET 100
#define MAXCITY 20
#define MAXSTATE 2
#define MAXZIP 5
#define MAXTEL 20
#define MAXE 100

struct address {
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

  struct address *prev;
  struct address *next;
};

typedef struct address Address;
typedef Address *Aptr;

Aptr head=NULL, tail=NULL;

Aptr goToHead(Aptr spot) {

  while(spot.prev != head) {
    spot = spot.prev;
  }
  return spot;
}

Aptr makeNode(char *lname, char *fname, char *str, char *ci, char *st, char *zip, char *tel, char *cell, char *email) {
  Aptr new;

  new = (Aptr)(malloc(sizeof(Address)));
  /*Not sure if I have to malloc each one..
new->lastname = (char *)(malloc(sizeof(char) * MAXNAME));
  */
  strcpy(new->lastname,lname);
  strcpy(new->firstname,fname);
  strcpy(new->street,str);
  strcpy(new->city,ci);
  strcpy(new->state,st);
  strcpy(new->zip,zip);
  strcpy(new->tel,tel);
  strcpy(new->cell,cell);
  strcpy(new->email,email);
 
  new->prev = NULL;
  new->next = NULL;

  return new;
}

Aptr insert(Aptr node, Aptr list) {

  if(list == NULL) {
    list = node;
    return list;
  }

  if(strcmp(node->lastname, list->lastname) < 0) {
    node->prev = head;
    node->next = list;
    list->prev = node;
    return goToHead(list);
  }

  while(list->next != NULL) {
    if(strcmp(node->lastname, list->next->lastname) < -1) {
      node->next = list->next;
      node->prev = list;
      list->next = node;
      list->next->prev = node;
      return goToHead(list);
    }
    else {
      list = list->next;
    }
  }
  list->next = node;
  node->prev = list;
  node->next = tail;
  return goToHead(list);
}
}


Aptr readFile() {
  FILE *fp;
  fp = fopen("addressbook.txt","r");
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  Aptr list;
  Aptr new;

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list == NULL;

  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return list;
    }
  }

}

void search(Aptr list) {

  char *lname = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter last name:  ");
  gets(lname);

  while(list != NULL) {
    if(strstr(list->lastname, lname) != NULL) {
      print_node(list);
    }
    list = list->next;
  }

}

void add(Aptr list) {
  Aptr new;
  new = MakeNode();
  /*
Prompting user for new info
  */
  printf("enter lastname: ");
  gets(new->lastname);
  printf("enter firstname: ");
  gets(new->firstname);
  printf("enter street: ");
  gets(new->street);
  printf("enter city: ");
  gets(new->city);
  printf("enter state: ");
  gets(new->state);
  printf("enter zip: ");
  gets(new->zip);
  printf("enter tel: ");
  gets(new->tel);
  printf("enter cell: ");
  gets(new->cell);
  printf("enter email: ");
  gets(new->email);

  list = insert(new, list);
}

void list(Aptr list) {

  Aptr temp = list;
  while(temp != NULL) {
    printf("%s %s\n",temp->firstname, temp->lastname);
  }
}

void delete(Aptr list) {
  Aptr temp = list;
  char c;
  char *delName = (char *)(malloc(sizeof(char) * MAXNAME));

  printf("Enter last name:  ");
  gets(delName);

  while(temp != NULL) {
    if(strstr(temp->lastname, delName) != NULL) {
      print_node(temp);
      printf("Are you sure you want to delete? Y or N:  ");
      c = getchar();
      if(c == 'y') {
      if(temp->prev == head && temp->next == tail) {
        temp = NULL;
      }
      else if(temp->prev = head) {
        temp->next->prev = head;
      }
      else if(temp->next = tail) {
        temp->prev->next = tail;
      }
      else {
        temp->prev->next = temp->next->prev;
        temp->next->prev = temp->prev->next;
      }
      }
    }
    temp = temp->next;
  }

}



void print(Aptr list) {
  temp = list;
  while(temp != NULL) {
    print_node(temp);
    temp = temp->next;
  }
}

void print_node(Aptr temp) {

  printf("%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}

void print_to_file(Aptr list) {
  FILE *fp;
  Aptr temp;
  char *fileName = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter filename:  ");
  gets(fileName);
  fp = fopen(fileName,"w");
  temp = list;
  while(temp != NULL) {
    print_node_to_file(temp);
    temp = temp->next;
  }
  fclose(fp);
}

void print_node_to_file(Aptr temp, FILE *fp) {

  fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



int main() {

  char input;

printf("Welcome to Online Address Book!\n"
         "Enter s to search for an entry...\n"
         "Enter a to add an entry...\n"
         "Enter l to list an entry...\n"
         "Enter d to delete an entry...\n"
         "Enter p to print the address book to a file...\n"
         "Enter q to quit.\n");


  Aptr list = (Aptr)(malloc(sizeof(Address)));
  list = readFile();

  while(1) {

    printf("Enter your choice:  ");
    input = getchar();
    switch(input) {
    case 's':
      search(list);
      break;
    case 'a':
      add(list);
      break;
    case 'l':
      list(list);
      break;
    case 'd':
      delete(list);
      break;
    case 'p':
      print(list);
      break;
    case 'q':
      exit(0);
    default:
      printf("Huh?");
    }

  }

}

cooldeanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Hi cooldean,

How about posting some of the errors and warning?  It's a lot easier to decipher the compiler messages than to predict them.   :)


Kent
0
cooldeanAuthor Commented:
Yeah, good call.  Here are the errors I got when I tried to compile it.  11 errors and 21 warnings :(


--------------------Configuration: hw8p1 - Win32 Debug--------------------
Compiling...
hw8p1.c
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(37) : error C2231: '.prev' : left operand points to 'struct', use '->'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(38) : error C2231: '.prev' : left operand points to 'struct', use '->'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(97) : error C2059: syntax error : '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(150) : warning C4013: 'print_node' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(159) : warning C4013: 'MakeNode' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(159) : warning C4047: '=' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(230) : error C2065: 'temp' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(230) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(231) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(233) : error C2223: left of '->next' must point to struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(237) : error C2371: 'print_node' : redefinition; different basic types
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(252) : warning C4013: 'print_node_to_file' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(258) : error C2371: 'print_node_to_file' : redefinition; different basic types
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(279) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(31) : see declaration of 'Aptr'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(279) : error C2146: syntax error : missing ';' before identifier 'list'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(279) : warning C4047: '=' : 'void (__cdecl *)(struct address *)' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(279) : error C2106: '=' : left operand must be l-value
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(279) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(280) : warning C4013: 'readFile' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(280) : warning C4047: '=' : 'void (__cdecl *)(struct address *)' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(280) : error C2106: '=' : left operand must be l-value
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(280) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(288) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'void (__cdecl *)(struct address *)'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(288) : warning C4024: 'search' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(291) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'void (__cdecl *)(struct address *)'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(291) : warning C4024: 'add' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(294) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'void (__cdecl *)(struct address *)'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(294) : warning C4024: 'list' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(297) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'void (__cdecl *)(struct address *)'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(297) : warning C4024: 'delete' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(300) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'void (__cdecl *)(struct address *)'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(300) : warning C4024: 'print' : different types for formal and actual parameter 1
Error executing cl.exe.

hw8p1.obj - 11 error(s), 21 warning(s)
0
Kent OlsenData Warehouse Architect / DBACommented:

Breaking them down by type:

C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(37) : error C2231: '.prev' : left operand points to 'struct', use '->'

  while(spot.prev != head) {
    spot = spot.prev;
  }
  return spot;
}

should be:

   while (spot->prev != head) {


C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(97) : error C2059: syntax error : '}'

You probably have one too many '}' symbols.


C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(150) : warning C4013: 'print_node' undefined; assuming extern returning int

There is no function prototype for function print_node().  Or it is being referenced prior to its definition.  (Make a prototype.)


C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(230) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'

You're passing the wrong type of argument or the function differs from the prototype or implied usage.

Most of the errors seem to be parameter/usage errors.

Kent

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

cooldeanAuthor Commented:
for:  C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(97) : error C2059: syntax error : '}'

You probably have one too many '}' symbols.

---
I tried to remove one of the last '}' symbols and I got more errors than before...The revised code is below...still got 9 errors and 22 warnings...please help me out...last coding work for the year!
---

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXSTREET 100
#define MAXCITY 20
#define MAXSTATE 2
#define MAXZIP 5
#define MAXTEL 20
#define MAXE 100

struct address {
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

  struct address *prev;
  struct address *next;
};

typedef struct address Address;
typedef Address *Aptr;

Aptr head=NULL, tail=NULL;

Aptr goToHead(Aptr spot) {

  while(spot->prev != head) {
    spot = spot.prev;
  }
  return spot;
}

Aptr makeNode(char *lname, char *fname, char *str, char *ci, char *st, char *zip, char *tel, char *cell, char *email) {
  Aptr new;

  new = (Aptr)(malloc(sizeof(Address)));
  /*Not sure if I have to malloc each one..
new->lastname = (char *)(malloc(sizeof(char) * MAXNAME));
  */
  strcpy(new->lastname,lname);
  strcpy(new->firstname,fname);
  strcpy(new->street,str);
  strcpy(new->city,ci);
  strcpy(new->state,st);
  strcpy(new->zip,zip);
  strcpy(new->tel,tel);
  strcpy(new->cell,cell);
  strcpy(new->email,email);
 
  new->prev = NULL;
  new->next = NULL;

  return new;
}

Aptr insert(Aptr node, Aptr list) {

  if(list == NULL) {
    list = node;
    return list;
  }

  if(strcmp(node->lastname, list->lastname) < 0) {
    node->prev = head;
    node->next = list;
    list->prev = node;
    return goToHead(list);
  }

  while(list->next != NULL) {
    if(strcmp(node->lastname, list->next->lastname) < -1) {
      node->next = list->next;
      node->prev = list;
      list->next = node;
      list->next->prev = node;
      return goToHead(list);
    }
    else {
      list = list->next;
    }
  }
  list->next = node;
  node->prev = list;
  node->next = tail;
  return goToHead(list);
}
}


Aptr readFile() {
  FILE *fp;
  fp = fopen("addressbook.txt","r");
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  Aptr list;
  Aptr new;

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list == NULL;

  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return list;
    }
  }

}

void print_node(Aptr temp) {

  printf("%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



void search(Aptr list) {

  char *lname = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter last name:  ");
  gets(lname);

  while(list != NULL) {
    if(strstr(list->lastname, lname) != NULL) {
      print_node(list);
    }
    list = list->next;
  }

}

void add(Aptr list) {
  Aptr new;
  new = MakeNode();
  /*
Prompting user for new info
  */
  printf("enter lastname: ");
  gets(new->lastname);
  printf("enter firstname: ");
  gets(new->firstname);
  printf("enter street: ");
  gets(new->street);
  printf("enter city: ");
  gets(new->city);
  printf("enter state: ");
  gets(new->state);
  printf("enter zip: ");
  gets(new->zip);
  printf("enter tel: ");
  gets(new->tel);
  printf("enter cell: ");
  gets(new->cell);
  printf("enter email: ");
  gets(new->email);

  list = insert(new, list);
}

void list(Aptr list) {

  Aptr temp = list;
  while(temp != NULL) {
    printf("%s %s\n",temp->firstname, temp->lastname);
  }
}

void delete(Aptr list) {
  Aptr temp = list;
  char c;
  char *delName = (char *)(malloc(sizeof(char) * MAXNAME));

  printf("Enter last name:  ");
  gets(delName);

  while(temp != NULL) {
    if(strstr(temp->lastname, delName) != NULL) {
      print_node(temp);
      printf("Are you sure you want to delete? Y or N:  ");
      c = getchar();
      if(c == 'y') {
      if(temp->prev == head && temp->next == tail) {
        temp = NULL;
      }
      else if(temp->prev = head) {
        temp->next->prev = head;
      }
      else if(temp->next = tail) {
        temp->prev->next = tail;
      }
      else {
        temp->prev->next = temp->next->prev;
        temp->next->prev = temp->prev->next;
      }
      }
    }
    temp = temp->next;
  }

}



void print(Aptr list) {
  temp = list;
  while(temp != NULL) {
    print_node(temp);
    temp = temp->next;
  }
}

void print_to_file(Aptr list) {
  FILE *fp;
  Aptr temp;
  char *fileName = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter filename:  ");
  gets(fileName);
  fp = fopen(fileName,"w");
  temp = list;
  while(temp != NULL) {
    print_node_to_file(temp);
    temp = temp->next;
  }
  fclose(fp);
}

void print_node_to_file(Aptr temp, FILE *fp) {

  fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



int main() {

  char input;

printf("Welcome to Online Address Book!\n"
         "Enter s to search for an entry...\n"
         "Enter a to add an entry...\n"
         "Enter l to list an entry...\n"
         "Enter d to delete an entry...\n"
         "Enter p to print the address book to a file...\n"
         "Enter q to quit.\n");


  Aptr list = (Aptr)(malloc(sizeof(Address)));
  list = readFile();

  while(1) {

    printf("Enter your choice:  ");
    input = getchar();
    switch(input) {
    case 's':
      search(list);
      break;
    case 'a':
      add(list);
      break;
    case 'l':
      list(list);
      break;
    case 'd':
      delete(list);
      break;
    case 'p':
      print(list);
      break;
    case 'q':
      exit(0);
    default:
      printf("Huh?");
    }

  }

}





0
Kent OlsenData Warehouse Architect / DBACommented:

If you're going to delete one of the '}' symbols, delete the one on line 97.  The number in parentheses is the line number.

C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(97) : error C2059: syntax error : '}'


And how about posting this iteration of errors/warnings?  :)

Kent
0
cooldeanAuthor Commented:
I deleted the one on line 97 and the errors went from 9 to 26!  Here are the errors:

--------------------Configuration: hw8p1 - Win32 Debug--------------------
Compiling...
hw8p1.c
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(38) : error C2231: '.prev' : left operand points to 'struct', use '->'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(103) : error C2143: syntax error : missing ';' before 'type'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(104) : error C2143: syntax error : missing ';' before 'type'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(105) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(31) : see declaration of 'Aptr'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(105) : error C2146: syntax error : missing ';' before identifier 'list'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(105) : error C2065: 'list' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(106) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(31) : see declaration of 'Aptr'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(106) : error C2146: syntax error : missing ';' before identifier 'new'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(106) : error C2065: 'new' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(108) : error C2065: 'lname' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(108) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(109) : error C2065: 'fname' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(109) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(110) : error C2065: 'str' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(110) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(111) : error C2065: 'ci' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(111) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(112) : error C2065: 'st' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(112) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(113) : error C2065: 'zip' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(113) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(114) : error C2065: 'tel' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(114) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(115) : error C2065: 'cell' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(115) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(116) : error C2065: 'email' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(116) : warning C4047: '=' : 'int ' differs in levels of indirection from 'char *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(118) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(118) : warning C4553: '==' : operator has no effect; did you intend '='?
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(121) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(121) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(122) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(122) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(123) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(123) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(124) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(124) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(125) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(125) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(126) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(126) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(127) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(127) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(128) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(128) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(129) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(129) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 2
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 3
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 4
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 5
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 6
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 7
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 8
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4024: 'makeNode' : different types for formal and actual parameter 9
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(131) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(132) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(132) : warning C4024: 'insert' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(132) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(132) : warning C4024: 'insert' : different types for formal and actual parameter 2
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(132) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(133) : error C2065: 'newLine' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(133) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(133) : warning C4024: 'fgets' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(134) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(136) : warning C4047: 'return' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(167) : warning C4013: 'MakeNode' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(167) : warning C4047: '=' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(193) : error C2373: 'list' : redefinition; different type modifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(238) : error C2065: 'temp' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(238) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(239) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(240) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(240) : warning C4024: 'print_node' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(241) : error C2223: left of '->next' must point to struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(254) : warning C4013: 'print_node_to_file' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(260) : error C2371: 'print_node_to_file' : redefinition; different basic types
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(281) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(31) : see declaration of 'Aptr'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(281) : error C2146: syntax error : missing ';' before identifier 'list'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(281) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(282) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(290) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(290) : warning C4024: 'search' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(293) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(293) : warning C4024: 'add' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(296) : error C2063: 'list' : not a function
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(299) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(299) : warning C4024: 'delete' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(302) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(302) : warning C4024: 'print' : different types for formal and actual parameter 1
Error executing cl.exe.

hw8p1.obj - 26 error(s), 74 warning(s)
0
Kent OlsenData Warehouse Architect / DBACommented:
Ok.

At line 38 you still need to change ".prev" to "->prev".

If you're using ANSI C, this is illegal:

Aptr readFile() {
  FILE *fp;
  fp = fopen("addressbook.txt","r");
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;


You need to place all of the declarations before executable statements:

Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  fp = fopen ("addressbook.txt", "r");


This accounts for most of the errors that follow which are "undefined variable".

Kent
0
cooldeanAuthor Commented:
ok...here's the revised...13 errors...

--------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXSTREET 100
#define MAXCITY 20
#define MAXSTATE 2
#define MAXZIP 5
#define MAXTEL 20
#define MAXE 100



struct address {
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

  struct address *prev;
  struct address *next;
};

typedef struct address Address;
typedef Address *Aptr;

Aptr head=NULL, tail=NULL;

Aptr goToHead(Aptr spot) {

  while(spot->prev != head) {
    spot = spot->prev;
  }
  return spot;
}

Aptr makeNode(char *lname, char *fname, char *str, char *ci, char *st, char *zip, char *tel, char *cell, char *email) {
  Aptr new;

  new = (Aptr)(malloc(sizeof(Address)));
  /*Not sure if I have to malloc each one..
new->lastname = (char *)(malloc(sizeof(char) * MAXNAME));
  */
  strcpy(new->lastname,lname);
  strcpy(new->firstname,fname);
  strcpy(new->street,str);
  strcpy(new->city,ci);
  strcpy(new->state,st);
  strcpy(new->zip,zip);
  strcpy(new->tel,tel);
  strcpy(new->cell,cell);
  strcpy(new->email,email);
 
  new->prev = NULL;
  new->next = NULL;

  return new;
}

Aptr insert(Aptr node, Aptr list) {

  if(list == NULL) {
    list = node;
    return list;
  }

  if(strcmp(node->lastname, list->lastname) < 0) {
    node->prev = head;
    node->next = list;
    list->prev = node;
    return goToHead(list);
  }

  while(list->next != NULL) {
    if(strcmp(node->lastname, list->next->lastname) < -1) {
      node->next = list->next;
      node->prev = list;
      list->next = node;
      list->next->prev = node;
      return goToHead(list);
    }
    else {
      list = list->next;
    }
  }
  list->next = node;
  node->prev = list;
  node->next = tail;
  return goToHead(list);
}



Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  fp = fopen("addressbook.txt","r");
  Aptr list;
  Aptr new;

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list == NULL;

  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return list;
    }
  }

}

void print_node(Aptr temp) {

  printf("%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



void search(Aptr list) {

  char *lname = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter last name:  ");
  gets(lname);

  while(list != NULL) {
    if(strstr(list->lastname, lname) != NULL) {
      print_node(list);
    }
    list = list->next;
  }

}

void add(Aptr list) {
  Aptr new;
  new = MakeNode();
  /*
Prompting user for new info
  */
  printf("enter lastname: ");
  gets(new->lastname);
  printf("enter firstname: ");
  gets(new->firstname);
  printf("enter street: ");
  gets(new->street);
  printf("enter city: ");
  gets(new->city);
  printf("enter state: ");
  gets(new->state);
  printf("enter zip: ");
  gets(new->zip);
  printf("enter tel: ");
  gets(new->tel);
  printf("enter cell: ");
  gets(new->cell);
  printf("enter email: ");
  gets(new->email);

  list = insert(new, list);
}

void list(Aptr list) {

  Aptr temp = list;
  while(temp != NULL) {
    printf("%s %s\n",temp->firstname, temp->lastname);
  }
}

void delete(Aptr list) {
  Aptr temp = list;
  char c;
  char *delName = (char *)(malloc(sizeof(char) * MAXNAME));

  printf("Enter last name:  ");
  gets(delName);

  while(temp != NULL) {
    if(strstr(temp->lastname, delName) != NULL) {
      print_node(temp);
      printf("Are you sure you want to delete? Y or N:  ");
      c = getchar();
      if(c == 'y') {
      if(temp->prev == head && temp->next == tail) {
        temp = NULL;
      }
      else if(temp->prev = head) {
        temp->next->prev = head;
      }
      else if(temp->next = tail) {
        temp->prev->next = tail;
      }
      else {
        temp->prev->next = temp->next->prev;
        temp->next->prev = temp->prev->next;
      }
      }
    }
    temp = temp->next;
  }

}



void print(Aptr list) {
  temp = list;
  while(temp != NULL) {
    print_node(temp);
    temp = temp->next;
  }
}

void print_to_file(Aptr list) {
  FILE *fp;
  Aptr temp;
  char *fileName = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter filename:  ");
  gets(fileName);
  fp = fopen(fileName,"w");
  temp = list;
  while(temp != NULL) {
    print_node_to_file(temp);
    temp = temp->next;
  }
  fclose(fp);
}

void print_node_to_file(Aptr temp, FILE *fp) {

  fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



int main() {

  char input;

printf("Welcome to Online Address Book!\n"
         "Enter s to search for an entry...\n"
         "Enter a to add an entry...\n"
         "Enter l to list an entry...\n"
         "Enter d to delete an entry...\n"
         "Enter p to print the address book to a file...\n"
         "Enter q to quit.\n");


  Aptr list = (Aptr)(malloc(sizeof(Address)));
  list = readFile();

  while(1) {

    printf("Enter your choice:  ");
    input = getchar();
    switch(input) {
    case 's':
      search(list);
      break;
    case 'a':
      add(list);
      break;
    case 'l':
      list(list);
      break;
    case 'd':
      delete(list);
      break;
    case 'p':
      print(list);
      break;
    case 'q':
      exit(0);
    default:
      printf("Huh?");
    }

  }

}

0
cooldeanAuthor Commented:
here are the errors...

-----------
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(112) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(38) : see declaration of 'Aptr'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(112) : error C2146: syntax error : missing ';' before identifier 'list'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(112) : error C2065: 'list' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(113) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(38) : see declaration of 'Aptr'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(113) : error C2146: syntax error : missing ';' before identifier 'new'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(113) : error C2065: 'new' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(125) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(125) : warning C4553: '==' : operator has no effect; did you intend '='?
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(138) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(139) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(139) : warning C4024: 'insert' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(139) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(139) : warning C4024: 'insert' : different types for formal and actual parameter 2
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(139) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(143) : warning C4047: 'return' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(174) : warning C4013: 'MakeNode' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(174) : warning C4047: '=' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(200) : error C2373: 'list' : redefinition; different type modifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(245) : error C2065: 'temp' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(245) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(246) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(247) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(247) : warning C4024: 'print_node' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(248) : error C2223: left of '->next' must point to struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(261) : warning C4013: 'print_node_to_file' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(267) : error C2371: 'print_node_to_file' : redefinition; different basic types
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(288) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(38) : see declaration of 'Aptr'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(288) : error C2146: syntax error : missing ';' before identifier 'list'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(288) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(289) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct address *'
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(297) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(297) : warning C4024: 'search' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(300) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(300) : warning C4024: 'add' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(303) : error C2063: 'list' : not a function
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(306) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(306) : warning C4024: 'delete' : different types for formal and actual parameter 1
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(309) : warning C4047: 'function' : 'struct address *' differs in levels of indirection from 'int '
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(309) : warning C4024: 'print' : different types for formal and actual parameter 1
Error executing cl.exe.

hw8p1.obj - 13 error(s), 26 warning(s)
0
Kent OlsenData Warehouse Architect / DBACommented:
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(112) : error C2275: 'Aptr' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(38) : see declaration of 'Aptr'

Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  fp = fopen("addressbook.txt","r");
  Aptr list;
  Aptr new;

Again, you need to move the fopen() statement AFTER your declarations.  Also, the word 'new' is a reserved word in C++ and may be to your C compiler.  Change the variable name and all occurrences with this function.  (And the other functions where you have a variable named 'new'.)

Kent


0
cooldeanAuthor Commented:
hmmm....this really sucks...
0
sunnycoderCommented:
1.  char *newLine;
 fp = fopen("addressbook.txt","r");
 Aptr list;
 Aptr new;
as Kent said ... move fopen to after declarations


2. void add(Aptr list) {
 Aptr new;
 new = MakeNode();

you have defined a function makeNode and not MakeNode .... case does matter ... also makeNode needs some arguments which you are not supplying ...

3. void print(Aptr list) {
 temp = list;       >>>>>>>>> temp undeclares ... perhaps you meant Aptr temp = list;
 while(temp != NULL) {
   print_node(temp);
   temp = temp->next;
 }
}

4. void print_node_to_file(Aptr temp, FILE *fp) {

 fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}

In function print)to_file, you are invoking print_node_to_file as   print_node_to_file(temp);
clearly there is an argument mismatch
Also, it is good to have atleast a function declaration before a function is actually called ... here you are placing the call withour declaring it

5. In main()
 Aptr list = (Aptr)(malloc(sizeof(Address)));
all declarations should be at the beginning of the block ... such statements in the middle of the code are not allowed

there are some 5-6 error messages bein generated due to this invalid declaration ...

while Kent's suggestion to rename new variable is a good one, it is unlikely that it is cause of errors ... new is not reserved as far as C is concerned
0
cooldeanAuthor Commented:
I got the errors to go down to just 2 errors and 1 warnings.  I dont' know why it says that there is no 'list' function in the int main() when I have it as a function at the top.  And, how can i fix:

void add(Aptr list) {
 Aptr new;
 new = MakeNode();

I know I have to make MakeNode to makeNode since that is how I declared it but what arguments do I need to put in there?

Here is the revised code:  (I'm almost there!)
---------------


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXSTREET 100
#define MAXCITY 20
#define MAXSTATE 2
#define MAXZIP 5
#define MAXTEL 20
#define MAXE 100


struct address {
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

  struct address *prev;
  struct address *next;
};

typedef struct address Address;
typedef Address *Aptr;

Aptr head=NULL, tail=NULL;

Aptr goToHead(Aptr spot) {

  while(spot->prev != head) {
    spot = spot->prev;
  }
  return spot;
}

Aptr makeNode(char *lname, char *fname, char *str, char *ci, char *st, char *zip, char *tel, char *cell, char *email) {
  Aptr new;

  new = (Aptr)(malloc(sizeof(Address)));
  /*Not sure if I have to malloc each one..
new->lastname = (char *)(malloc(sizeof(char) * MAXNAME));
  */
  strcpy(new->lastname,lname);
  strcpy(new->firstname,fname);
  strcpy(new->street,str);
  strcpy(new->city,ci);
  strcpy(new->state,st);
  strcpy(new->zip,zip);
  strcpy(new->tel,tel);
  strcpy(new->cell,cell);
  strcpy(new->email,email);
 
  new->prev = NULL;
  new->next = NULL;

  return new;
}

Aptr insert(Aptr node, Aptr list) {

  if(list == NULL) {
    list = node;
    return list;
  }

  if(strcmp(node->lastname, list->lastname) < 0) {
    node->prev = head;
    node->next = list;
    list->prev = node;
    return goToHead(list);
  }

  while(list->next != NULL) {
    if(strcmp(node->lastname, list->next->lastname) < -1) {
      node->next = list->next;
      node->prev = list;
      list->next = node;
      list->next->prev = node;
      return goToHead(list);
    }
    else {
      list = list->next;
    }
  }
  list->next = node;
  node->prev = list;
  node->next = tail;
  return goToHead(list);
}



Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  Aptr list;
  Aptr new;

  fp = fopen("addressbook.txt","r");

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list == NULL;

  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return list;
    }
  }

}

void print_node(Aptr temp) {

  printf("%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



void search(Aptr list) {

  char *lname = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter last name:  ");
  gets(lname);

  while(list != NULL) {
    if(strstr(list->lastname, lname) != NULL) {
      print_node(list);
    }
    list = list->next;
  }

}

void add(Aptr list) {
  Aptr new;
  new = makeNode();
  /*
Prompting user for new info
  */
  printf("enter lastname: ");
  gets(new->lastname);
  printf("enter firstname: ");
  gets(new->firstname);
  printf("enter street: ");
  gets(new->street);
  printf("enter city: ");
  gets(new->city);
  printf("enter state: ");
  gets(new->state);
  printf("enter zip: ");
  gets(new->zip);
  printf("enter tel: ");
  gets(new->tel);
  printf("enter cell: ");
  gets(new->cell);
  printf("enter email: ");
  gets(new->email);

  list = insert(new, list);
}

void list(Aptr list) {

  Aptr temp = list;
  while(temp != NULL) {
    printf("%s %s\n",temp->firstname, temp->lastname);
  }
}

void delete(Aptr list) {
  Aptr temp = list;
  char c;
  char *delName = (char *)(malloc(sizeof(char) * MAXNAME));

  printf("Enter last name:  ");
  gets(delName);

  while(temp != NULL) {
    if(strstr(temp->lastname, delName) != NULL) {
      print_node(temp);
      printf("Are you sure you want to delete? Y or N:  ");
      c = getchar();
      if(c == 'y') {
      if(temp->prev == head && temp->next == tail) {
        temp = NULL;
      }
      else if(temp->prev = head) {
        temp->next->prev = head;
      }
      else if(temp->next = tail) {
        temp->prev->next = tail;
      }
      else {
        temp->prev->next = temp->next->prev;
        temp->next->prev = temp->prev->next;
      }
      }
    }
    temp = temp->next;
  }

}



void print(Aptr list) {
  Aptr temp = list;
  while(temp != NULL) {
    print_node(temp);
    temp = temp->next;
  }
}


void print_node_to_file(Aptr temp, FILE *fp) {

  fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}


void print_to_file(Aptr list) {
  FILE *fp;
  Aptr temp;
  char *fileName = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter filename:  ");
  gets(fileName);
  fp = fopen(fileName,"w");
  temp = list;
  while(temp != NULL) {
    print_node_to_file(temp, fp);
    temp = temp->next;
  }
  fclose(fp);
}




int main() {

  char input;
 Aptr list = (Aptr)(malloc(sizeof(Address)));
  list = readFile();


printf("Welcome to Online Address Book!\n"
         "Enter s to search for an entry...\n"
         "Enter a to add an entry...\n"
         "Enter l to list an entry...\n"
         "Enter d to delete an entry...\n"
         "Enter p to print the address book to a file...\n"
         "Enter q to quit.\n");

 

  while(1) {

    printf("Enter your choice:  ");
    input = getchar();
    switch(input) {
    case 's':
      search(list);
      break;
    case 'a':
      add(list);
      break;
    case 'l':
      list(list);
      break;
    case 'd':
      delete(list);
      break;
    case 'p':
      print(list);
      break;
    case 'q':
      exit(0);
    default:
      printf("Huh?");
    }

  }

}

0
cooldeanAuthor Commented:
Ok I fixed it...I changed 'list' to 'listStuff' and for some reason it recognized it.  Now I just need help on fixing the makeNode...telling me that I dont' ahve the arguments.  What arguments should I put on there?

Here is the revised code...with 1 error..and no warnings!!!

---------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXSTREET 100
#define MAXCITY 20
#define MAXSTATE 2
#define MAXZIP 5
#define MAXTEL 20
#define MAXE 100


struct address {
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

  struct address *prev;
  struct address *next;
};

typedef struct address Address;
typedef Address *Aptr;

Aptr head=NULL, tail=NULL;

Aptr goToHead(Aptr spot) {

  while(spot->prev != head) {
    spot = spot->prev;
  }
  return spot;
}

Aptr makeNode(char *lname, char *fname, char *str, char *ci, char *st, char *zip, char *tel, char *cell, char *email) {
  Aptr new;

  new = (Aptr)(malloc(sizeof(Address)));
  /*Not sure if I have to malloc each one..
new->lastname = (char *)(malloc(sizeof(char) * MAXNAME));
  */
  strcpy(new->lastname,lname);
  strcpy(new->firstname,fname);
  strcpy(new->street,str);
  strcpy(new->city,ci);
  strcpy(new->state,st);
  strcpy(new->zip,zip);
  strcpy(new->tel,tel);
  strcpy(new->cell,cell);
  strcpy(new->email,email);
 
  new->prev = NULL;
  new->next = NULL;

  return new;
}

Aptr insert(Aptr node, Aptr list) {

  if(list == NULL) {
    list = node;
    return list;
  }

  if(strcmp(node->lastname, list->lastname) < 0) {
    node->prev = head;
    node->next = list;
    list->prev = node;
    return goToHead(list);
  }

  while(list->next != NULL) {
    if(strcmp(node->lastname, list->next->lastname) < -1) {
      node->next = list->next;
      node->prev = list;
      list->next = node;
      list->next->prev = node;
      return goToHead(list);
    }
    else {
      list = list->next;
    }
  }
  list->next = node;
  node->prev = list;
  node->next = tail;
  return goToHead(list);
}



Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  Aptr list;
  Aptr new;

  fp = fopen("addressbook.txt","r");

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list = NULL;

  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return list;
    }
  }

}

void print_node(Aptr temp) {

  printf("%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



void search(Aptr list) {

  char *lname = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter last name:  ");
  gets(lname);

  while(list != NULL) {
    if(strstr(list->lastname, lname) != NULL) {
      print_node(list);
    }
    list = list->next;
  }

}

void add(Aptr list) {
  Aptr new;
  new = makeNode();
  /*
Prompting user for new info
  */
  printf("enter lastname: ");
  gets(new->lastname);
  printf("enter firstname: ");
  gets(new->firstname);
  printf("enter street: ");
  gets(new->street);
  printf("enter city: ");
  gets(new->city);
  printf("enter state: ");
  gets(new->state);
  printf("enter zip: ");
  gets(new->zip);
  printf("enter tel: ");
  gets(new->tel);
  printf("enter cell: ");
  gets(new->cell);
  printf("enter email: ");
  gets(new->email);

  list = insert(new, list);
}

void listStuff(Aptr list) {

  Aptr temp = list;
  while(temp != NULL) {
    printf("%s %s\n",temp->firstname, temp->lastname);
  }
}

void delete(Aptr list) {
  Aptr temp = list;
  char c;
  char *delName = (char *)(malloc(sizeof(char) * MAXNAME));

  printf("Enter last name:  ");
  gets(delName);

  while(temp != NULL) {
    if(strstr(temp->lastname, delName) != NULL) {
      print_node(temp);
      printf("Are you sure you want to delete? Y or N:  ");
      c = getchar();
      if(c == 'y') {
      if(temp->prev == head && temp->next == tail) {
        temp = NULL;
      }
      else if(temp->prev = head) {
        temp->next->prev = head;
      }
      else if(temp->next = tail) {
        temp->prev->next = tail;
      }
      else {
        temp->prev->next = temp->next->prev;
        temp->next->prev = temp->prev->next;
      }
      }
    }
    temp = temp->next;
  }

}



void print(Aptr list) {
  Aptr temp = list;
  while(temp != NULL) {
    print_node(temp);
    temp = temp->next;
  }
}


void print_node_to_file(Aptr temp, FILE *fp) {

  fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}


void print_to_file(Aptr list) {
  FILE *fp;
  Aptr temp;
  char *fileName = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter filename:  ");
  gets(fileName);
  fp = fopen(fileName,"w");
  temp = list;
  while(temp != NULL) {
    print_node_to_file(temp, fp);
    temp = temp->next;
  }
  fclose(fp);
}




int main() {

  char input;
 Aptr list = (Aptr)(malloc(sizeof(Address)));
  list = readFile();


printf("Welcome to Online Address Book!\n"
         "Enter s to search for an entry...\n"
         "Enter a to add an entry...\n"
         "Enter l to list an entry...\n"
         "Enter d to delete an entry...\n"
         "Enter p to print the address book to a file...\n"
         "Enter q to quit.\n");

 

  while(1) {

    printf("Enter your choice:  ");
    input = getchar();
    switch(input) {
    case 's':
      search(list);
      break;
    case 'a':
      add(list);
      break;
    case 'l':
      listStuff(list);
      break;
    case 'd':
      delete(list);
      break;
    case 'p':
      print(list);
      break;
    case 'q':
      exit(0);
    default:
      printf("Huh?");
    }

  }

}

0
Kent OlsenData Warehouse Architect / DBACommented:

Hi cooldean,

Man, I wish we could get you into the habit of posting the errors along with the source.  :)

main() may be prototyped as int main (int argc, char **argv).

'MakeNode' doesn't exist in your source anywhere so I don't know what that problem might be.


Kent
0
Kent OlsenData Warehouse Architect / DBACommented:

That's an easy one.

In add() you call makeNode() without passing any arguments.

Kent
0
cooldeanAuthor Commented:
Oh sorry...here is the error I get:  


And yeah there are no arguments..but what arguments should I put on there? I don't know what to put..


--------------------Configuration: hw8p1 - Win32 Debug--------------------
Compiling...
hw8p1.c
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(175) : error C2198: 'makeNode' : too few actual parameters
Error executing cl.exe.

hw8p1.obj - 1 error(s), 0 warning(s)
0
Kent OlsenData Warehouse Architect / DBACommented:

Several things need some cleanup.

The biggest one seems to be that you make pretty liberal use of the gets() function.  Avoid gets() whenever you can.  It has no way of knowing the length of your input field so it can overwrite things that are best left alone.  :)  The way your structure is organized, you're unlikely to "break" anything to the point of a program crash, but you might easily get data truncated.  That could cause you some headaches down the road.

gets (node->zip);  // User enters 123456
gets (node->tel);  // User enters 1234567890

fputs (node->zip);  // 1234561234567890 will be displayed.

fgets (buffer, MAX_SIZE, stdin);  Will solve that problem.


Kent
0
Kent OlsenData Warehouse Architect / DBACommented:

Your add() function needs to be more like this:

void add(Aptr list) {
Aptr new;
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

/*
Prompting user for new info
*/
 printf("enter lastname: ");
 fgets(lastname, MAXNAME, stdin);
 printf("enter firstname: ");
 fgets(firstname, MAXNAME, stdin);
 printf("enter street: ");
 fgets(street, MAXSTREET, stdin);
 printf("enter city: ");
 fgets(city, MAXCITY, stdin);
 printf("enter state: ");
 fgets(state, MAXSTATE, stdin);
 printf("enter zip: ");
 fgets(zip, MAXZIP, stdin);
 printf("enter tel: ");
 fgets(tel, MAXTEL, stdin);
 printf("enter cell: ");
 fgets(cell, MAXTEL, stdin);
 printf("enter email: ");
 fgets(email, MAXE, stdin);
 
new = makeNode(lastname, firstname, street, city, state, zip, tel, cell, email);
}

Note that you've made a new node, but you haven't actually added it it to the list of node.  You need to insert() the node.


Kent
0
cooldeanAuthor Commented:
Ok...I fixed it and I just got one warning...but whenever I run the code, it crashes...can you try running it and see if it works..

Here is the one warning...

C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(149) : warning C4700: local variable 'newLine' used without having been initialized

hw8p1.obj - 0 error(s), 1 warning(s)

-----------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXSTREET 100
#define MAXCITY 20
#define MAXSTATE 2
#define MAXZIP 5
#define MAXTEL 20
#define MAXE 100

/* course-specific functions to avoid using scanf */
int getInt() {
  int tmp = 0;
  char buf[200];
  return sscanf(gets(buf), "%d", &tmp) * tmp;
}

double getDouble() {
  double tmp = 0.0;
  char buf[200];
  return sscanf(gets(buf), "%lf", &tmp) * tmp;
}



struct address {
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

  struct address *prev;
  struct address *next;
};

typedef struct address Address;
typedef Address *Aptr;

Aptr head=NULL, tail=NULL;

Aptr goToHead(Aptr spot) {

  while(spot->prev != head) {
    spot = spot->prev;
  }
  return spot;
}

Aptr makeNode(char *lname, char *fname, char *str, char *ci, char *st, char *zip, char *tel, char *cell, char *email) {
  Aptr new;

  new = (Aptr)(malloc(sizeof(Address)));
  /*Not sure if I have to malloc each one..
new->lastname = (char *)(malloc(sizeof(char) * MAXNAME));
  */
  strcpy(new->lastname,lname);
  strcpy(new->firstname,fname);
  strcpy(new->street,str);
  strcpy(new->city,ci);
  strcpy(new->state,st);
  strcpy(new->zip,zip);
  strcpy(new->tel,tel);
  strcpy(new->cell,cell);
  strcpy(new->email,email);
 
  new->prev = NULL;
  new->next = NULL;

  return new;
}

Aptr insert(Aptr node, Aptr list) {

  if(list == NULL) {
    list = node;
    return list;
  }

  if(strcmp(node->lastname, list->lastname) < 0) {
    node->prev = head;
    node->next = list;
    list->prev = node;
    return goToHead(list);
  }

  while(list->next != NULL) {
    if(strcmp(node->lastname, list->next->lastname) < -1) {
      node->next = list->next;
      node->prev = list;
      list->next = node;
      list->next->prev = node;
      return goToHead(list);
    }
    else {
      list = list->next;
    }
  }
  list->next = node;
  node->prev = list;
  node->next = tail;
  return goToHead(list);
}



Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  Aptr list;
  Aptr new;

  fp = fopen("addressbook.txt","r");

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list = NULL;

  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return list;
    }
  }

}

void print_node(Aptr temp) {

  printf("%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



void search(Aptr list) {

  char *lname = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter last name:  ");
  gets(lname);

  while(list != NULL) {
    if(strstr(list->lastname, lname) != NULL) {
      print_node(list);
    }
    list = list->next;
  }

}

void add(Aptr list) {
Aptr new;
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

/*
Prompting user for new info
*/
 printf("enter lastname: ");
 fgets(lastname, MAXNAME, stdin);
 printf("enter firstname: ");
 fgets(firstname, MAXNAME, stdin);
 printf("enter street: ");
 fgets(street, MAXSTREET, stdin);
 printf("enter city: ");
 fgets(city, MAXCITY, stdin);
 printf("enter state: ");
 fgets(state, MAXSTATE, stdin);
 printf("enter zip: ");
 fgets(zip, MAXZIP, stdin);
 printf("enter tel: ");
 fgets(tel, MAXTEL, stdin);
 printf("enter cell: ");
 fgets(cell, MAXTEL, stdin);
 printf("enter email: ");
 fgets(email, MAXE, stdin);
 
      new = makeNode(lastname, firstname, street, city, state, zip, tel, cell, email);

  list = insert(new, list);
}

void listStuff(Aptr list) {

  Aptr temp = list;
  while(temp != NULL) {
    printf("%s %s\n",temp->firstname, temp->lastname);
  }
}

void delete(Aptr list) {
  Aptr temp = list;
  char c;
  char *delName = (char *)(malloc(sizeof(char) * MAXNAME));

  printf("Enter last name:  ");
  gets(delName);

  while(temp != NULL) {
    if(strstr(temp->lastname, delName) != NULL) {
      print_node(temp);
      printf("Are you sure you want to delete? Y or N:  ");
      c = getchar();
      if(c == 'y') {
      if(temp->prev == head && temp->next == tail) {
        temp = NULL;
      }
      else if(temp->prev = head) {
        temp->next->prev = head;
      }
      else if(temp->next = tail) {
        temp->prev->next = tail;
      }
      else {
        temp->prev->next = temp->next->prev;
        temp->next->prev = temp->prev->next;
      }
      }
    }
    temp = temp->next;
  }

}



void print(Aptr list) {
  Aptr temp = list;
  while(temp != NULL) {
    print_node(temp);
    temp = temp->next;
  }
}


void print_node_to_file(Aptr temp, FILE *fp) {

  fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}


void print_to_file(Aptr list) {
  FILE *fp;
  Aptr temp;
  char *fileName = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter filename:  ");
  gets(fileName);
  fp = fopen(fileName,"w");
  temp = list;
  while(temp != NULL) {
    print_node_to_file(temp, fp);
    temp = temp->next;
  }
  fclose(fp);
}




int main() {

  char input;
 Aptr list = (Aptr)(malloc(sizeof(Address)));
  list = readFile();


printf("Welcome to Online Address Book!\n"
         "Enter s to search for an entry...\n"
         "Enter a to add an entry...\n"
         "Enter l to list an entry...\n"
         "Enter d to delete an entry...\n"
         "Enter p to print the address book to a file...\n"
         "Enter q to quit.\n");

 

  while(1) {

    printf("Enter your choice:  ");
    input = getchar();
    switch(input) {
    case 's':
      search(list);
      break;
    case 'a':
      add(list);
      break;
    case 'l':
      listStuff(list);
      break;
    case 'd':
      delete(list);
      break;
    case 'p':
      print(list);
      break;
    case 'q':
      exit(0);
    default:
      printf("Huh?");
    }

  }

}

0
Kent OlsenData Warehouse Architect / DBACommented:

The problem is in readFile().  Even if your data file doesn't exist, you try and read it anyway.  You wind up dereferencing NULL as the FILE item and writing things into low memory that shouldn't be there.

if (fp)
  while {
  }

Wrapping an if() block around the while() statement like just above will work fine.

Kent
0
cooldeanAuthor Commented:
Ok...this is the change I made for the read file function:

------------------
Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  Aptr list;
  Aptr new;

  fp = fopen("addressbook.txt","r");

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list = NULL;

  if(fp)
  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return list;
    }
  }
}
----------------------------

And this is the warnings I got:

Compiling...
hw8p1.c
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(157) : warning C4715: 'readFile' : not all control paths return a value
C:\Program Files\Microsoft Visual Studio\MyProjects\CSE110\HW8\hw8p1\hw8p1.c(150) : warning C4700: local variable 'newLine' used without having been initialized

hw8p1.obj - 0 error(s), 2 warning(s)


And it still crashes when I run it...man, I've spent so much time on this...
0
Kent OlsenData Warehouse Architect / DBACommented:

Hmm...

I've run it and it gives me a menu.  Haven't tried past that yet.

What the last thing that you do before you get the abort?

Kent
0
cooldeanAuthor Commented:
I run the file and when the DOS prompt pops up, it immediately crashes, saying that it encountered a problem and needs to close.  Do I need to restart Visual Basic maybe?
0
Kent OlsenData Warehouse Architect / DBACommented:

VB?

0
cooldeanAuthor Commented:
I meant, restart my computer and run the program again...
0
Kent OlsenData Warehouse Architect / DBACommented:

Since you've bombed a couple of pointers, it can't hurt.

To run the program, open a DOS window and run it from the window.  (Don't run it from Window and the automatic window that it creates.)  This way when it crashes the window will stick around.

Kent
0
cooldeanAuthor Commented:
i'll do that...but u said u tried to run it...did it work...especially the options?
0
Kent OlsenData Warehouse Architect / DBACommented:

I didn't try any of the options.  I saw the "menu" and selected "quit".

Offhand, I think there's more work to do.  :(   But I'll help you through it.  :)

Kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cooldeanAuthor Commented:
yeah..unfortunately it's due tomorrow morning ..and got a final tomorrow...man, I don't even know why I spent so much time on this...
0
Kent OlsenData Warehouse Architect / DBACommented:

readfFile() still has a problem.

When the input file doesn't exist, the open fails and fp is still NULL.

if (fp)
  while (1) {}

The while loop is never entered so the return statement within it isn't executed.  When readFile() exits, it returns whatever junk is already on the stack.

Make sure that the last think within readFile() is 'return (list);'


Kent
0
cooldeanAuthor Commented:
I restarted my computer and tried to run the code  again but it crashes as soon as the window pops up...the code is below...


----------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXSTREET 100
#define MAXCITY 20
#define MAXSTATE 2
#define MAXZIP 5
#define MAXTEL 20
#define MAXE 100

/* course-specific functions to avoid using scanf */
int getInt() {
  int tmp = 0;
  char buf[200];
  return sscanf(gets(buf), "%d", &tmp) * tmp;
}

double getDouble() {
  double tmp = 0.0;
  char buf[200];
  return sscanf(gets(buf), "%lf", &tmp) * tmp;
}



struct address {
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

  struct address *prev;
  struct address *next;
};

typedef struct address Address;
typedef Address *Aptr;

Aptr head=NULL, tail=NULL;

Aptr goToHead(Aptr spot) {

  while(spot->prev != head) {
    spot = spot->prev;
  }
  return spot;
}

Aptr makeNode(char *lname, char *fname, char *str, char *ci, char *st, char *zip, char *tel, char *cell, char *email) {
  Aptr new;

  new = (Aptr)(malloc(sizeof(Address)));
  /*Not sure if I have to malloc each one..
new->lastname = (char *)(malloc(sizeof(char) * MAXNAME));
  */
  strcpy(new->lastname,lname);
  strcpy(new->firstname,fname);
  strcpy(new->street,str);
  strcpy(new->city,ci);
  strcpy(new->state,st);
  strcpy(new->zip,zip);
  strcpy(new->tel,tel);
  strcpy(new->cell,cell);
  strcpy(new->email,email);
 
  new->prev = NULL;
  new->next = NULL;

  return new;
}

Aptr insert(Aptr node, Aptr list) {

  if(list == NULL) {
    list = node;
    return list;
  }

  if(strcmp(node->lastname, list->lastname) < 0) {
    node->prev = head;
    node->next = list;
    list->prev = node;
    return goToHead(list);
  }

  while(list->next != NULL) {
    if(strcmp(node->lastname, list->next->lastname) < -1) {
      node->next = list->next;
      node->prev = list;
      list->next = node;
      list->next->prev = node;
      return goToHead(list);
    }
    else {
      list = list->next;
    }
  }
  list->next = node;
  node->prev = list;
  node->next = tail;
  return goToHead(list);
}



Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char *newLine;
  Aptr list;
  Aptr new;

  fp = fopen("addressbook.txt","r");

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list = NULL;

  if(fp)
  while(1) {
    fgets(lname, MAXNAME, fp);
    fgets(fname, MAXNAME, fp);
      fgets(str, MAXSTREET, fp);
      fgets(ci, MAXCITY, fp);
      fgets(st, MAXSTATE, fp);
      fgets(zip, MAXZIP, fp);
      fgets(tel, MAXTEL, fp);
      fgets(cell, MAXTEL, fp);
      fgets(email, MAXE, fp);

      new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
    list = insert(new, list);
    fgets(newLine,50,fp);
    if(newLine == NULL) {
      fclose(fp);
      return (list);
    }
  }

}

void print_node(Aptr temp) {

  printf("%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}



void search(Aptr list) {

  char *lname = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter last name:  ");
  gets(lname);

  while(list != NULL) {
    if(strstr(list->lastname, lname) != NULL) {
      print_node(list);
    }
    list = list->next;
  }

}

void add(Aptr list) {
Aptr new;
  char lastname[MAXNAME + 1];
  char firstname[MAXNAME + 1];
  char street[MAXSTREET + 1];
  char city[MAXCITY+1];
  char state[MAXSTATE+1];
  char zip[MAXZIP+1];
  char tel[MAXTEL+1];
  char cell[MAXTEL+1];
  char email[MAXE+1];

/*
Prompting user for new info
*/
 printf("enter lastname: ");
 fgets(lastname, MAXNAME, stdin);
 printf("enter firstname: ");
 fgets(firstname, MAXNAME, stdin);
 printf("enter street: ");
 fgets(street, MAXSTREET, stdin);
 printf("enter city: ");
 fgets(city, MAXCITY, stdin);
 printf("enter state: ");
 fgets(state, MAXSTATE, stdin);
 printf("enter zip: ");
 fgets(zip, MAXZIP, stdin);
 printf("enter tel: ");
 fgets(tel, MAXTEL, stdin);
 printf("enter cell: ");
 fgets(cell, MAXTEL, stdin);
 printf("enter email: ");
 fgets(email, MAXE, stdin);
 
      new = makeNode(lastname, firstname, street, city, state, zip, tel, cell, email);

  list = insert(new, list);
}

void listStuff(Aptr list) {

  Aptr temp = list;
  while(temp != NULL) {
    printf("%s %s\n",temp->firstname, temp->lastname);
  }
}

void delete(Aptr list) {
  Aptr temp = list;
  char c;
  char *delName = (char *)(malloc(sizeof(char) * MAXNAME));

  printf("Enter last name:  ");
  gets(delName);

  while(temp != NULL) {
    if(strstr(temp->lastname, delName) != NULL) {
      print_node(temp);
      printf("Are you sure you want to delete? Y or N:  ");
      c = getchar();
      if(c == 'y') {
      if(temp->prev == head && temp->next == tail) {
        temp = NULL;
      }
      else if(temp->prev = head) {
        temp->next->prev = head;
      }
      else if(temp->next = tail) {
        temp->prev->next = tail;
      }
      else {
        temp->prev->next = temp->next->prev;
        temp->next->prev = temp->prev->next;
      }
      }
    }
    temp = temp->next;
  }

}



void print(Aptr list) {
  Aptr temp = list;
  while(temp != NULL) {
    print_node(temp);
    temp = temp->next;
  }
}


void print_node_to_file(Aptr temp, FILE *fp) {

  fprintf(fp,"%s %s\n%s\n%s %s %s\n%s\n%s\n%s",temp->lastname, temp->firstname, temp->street, temp->city, temp->state, temp->zip, temp->tel, temp->cell, temp->email);

}


void print_to_file(Aptr list) {
  FILE *fp;
  Aptr temp;
  char *fileName = (char *)(malloc(sizeof(char) * MAXNAME));
  printf("Enter filename:  ");
  gets(fileName);
  fp = fopen(fileName,"w");
  temp = list;
  while(temp != NULL) {
    print_node_to_file(temp, fp);
    temp = temp->next;
  }
  fclose(fp);
}




int main() {

  char input;
 Aptr list = (Aptr)(malloc(sizeof(Address)));
  list = readFile();


printf("Welcome to Online Address Book!\n"
         "Enter s to search for an entry...\n"
         "Enter a to add an entry...\n"
         "Enter l to list an entry...\n"
         "Enter d to delete an entry...\n"
         "Enter p to print the address book to a file...\n"
         "Enter q to quit.\n");

 

  while(1) {

    printf("Enter your choice:  ");
    input = getchar();
    switch(input) {
    case 's':
      search(list);
      break;
    case 'a':
      add(list);
      break;
    case 'l':
      listStuff(list);
      break;
    case 'd':
      delete(list);
      break;
    case 'p':
      print(list);
      break;
    case 'q':
      exit(0);
    default:
      printf("Huh?");
    }

  }

}

0
Kent OlsenData Warehouse Architect / DBACommented:
Hi cooldean,

You must have missed my last post.  You need to do an explicit return at the bottom of the readFile() function.

You also need to allocate storage for newLine in this function.  (How long is your input buffer?)

When you actually read the file, you will miss a line because when you fgets() at the bottom of the loop, you read the line then read over it again at the top of the loop.

The corrected code for all three problems is below, though I had to make a couple of guesses and take a liberty or two.

Also, do NOT fire up the exe from Windows.  Open a DOS window and run the program from the DOS command line.

Kent


Aptr readFile() {
  FILE *fp;
  char *lname, *fname, *str, *ci, *st, *zip, *tel, *cell, *email;
  char newLine[200];
  Aptr list;
  Aptr new;

  fp = fopen("addressbook.txt","r");

  lname = (char *)(malloc(sizeof(char) * MAXNAME));
  fname = (char *)(malloc(sizeof(char) * MAXNAME));
  str = (char *)(malloc(sizeof(char) * MAXSTREET));
  ci = (char *)(malloc(sizeof(char) * MAXCITY));
  st = (char *)(malloc(sizeof(char) * MAXSTATE));
  zip = (char *)(malloc(sizeof(char) * MAXZIP));
  tel = (char *)(malloc(sizeof(char) * MAXTEL));
  cell = (char *)(malloc(sizeof(char) * MAXTEL));
  email = (char *)(malloc(sizeof(char) * MAXE));

  list = NULL;

  if(fp)
  while(1) {
     fgets(lname, MAXNAME, fp);
     fgets(newLine,200,fp);
     if (feof (fp) {
       fclose(fp);
       break;
     }
     fgets(fname, MAXNAME, fp);
     fgets(str, MAXSTREET, fp);
     fgets(ci, MAXCITY, fp);
     fgets(st, MAXSTATE, fp);
     fgets(zip, MAXZIP, fp);
     fgets(tel, MAXTEL, fp);
     fgets(cell, MAXTEL, fp);
     fgets(email, MAXE, fp);

     new = makeNode(lname, fname, str, ci, st, zip, tel, cell, email);
     list = insert(new, list);
   }
  return (list);
}
0
cooldeanAuthor Commented:
I attached the code and the requirements to an email..check your email dude.
0
cooldeanAuthor Commented:
yeah the return(list) and it is reading a file that may not be there...i figured it out...thanks alot!
0
Kent OlsenData Warehouse Architect / DBACommented:

I hope that you got it finished in time to turn it in.

Kent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.