Witam. Mam problem którego nie potrafie dokładnie rozszyfrować wiem że program psuje funkcja scanf.
Tworze baze filmów opierającą się na liście chcę stworzyć menu w którym będe wybierał co chce w danym momencie zrobić i poprzez switch case wywołać odpowiednią funkcje. Program dobrze wczytuje tytuł filmu i zapisuje podczas, gdy wywołam samą funkcje AddMovie() jeśli zrobie to poprzez to menu, które stworzyłem program nie wczytuje.
Kod działający:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MOVIE_NAME_LENGHT 100
#define MAX_DIRECTOR_NAME 50
struct movie{
int id;
char movieName[MAX_MOVIE_NAME_LENGHT];
short int yearOfProduction;
char directorName[MAX_DIRECTOR_NAME];
char typeOfMovie[MAX_MOVIE_NAME_LENGHT];
short int amountOfCopies;
short int amountOfRented;
struct movie *next;
};
struct movie *mainPointer = NULL;
void AddMovie(struct movie *pointer)
{
struct movie *newMovie = (struct movie *)malloc(sizeof(struct movie));
if(pointer == NULL)
pointer = newMovie;
if(newMovie != NULL)
{
printf("Przydzielono pamiec. \n");
printf("Podaj tytul: ");
fgets(newMovie->movieName,MAX_MOVIE_NAME_LENGHT,stdin);
printf("%s",newMovie->movieName);
}
}
int main()
{
AddMovie(mainPointer);
return 0;
}
Kod niedziałający:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MOVIE_NAME_LENGHT 100
#define MAX_DIRECTOR_NAME 50
struct movie{
int id;
char movieName[MAX_MOVIE_NAME_LENGHT];
short int yearOfProduction;
char directorName[MAX_DIRECTOR_NAME];
char typeOfMovie[MAX_MOVIE_NAME_LENGHT];
short int amountOfCopies;
short int amountOfRented;
struct movie *next;
};
struct movie *mainPointer = NULL;
void AddMovie(struct movie *pointer)
{
struct movie *newMovie = (struct movie *)malloc(sizeof(struct movie));
if(pointer == NULL)
pointer = newMovie;
if(newMovie != NULL)
{
printf("Przydzielono pamiec. \n");
printf("Podaj tytul: ");
fgets(newMovie->movieName,MAX_MOVIE_NAME_LENGHT,stdin);
printf("%s",newMovie->movieName);
}
}
int main()
{
int menu = 0;
printf("[1] Dodaj film \n");
printf("[2] Usun film \n");
scanf("%d",&menu);
switch(menu)
{
case 1: AddMovie(mainPointer); break;
case 2: printf("Case 2"); break;
default: printf("Error");
}
return 0;
}
proszę o pomoc :)(