Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We were given a code example to look at and understand how it works but gives al

ID: 3677395 • Letter: W

Question

We were given a code example to look at and understand how it works but gives alot of errors. Is there any thing important missing from the code? Also this is a c program.

#include <stdio.h>
#include <stdlib.h> // malloc is defined in this library
#include <string.h> // string operations are defined in this library
struct contact {
   char name[30];
   char phone[20];
   char address[50];
   struct contact *next;
}*head = NULL;

char *file_name; // Use a string for file name
// forward declaration
void menu();
void branching(char c);
struct contact* find_node(char *str, int *position);
void display_node(struct contact *node, int index);
int insert();

int deletion();
int modify();
int search_name();
void display_all();
void load_file();
void save_file();

void menu() {
   printf(" MENU ");
   printf("---- ");
   printf("i: Insert a new entry. ");
   printf("d: Delete an entry. ");
   printf("m: Modify an entry. ");
   printf("s: Search by name for an entry. ");
   printf("p: Print all entries. ");
   printf("q: Quit the program. ");
   printf("Please enter your choice (i, d, m, s, p, or q) --> ");
}
void branching(char c) {
   switch (c) {
   case 'i': if (insert() != 0)
       printf("INSERTION OPERATION FAILED. ");
       elseprintf("INSERTED NODE IN THE LIST SUCCESSFULLY. ");
       break;
   case 'd': if (deletion() != 0)
       printf("DELETION OPERATION FAILED. ");
           else
               printf("DELETED THE ABOVE NODE SUCCESSFULLY. ");
       break;
   case 'm': if (modify() != 0)
       printf("MODIFY OPERATION FAILED. ");
           else
               printf("MODIFIED THE ABOVE NODE SUCCESSFULLY. ");
       break;
   case 's': if (search_name() != 0)
       printf("SEARCH FAILED. ");
           else
               printf("SEARCH FOR THE NODE SUCCESSFUL. ");
       break;
   case 'p': display_all();
       break;
   case 'q': save_file();
       break;
   default: printf("ERROR - Invalid input. ");
       printf("Try again..... ");
       break;
   }
   return;
}
int insert() {
   struct contact *node;
   char sname[30];
   int index = 1;
   printf(" Insertion module............... ");
   printf("Enter the name of the person to be inserted: ");
   scanf("%s", sname);
   node = find_node(sname, &index); // find duplicates
   if (node != NULL) {
       printf("ERROR - Duplicate entry not allowed. ");
       printf("A entry is found in the list at index %d. ", index);
       display_node(node, index);
       return -1;
   }
   else {
       node = (struct contact*) malloc(sizeof(struct contact));
       if (node == NULL) {
           printf("ERROR - Could not allocate memory ! ");
           return -1;
       }
       strcpy(node->name, sname);
       printf("Enter telephone number: ");
       scanf("%s", node->phone);
       printf("Enter address: ");
       scanf("%s", node->address);
       node->next = head;
       head = node;
       return 0;
   }
}
int deletion() {
   char sname[30];
   struct contact *temp, *prev;
   int index = 1;
   printf(" Deletion module............... ");
   printf("Please enter the name of the person to be deleted: ");
   scanf("%s", sname);
   temp = head;
   while (temp != NULL)
   if (stricmp(sname, temp->name) != 0) { // case insensitive strcmp
       prev = temp;
       temp = temp->next;
       index++;
   }
   else {
       printf("Person to be deleted is found at index %d.", index);
       display_node(temp, index);
       if (temp != head)
           prev->next = temp->next;
       else
           head = head->next;
       free(temp);
       return 0;
   }
   printf("The person with name '%s' does not exist. ", sname);
   return -1;
}
int modify() {
   struct contact *node;
   char sname[30];
   int index = 1;
   printf(" Modification module............... "); printf("Enter the name whose record is to be modified in the ");
   printf("database: ");
   scanf("%s", sname);
   node = find_node(sname, &index);
   if (node != NULL) {
       printf("Person to be modified is found at index %d.", index);
       display_node(node, index);
       printf(" Enter the new telephone number of this person: ");
       scanf("%s", node->phone);
       printf("Enter the new address of this person: ");
       scanf("%s", node->address);
       return 0;
   }
   else {
       printf("The person with name '%s' does not exist ", sname);
       printf("database. ");
       return -1;
   }
}
int search_name() {
   struct contact *node;
   char sname[30];
   int index = 1;
   printf(" Search_name module............... ");
   printf("Please enter the name to be searched in the database: ");
   scanf("%s", sname);
   node = find_node(sname, &index);
   if (node != NULL) {
       printf("Person searched is found at index %d.", index);
       display_node(node, index);
       return 0;
   }
   else {
       printf("The person '%s' does not exist. ", sname);
       return -1;
   }
}
void display_all() {
   struct contact *node;
   int counter = 0;
   printf(" Display module...............");
   node = head;
   while (node != NULL) {
       display_node(node, counter++);
       node = node->next;
   }
   printf(" No more records. ");
}
void load_file( ) {
   FILE *file_descriptor;
   struct contact *node, *temp;
   char str[30];
file_descriptor = fopen(file_name, "r");
   if (file_descriptor != NULL) {
       while (fread(str, 30, 1, file_descriptor) == 1) {
           node = (struct contact*) malloc(sizeof(struct contact));
           strcpy(node->name, str);
           fread(node->phone, 20, 1, file_descriptor);
           fread(node->address, 50, 1, file_descriptor);
           if (head != NULL)
               temp->next = node;
           else
               head = node;
           node->next = NULL;
           temp = node;
       }
       fclose(file_descriptor);
   }
}
void save_file() {
   FILE *file_descriptor;
   struct contact *node;
   file_descriptor = fopen(file_name, "w");
   if (file_descriptor != NULL) {
       node = head;
       while (node != NULL) {
           fwrite(node->name, 30, 1, file_descriptor);
           fwrite(node->phone, 20, 1, file_descriptor);
           fwrite(node->address, 50, 1, file_descriptor);
           node = node->next;
       }
   }
   else {
       printf(" ERROR - Could not open file for saving data ! ");
       getchar();
       exit(-1);
   }
}struct contact* find_node(char *str, int *position) {
   struct contact *temp = head;
   while (temp != NULL) {
       if (stricmp(str, temp->name) != 0) { // case insensitive strcmp
           temp = temp->next;
           (*position)++;
       }
       else
           return temp;
   }
   return NULL;
}
void display_node(struct contact *node, int index) {
   printf(" RECORD %d: ", index);
   printf(" Name: %s ", node->name);
   printf(" Telephone: %s ", node->phone);
   printf(" Address: %s ", node->address);
}

