This is what i have so far. I need to write a function to add and remove a field from a file. I called the add function in main but do not know what to do next.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct moto
{
char make[10];
char model[10];
char trim[10];
int year;
int price;
struct moto* next;
} MOTO;
char menu(void);
MOTO* getdata (MOTO* head);
MOTO* position(MOTO* head, MOTO* adding);
void print(MOTO* head);
void add(MOTO* arrival);
void tofile(MOTO* head);
int main (void)
{
MOTO* head = NULL;
MOTO* arrival;
char userop;
int num;
head = getdata(head);
print(head);
while ((userop=menu()) != 'Q')
{
switch(userop)
{
case 'P': print(head);
break;
case 'A':
{
add(&arrival);
// <--- what goes here?
}
break;
case 'R':
break;
case 'M':
break;
case 'O': tofile(head);
break;
default: printf("\n Invalid. Enter choice again");
break;
}
}
return 0;
}
void add(MOTO* arrival)
{
printf("Enter the year of the motocycle: ");
scanf("%d",&arrival->year);
printf("Enter motocycle make: ");
scanf("%s",arrival->make);
printf("Enter motocycle model: ");
scanf("%s",arrival->model);
printf("Enter motocycle trim: ");
scanf("%s",arrival->trim);
printf("Enter motocycle price: ");
scanf("%d",&arrival->price);
return;
}
MOTO* getdata (MOTO* head)
{
FILE* data;
MOTO* ptr;
char temp [255];
if ((data=fopen ("moto.txt", "r")) == NULL)
{
printf("Error opening file.");
exit(100);
}
while (fgets (temp, sizeof(temp), data) != NULL)
{
ptr = (MOTO*) malloc (sizeof (MOTO));
sscanf (temp, "%d%s%s%s%d",&ptr->year, ptr->make, ptr->model, ptr->trim, &ptr->price);
ptr->next = NULL;
head = position(head, ptr);
}
fclose (data);
return head;
}
MOTO* position(MOTO* head, MOTO* adding)
{
MOTO* current;
MOTO* previous;
if(!head)
head = adding;
else
{
current = head;
previous = NULL;
while (current && adding->year < current->year)
{
previous = current;
current = current->next;
}
if (previous)
{
adding->next = current;
previous->next = adding;
}
else
{
adding->next = head;
head = adding;
}
}
return head;
}
void print(MOTO* head)
{
MOTO* walker;
walker = head;
printf("\n");
while(walker)
{
printf("%d %s %s %s $%d\n", walker->year, walker->make, walker->model, walker->trim, walker->price);
walker=walker->next;
}
return;
}
void tofile(MOTO* head)
{
FILE* fout;
MOTO* walker;
walker = head;
fout = fopen("motoout.txt","w");
while(walker)
{
fprintf(fout,"%d %s %s %s %d \n",walker->year, walker->make, walker->model, walker->trim, walker->price);
walker = walker->next;
}
fclose(fout);
printf("Data printed to file. \n");
return;
}
char menu(void)
{
char choice;
printf("\n**********************************************");
printf("\n1. Enter 'P' to print the list of motorcycles");
printf("\n2. Enter 'A' to add a motorcycle");
printf("\n3. Enter 'R' to remove a motorcycle");
printf("\n4. Enter 'M' to modify a field.");
printf("\n5. Enter 'O' to output to file.");
printf("\n6. Enter 'Q' to quit.");
printf("\n**********************************************");
printf("\nPlease enter a choice: ");
scanf(" %c",&choice);
return toupper(choice);
}