This is where I'm at right now, output for shipName isn't working correctly
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct N{
int j;
char shipName[25];
char shipType;
int hostage;
int shipValue;
int ransom;
struct N* next;
}*LOG;
void add(char ship[], char type, int hostage, int shipValue, int ransom);
void display();
int length();
int main()
{
LOG=NULL;
int choice, hostage, ransom, shipValue, loc;
char ship[25], type;
while(1){
printf("Main Menu\n");
printf("1. See A Captured Ship's Information\n");
printf("2. Add a Ship\n");
printf("3. Remove a Ship\n");
printf("4. Generate Report (textfile)\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf(" \nThere Are %d ships in the list\n",length());
display();
break;
case 2:{
printf("Shipname:\n");
scanf("%s", ship);
printf("Ship Type(T = Tanker, S = Schooner, Y= Yacht):\n");
scanf("%s", &type);
printf("Hostage:\n");
scanf("%d", &hostage);
printf("Ship Value:\n");
scanf("%d", &ransom);
printf("RANSOM:\n");
scanf("%d", &shipValue);
add(ship, type, hostage, shipValue, ransom); }
break;
case 3: {
printf(" \nWhich Entry Do You Wish To Delete ");
scanf("%d",&loc);
delLoc(loc);
display(); }
break;
case 4:{
FILE *fp;
fp=fopen("log.txt", "w");
struct N *this;
if(LOG == NULL)
{
printf("This is Empty\n");
break;
}
for ( this = LOG ; this != NULL ; this = this->next ) {
fwrite(this, sizeof(*this), 1, fp);
}
}
break;
case 5:
exit(0);
}
}
}
void add(char ship[], char type, int hostage, int shipValue, int ransom)
{
struct N *temp1, *temp2;
static int j=0;
temp1=(struct N *)malloc(sizeof(struct N));
temp1->j=j++;
strcpy(temp1->shipName, ship);
temp1->shipType=type;
temp1->hostage=hostage;
temp1->shipValue=shipValue;
temp1->ransom=ransom;
temp2=LOG;
if(LOG == NULL)
{
//First Node.
LOG=temp1;
LOG->next=NULL;
}
else
{
// End of the list.
while(temp2->next != NULL)
temp2=temp2->next;
// End of the list.
temp1->next=NULL;
temp2->next=temp1;
}
}
int length()
{
struct N *ptr;
int count=0;
ptr=LOG;
while(ptr != NULL)
{
ptr=ptr->next;
count++;
}
return(count);
}
void display()
{
struct N *ptr;
ptr=LOG;
if(ptr==NULL)
{
printf("\nNo Ship Logs");
}
else
{
while(ptr != NULL)
{
printf("Entry Number %d\n", ptr->j);
printf("Ship Name: %s\n", ptr->shipName);
printf("Type:%c\n", ptr->shipType);
printf("Number of Hostages: %d\n", ptr->hostage);
printf("Ship Value: %d\n", ptr->shipValue);
printf("Ransom Demanded: %d\n", ptr->ransom);
ptr=ptr->next;
}
printf("\n");
}
}
int delLoc(int loc)
{
struct N *prev_ptr, *ptr;
int i;
ptr=LOG;
if(loc > (length()) || loc <= 0)
{
printf("\nDeletion of LOG is not possible\n ");
}
else
{
// If the location is starting of the list
if (loc == 1)
{
LOG=ptr->next;
free(ptr);
return 0;
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=ptr;
ptr=ptr->next;
}
prev_ptr->next=ptr->next;
free(ptr);
}
}
return 1;
}