int main(int argc, char *argv[]) {

   char ch;
   if (argc != 2) { // Two command line parameters required
       printf("Command Line Parameters Required ! ");
       printf("Try again...... ");
       getchar(); // enter any character to return
       return -1;
   }
   printf("SINGLY LINKED LIST ");
   printf("******************");
   file_name = argv[1]; // assign person.dbms to file_name
   load_file();
   do {
       menu();
       //fflush(stdin); // Flush the standard input buffer
       ch = tolower(getchar()); // read a char, convert to lower case
       branching(ch);
   } while (ch != 'q');
   return 0;
}

When I try to run the program I get the following errors:

Error   5   error C2065: 'FILE' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   177   1   CSC240

Error   18   error C2065: 'FILE' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   198   1   CSC240

Error   6   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   177   1   CSC240

Error   8   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   180   1   CSC240
Error   10   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   181   1   CSC240
Error   13   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   182   1   CSC240
Error   14   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   185   1   CSC240
Error   15   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   186   1   CSC240
Error   17   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   194   1

Error   8   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   180   1   CSC240

Error   10   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   181   1   CSC240
Error   13   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   182   1   CSC240
Error   14   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   185   1   CSC240
Error   15   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   186   1   CSC240
Error   17   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   194   1   CSC240
Error   19   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   198   1   CSC240
Error   21   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   200   1   CSC240
Error   22   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   201   1   CSC240
Error   25   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   204   1   CSC240
Error   26   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   205   1   CSC240
Error   27   error C2065: 'file_descriptor' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   206   1   CSC240
Error   2   error C2065: 'stdin' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   25   1   CSC240
Error   29   error C2065: 'stdin' : undeclared identifier   e:csc_workspacespring 2016 csc 240csc240csc240contactbook.c   249   1   CSC240

Explanation / Answer

/************* The code is running fine after making changes to the some lines highlighted in the code *****/

