Proszę o pomoc przy funkcjach dodających film oraz klienta. Niestety nie działają.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 20
struct movies {
int id;
char title[N];
char producer[N];
char year[N];
//bool availibity;
struct movies *next;
};
struct movies_pointers
{
struct movies *head , *tail;
}movie;
void load_movie(struct movies *movie){
puts("Title: ");
scanf("%s",movie->title);
puts("Producer: ");
scanf("%s",movie->producer);
puts("Publication date: ");
scanf("%s",movie->year);
movie->next=NULL;
}
struct movies* add_movie(struct movies *pointer){
if(pointer){
struct movies* new_movie = (struct movies *) malloc(sizeof(struct movies));
if(new_movie){
load_movie(new_movie);
new_movie->next=pointer;
new_movie->id=new_movie->next->id+1;
return new_movie;
}
else{
printf("Malloc error!\n");
return pointer;
}
}
else{
printf("Lack of list!\n");
return pointer;
}
}
void print_movies(struct movies_pointers movie){
puts("ID \t TITLE \t\t PRODUCER \t\t PUBLICATION DATE");
while(movie.head){
printf("%d \t %s \t\t %s \t\t %s \n",movie.head->id, movie.head->title, movie.head->producer, movie.head->year);
movie.head=movie.head->next;
}
puts(" ");
}
void save_movies(struct movies *movie)
{
struct movies *tmp = movie;
if(movie)
{
FILE *movies_txt = NULL;
movies_txt = fopen("movies.txt","w");
while(tmp!=NULL)
{
fprintf(movies_txt, "%s", tmp->title);
fprintf(movies_txt, "%s\n", tmp->producer);
fprintf(movies_txt, "%s\n", tmp->year);
tmp = tmp->next;
}
fclose(movies_txt);
}
else
{
printf("Error");
}
}
struct movies *movies_from_file(struct movies *pointer){
if(pointer){
FILE * movies_txt = fopen("movies.txt","r");
while(!feof(movies_txt))
{
struct movies* new_movie = (struct movies *) malloc(sizeof(struct movies));
if(new_movie){
fscanf(movies_txt,"%s",new_movie->title);
fscanf(movies_txt,"%s",new_movie->producer);
fscanf(movies_txt,"%s",new_movie->year);
new_movie->next=pointer;
new_movie->id=new_movie->next->id+1;
pointer=new_movie;
}
else{
printf("Malloc error\n");
return pointer;
}
}
fclose(movies_txt);
return pointer;
}
else{
printf("Lack of list");
return pointer;
}
}
void how_many_movies(struct movies_pointers movie){
int wynik=0;
while(movie.head){
movie.head=movie.head->next;
wynik++;
}
printf("The amount of movies in the base is: %d\n\n",wynik);
}
void search_by_title(struct movies_pointers movie, char tag[N]){
puts("What are you looking for?\n");
printf("%s\n",tag);
while(movie.head){
if(movie.head->title==tag){
printf("%d \t %s \t %s \t %s",movie.head->id, movie.head->title, movie.head->producer, movie.head->year);
movie.head=movie.head->next;
}
else{
puts("No results.\n");
break;
}
}
}
struct clients {
int id;
char name[N];
char surname[N];
struct clients *next;
};
struct clients_pointers
{
struct clients *head , * tail;
}client;
void load_client(struct clients *client){
puts("Name: ");
scanf("%s",client->name);
puts("Surname: ");
scanf("%s",client->surname);
client->next=NULL;
}
struct clients* add_client(struct clients *pointer){
if(pointer){
struct clients *new_client = (struct clients*) malloc(sizeof(struct clients));
if(new_client){
load_client(new_client);
new_client->next=pointer;
new_client->id=new_client->next->id+1;
return new_client;
}
else{
printf("Malloc error!\n");
return pointer;
}
}
else{
printf("Lack of list!\n");
return pointer;
}
}
void print_clients(struct clients_pointers client){
puts("ID \t NAME \t\t SURNAME");
while(client.head){
printf("%d \t %s \t\t %s \n",client.head->id, client.head->name, client.head->surname);
client.head=client.head->next;
}
puts(" ");
}
void save_clients(struct clients *client)
{
struct clients *tmp = client;
if(client)
{
FILE *clients_txt = NULL;
clients_txt = fopen("clients.txt","w");
while(tmp!=NULL)
{
fprintf(clients_txt, "%s", tmp->name);
fprintf(clients_txt, "%s\n", tmp->surname);
tmp = tmp->next;
}
fclose(clients_txt);
}
else
{
printf("Error");
}
}
struct clients *clients_from_file(struct clients *pointer){
if(pointer){
FILE * clients_txt = fopen("clients.txt","r");
while(!feof(clients_txt))
{
struct clients* new_client = (struct clients *) malloc(sizeof(struct clients));
if(new_client){
fscanf(clients_txt,"%s",new_client->name);
fscanf(clients_txt,"%s",new_client->surname);
new_client->next=pointer;
new_client->id=new_client->next->id+1;
pointer=new_client;
}
else{
printf("Malloc error\n");
return pointer;
}
}
fclose(clients_txt);
return pointer;
}
else{
printf("Lack of list");
return pointer;
}
}
int main()
{
int x;
puts("1 - LIST OF MOVIES \t 2 - LIST OF CLIENTS \t 3 - AMOUNT OF MOVIES \t 4 - SEARCHING \t 0 - CLOSE \t 5 - ADD MOVIE \t 6 - ADD CLIENT ");
scanf("%d",&x);
while(x!=0){
if(x==1){
print_movies(movie);
}else if(x==2){
print_clients(client);
}else if(x==3){
how_many_movies(movie);
}
else if(x==4){
search_by_title(movie,"Matrix");
}
else if(x==5){
add_movie(&movie);
}
else if(x==6){
add_client(&client);
}
else{
puts("wrong code.");
}
puts("\n\n\n\n1 - LIST OF MOVIES \t 2 - LIST OF CLIENTS \t 3 - AMOUNT OF MOVIES \t 4 - SEARCHING \t 0 - CLOSE \t 5 - ADD MOVIE \t 6 - ADD CLIENT ");
scanf("%d",&x);
}
return 0;
}