Code:
#include <stdio.h>
#include <stdlib.h>
char choice, firstname, lastname, phonenumber, comporg, email, search;
// Definition of node for holding customer info
struct customer {
char firstname;
char lastname;
char phonenumber;
char comporg;
char email;
struct customer *next;
};
typedef struct customer Customer;
typedef Customer *Customerptr;
// Function prototypes:
void loadmainmenu ();
void add( Customerptr *headOfList, char firstname, char lastname, char phonenumber, char comporg, char email );
char delete( Customerptr *headOfList, char fname, char lname );
void loadsearchmenu ();
void print( Customerptr *headOfList);
void printlist( Customerptr *headOfList );
int isEmpty( Customerptr cptr );
// Main program:
int main(void) {
Customerptr startptr = NULL;
loadmainmenu();
printf("Your choice? \n");
scanf("%c", choice);
while ( choice != 'e' || choice != 'E' ) {
// Evaluates menu options
switch (choice) {
// Displays add customer submenu:
case 'A':
case 'a':
printf("Add Submenu: \n\n");
printf("Please input First Name: \n");
scanf ("%c \n", &firstname);
printf("Please input Last Name: \n");
scanf ("%c \n", &lastname);
printf("Please input Phone Number: \n");
scanf ("%c \n", &phonenumber);
printf("Please input Company/Organization: \n");
scanf ("%c \n", &comporg);
printf("Please input Email: \n\n");
scanf ("%c \n", &email);
add(&startptr, firstname, lastname, phonenumber, comporg, email);
printf("A new customer has been added \n");
break;
// Displays remove customer submenu:
case 'R':
case 'r':
printf("Remove Submenu \n");
if ( !isEmpty( startptr )) {
printf("Please input First Name: \n");
scanf ("%c \n", &firstname);
printf("Please input Last Name: \n\n");
scanf ("%c \n", &lastname);
}
if ( delete( &startptr, firstname, lastname ) ) {
printf("%c %c has been removed from the system. \n", firstname, lastname);
}
break;
case 'S':
case 's':
printf("Search Submenu \n");
if ( !isEmpty( startptr )) {
printf("Please input the customer’s First or Last name: \n");
scanf ("%c \n", &search);
}
else
print (&startptr);
break;
// Displays display customer submenu:
case 'D':
case 'd':
if ( !isEmpty( startptr )) {
printf("Display Submenu \n");
printlist( &startptr );
}
printf("All customers are displayed: \n");
break;
case 'E':
case 'e':
exit(0);
break;
default:
printf("You entered an invalid choice! \n");
printf("Please try again ... \n");
loadmainmenu();
break;
}
}
}
// Displays main menu options:
void loadmainmenu() {
printf("Main Menu: \n\n");
printf("(a) Add a customer \n");
printf("(r) Remove a customer \n");
printf("(s) Search a customer \n");
printf("(d) Display all customers' information \n");
printf("(e) Exit \n\n");
}
// Adds customer name to database
void add( Customerptr *headOfList, char firstname, char lastname, char phonenumber, char comporg, char email ) {
Customerptr newPtr;
Customerptr previousPtr;
Customerptr currentPtr;
newPtr = ( Customer * ) malloc( sizeof( Customer ) );
if ( newPtr != NULL ) {
newPtr->firstname = firstname;
newPtr->lastname = lastname;
newPtr->phonenumber = phonenumber;
newPtr->comporg = comporg;
newPtr->email = email;
newPtr->next = NULL;
previousPtr = NULL;
currentPtr = *headOfList;
if ( currentPtr != NULL ) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if ( previousPtr == NULL ) {
newPtr->next = *headOfList;
*headOfList = newPtr;
}
else {
previousPtr->next = newPtr;
newPtr->next = currentPtr;
}
}
else {
printf( "%c %c not inserted. There is no memory available.\n", firstname, lastname);
}
}
// Deletes customer from database
char delete( Customerptr *headOfList, char fname, char lname ) {
Customerptr previousPtr;
Customerptr currentPtr;
Customerptr tempPtr;
if ( ( fname == ( *headOfList )->firstname ) && ( lname == ( *headOfList )->lastname ) ) {
tempPtr = *headOfList;
*headOfList = ( *headOfList )->next;
free( tempPtr );
tempPtr = *headOfList;
*headOfList = ( *headOfList )->next;
free( tempPtr );
return fname, lname;
}
else {
previousPtr = *headOfList;
currentPtr = ( *headOfList )->next;
while ( ( currentPtr != NULL && currentPtr->firstname != fname ) && ( currentPtr != NULL && currentPtr->lastname != lname ) ) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->next = currentPtr->next;
free( tempPtr );
tempPtr = currentPtr;
previousPtr->next = currentPtr->next;
free( tempPtr );
return fname, lname;
}
}
return '\0';
}
void searchlist () {
}
void print( Customerptr *headOfList) {
printf("First name: ->%s \n", ( *headOfList )->firstname );
printf("Last name: ->%d \n", ( *headOfList )->lastname );
printf("Phonenumber: ->%d \n", ( *headOfList )->phonenumber );
printf("Company/Organization: ->%d \n", ( *headOfList )->comporg );
printf("Email: ->%d \n\n", ( *headOfList )->email );
}
void printlist( Customerptr *headOfList ) {
Customerptr currentPtr;
while( currentPtr != NULL )
{
print( headOfList );
*headOfList = ( *headOfList )->next;
}
}
int isEmpty( Customerptr cptr ) {
return cptr == NULL;
}
writefile(Customerptr *headOfList ) {
Customerptr currentPtr;
FILE *fp;
char c;
fp = fopen("customers.txt", "w");
if (fp == NULL) { // If file doesn't exist
printf("Cannot open file. \n");
return(0);
}
while( currentPtr != NULL )
{
fprintf (fp, "%s \n" , print( headOfList ));
*headOfList = ( *headOfList )->next;
}
}
My question is, in adding the linked list to the file, what am I doing wrong? & Also when searching the linked list to display the results with either first or last name, say for example "Feng", displays all results with first or last name "Feng".