#include <stdio.h>
#include <stdlib.h> // malloc is defined in this library
#include <string.h> // string operations are defined in this library
struct contact {
char name[30];
char phone[20];
char address[50];
struct contact *next;
}*head = NULL;
char *file_name; // Use a string for file name
// forward declaration
void menu();
void branching(char c);
struct contact* find_node(char *str, int *position);
void display_node(struct contact *node, int index);
int insert();
int deletion();
int modify();
int search_name();
void display_all();
void load_file();
void save_file();
void menu() {
printf(" MENU ");
printf("---- ");
printf("i: Insert a new entry. ");
printf("d: Delete an entry. ");
printf("m: Modify an entry. ");
printf("s: Search by name for an entry. ");
printf("p: Print all entries. ");
printf("q: Quit the program. ");
printf("Please enter your choice (i, d, m, s, p, or q) --> ");
}
void branching(char c) {
switch (c) {
case 'i': if (insert() != 0)
printf("INSERTION OPERATION FAILED. ");
else printf("INSERTED NODE IN THE LIST SUCCESSFULLY. "); // else and printf shhould be separated
break;
case 'd': if (deletion() != 0)
printf("DELETION OPERATION FAILED. ");
else
printf("DELETED THE ABOVE NODE SUCCESSFULLY. ");
break;
  
case 'm': if (modify() != 0)
printf("MODIFY OPERATION FAILED. ");
else
printf("MODIFIED THE ABOVE NODE SUCCESSFULLY. ");
break;
case 's': if (search_name() != 0)
printf("SEARCH FAILED. ");
else
printf("SEARCH FOR THE NODE SUCCESSFUL. ");
break;
case 'p': display_all();
break;
case 'q': save_file();
break;
default: printf("ERROR - Invalid input. ");
printf("Try again..... ");
break;
}
return;
}
int insert() {
struct contact *node;
char sname[30];
int index = 1;
printf(" Insertion module............... ");
printf("Enter the name of the person to be inserted: ");
scanf("%s", sname);
node = find_node(sname, &index); // find duplicates
if (node != NULL) {
printf("ERROR - Duplicate entry not allowed. ");
printf("A entry is found in the list at index %d. ", index);
display_node(node, index);
return -1;
}
else {
node = (struct contact*) malloc(sizeof(struct contact));
if (node == NULL) {
printf("ERROR - Could not allocate memory ! ");
return -1;
}
strcpy(node->name, sname);
printf("Enter telephone number: ");
scanf("%s", node->phone);
printf("Enter address: ");
scanf("%s", node->address);
node->next = head;
head = node;
return 0;
}
}
int deletion() {
char sname[30];
struct contact *temp, *prev;
int index = 1;
printf(" Deletion module............... ");
printf("Please enter the name of the person to be deleted: ");
scanf("%s", sname);
temp = head;
while (temp != NULL)
if (strcmp(sname, temp->name) != 0) { // case insensitive strcmp , it should be strcmp not stricmp
prev = temp;
temp = temp->next;
index++;
}
else {
printf("Person to be deleted is found at index %d.", index);
display_node(temp, index);
if (temp != head)
prev->next = temp->next;
else
head = head->next;
free(temp);
return 0;
}
printf("The person with name '%s' does not exist. ", sname);
return -1;
}
int modify() {
struct contact *node;
char sname[30];
int index = 1;
printf(" Modification module............... "); printf("Enter the name whose record is to be modified in the ");
printf("database: ");
scanf("%s", sname);
node = find_node(sname, &index);
if (node != NULL) {
printf("Person to be modified is found at index %d.", index);
display_node(node, index);
printf(" Enter the new telephone number of this person: ");
scanf("%s", node->phone);
printf("Enter the new address of this person: ");
scanf("%s", node->address);
return 0;
}
else {
printf("The person with name '%s' does not exist ", sname);
printf("database. ");
return -1;
}
}
int search_name() {
struct contact *node;
char sname[30];
int index = 1;
printf(" Search_name module............... ");
printf("Please enter the name to be searched in the database: ");
scanf("%s", sname);
node = find_node(sname, &index);
if (node != NULL) {
printf("Person searched is found at index %d.", index);
display_node(node, index);
return 0;
}
else {
printf("The person '%s' does not exist. ", sname);
return -1;
}
}
void display_all() {
struct contact *node;
int counter = 0;
printf(" Display module...............");
node = head;
while (node != NULL) {
display_node(node, counter++);
node = node->next;
}
printf(" No more records. ");
}
void load_file( ) {
FILE *file_descriptor;
struct contact *node, *temp;
char str[30];
file_descriptor = fopen(file_name, "r");
if (file_descriptor != NULL) {
while (fread(str, 30, 1, file_descriptor) == 1) {
node = (struct contact*) malloc(sizeof(struct contact));
strcpy(node->name, str);
fread(node->phone, 20, 1, file_descriptor);
fread(node->address, 50, 1, file_descriptor);
if (head != NULL)
temp->next = node;
else
head = node;
node->next = NULL;
temp = node;
}
fclose(file_descriptor);
}
}
void save_file() {
FILE *file_descriptor;
struct contact *node;
file_descriptor = fopen(file_name, "w");
if (file_descriptor != NULL) {
node = head;
while (node != NULL) {
fwrite(node->name, 30, 1, file_descriptor);
fwrite(node->phone, 20, 1, file_descriptor);
fwrite(node->address, 50, 1, file_descriptor);
node = node->next;
}
}
else {
printf(" ERROR - Could not open file for saving data ! ");
getchar();
exit(-1);
}
}struct contact* find_node(char *str, int *position) {
struct contact *temp = head;
while (temp != NULL) {
   if (strcmp(str, temp->name) != 0) { // case insensitive strcmp  it should be strcmp not stricmp
temp = temp->next;
(*position)++;
}
else
return temp;
}
return NULL;
}
void display_node(struct contact *node, int index) {
printf(" RECORD %d: ", index);
printf(" Name: %s ", node->name);
printf(" Telephone: %s ", node->phone);
printf(" Address: %s ", node->address);
}
int main(int argc, char *argv[]) {
char ch;
if (argc != 2) { // Two command line parameters required
printf("Command Line Parameters Required ! ");
printf("Try again...... ");
getchar(); // enter any character to return
return -1;
}
printf("SINGLY LINKED LIST ");
printf("******************");
file_name = argv[1]; // assign person.dbms to file_name
load_file();
do {
menu();
//fflush(stdin); // Flush the standard input buffer
ch = tolower(getchar()); // read a char, convert to lower case
branching(ch);
} while (ch != 'q');
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote