hi was wondering can anyone help me
whenever i run this on Visual Studio, it keeps exiting with code 0
Code:
#include "stdafx.h"
#include "stdio.h" // NULL defined here
#include "stdlib.h" // malloc function here
#include <string.h> //for handling strings
#include <ctype.h>
char reg_num[21]; //used in get_new_value
struct TAXI_STRUCT
struct TaxiNode
{
char* pszRegistrationNo;
TaxiNode* pNext;
TaxiNode* pPrevious;
};
struct TaxiQueue
{
TaxiNode* pFirstTaxi;
TaxiNode* pLastTaxi;
};
typedef struct node *Listpointer;
/*typedef struct node
{
char value[21];
struct node *next;
}*Listpointer; */
//global variables
//prototypes
void menu(char choice[21]);
void read_strings(char s [21], int n);
void get_new_value(Listpointer ptr);
void arrive(Listpointer ptr);
void print(Listpointer ptr);
int search(Listpointer ptr, char search_string[]);
void leave(Listpointer ptr);
void front(Listpointer ptr);
void back(Listpointer ptr);
void empty(Listpointer ptr);
c.:
#include "linkedlist.h"
int main()
{
//variable declarations
char choice[21];
char search_string[21];
int diff; //for strcmp()
int done;
Listpointer list;
//initialise head of list
list = (Listpointer)malloc(sizeof(struct node));
list -> next = NULL;
strcpy(list -> value, "EMPTY");
//call the menu
menu(choice);
diff = strcmp(choice, "exit");
while(diff != 0)
{
done = 0;
diff = strcmp(choice, "arrive");
//arrive choice
if(diff == 0)
{
get_new_value(list);
arrive(list);
done = 1;
}
diff = strcmp(choice, "leave");
//leave choice
if(diff == 0)
{
leave(list);
done = 1;
}
diff = strcmp(choice, "search");
//search choice
if(diff == 0)
{
printf("Enter a registration number:\n");
read_strings(search_string, 1);
search(list,search_string);
done = 1;
}
diff = strcmp(choice, "print");
//print choice
if(diff == 0)
{
print(list);
done = 1;
}
diff = strcmp(choice, "front");
//front choice
if(diff == 0)
{
front(list);
done = 1;
}
diff = strcmp(choice, "back");
//back choice
if(diff == 0)
{
back(list);
done = 1;
}
diff = strcmp(choice, "empty");
//empty choice
if(diff == 0)
{
empty(list);
done = 1;
}
//default choice
if(done == 0)
printf("Choice not recognised. ");
//recall menu
menu(choice);
diff = strcmp(choice,"exit");
}
return (1);
}
//system menu
void menu(char choice[21])
{
//display the menue
printf("Please choose from one of the following\n");
printf("1.Arrive\n");
printf("2.Depart\n");
printf("3.Print\n");
printf("4.Find\n");
printf("0.exit\n");
read_strings(choice, 1);
printf("\n");
}
//read in a string to a given array
void read_strings(char s [21], int n)
{
int i;
for(i = 0; i < n; i++)
{
gets(s[i]);
}
}
//validates the value to be entered into the list
void get_new_value(Listpointer ptr)
{
int done = 1;
int diff = 0; //for strcmp()
int i;
while(done == 1)
{
done = 0;
printf("enter the registration number, 'empty' to abort\n");
read_strings(reg_num, 1);
//convert to lower case
for(i = 0; reg_num[i]; i++)
{
reg_num[i] = toupper(reg_num[i]);
}
//clear any symbols
//check for abort entry
diff = strcmp(reg_num,"EMPTY");
if(diff != 0)
{
//if abort entry found, set done to 1
done = 1;
}
if(done == 1)
{
done = 0;
while(ptr != NULL)
{ // not end
diff = strcmp(ptr -> value, reg_num);
if((diff == 0) && (done != 1))
{
done = 1; //found reg
}
ptr = ptr -> next;
} // end while
if(done == 1)
{
//error message for car found in array.
printf("That car is already in the list\n");
}
}
}
}
void arrive(Listpointer ptr)
{
Listpointer new; //now declared here
int diff; //for strcmp()
diff = strcmp(reg_num, "EMPTY");
if(diff != 0)
{
while(ptr -> next != NULL)
{
ptr = ptr -> next;
}
new = (Listpointer)malloc(sizeof(struct node));
strcpy(new -> value, reg_num);
ptr -> next = new;
new -> next = NULL;
}
else
{
printf("Car not added\n");
}
}
void leave(Listpointer ptr)
{
Listpointer delete; //now declared here
if(ptr -> next != NULL)
{
printf("taxi %s has left the rank\n", ptr -> next -> value);
delete = (Listpointer)malloc(sizeof(struct node));
delete -> next = ptr -> next;
if(ptr -> next == NULL)
{
free(ptr -> next);
}
else
{
ptr -> next = (ptr -> next -> next);
}
free(delete -> next);
free(delete);
}
else
{
printf("the list is currently empty\n");
}
}
int search(Listpointer ptr, char search_string[])
{
int diff; //for strcmp()
int i;
int taxiNumber = 0;
//convert to lower case
for(i = 0; search_string[i]; i++)
{
search_string[i] = toupper(search_string[i]);
}
while(ptr != NULL)
{
diff = strcmp(ptr -> value, search_string);
if(diff == 0)
{
printf("taxi %s is %d from the top of the list\n", search_string, taxiNumber);
return (1);
}
ptr = ptr -> next;
taxiNumber ++;
}
printf("taxi %s not found\n", search_string);
return (0);
}
void print(Listpointer ptr)
{
int taxiNumber = 0;
int diff; //for strcmp()
while(ptr != NULL)
{
diff = strcmp(ptr -> value, "EMPTY");
if(diff != 0)
{
printf("%d. %s\n", taxiNumber, ptr -> value);
}
taxiNumber ++;
ptr = ptr -> next;
}
}
void front(Listpointer ptr)
{
if(ptr -> next != NULL)
{
ptr = ptr -> next;
printf("The taxi at the front of the list is %s\n", ptr -> value);
}
else
{
printf("The queue is currently empty\n");
}
}
void back(Listpointer ptr)
{
if(ptr -> next != NULL)
{
while(ptr -> next != NULL)
{
ptr = ptr -> next;
}
printf("The taxi at the back of the queue is %s\n", ptr -> value);
}
else
{
printf("The list is currently empty\n");
}
}
void empty(Listpointer ptr)
{
if(ptr -> next != NULL)
{
printf("The list is not empty\n");
}
else
{
printf("The list is empty\n");
}
}