Witam, otóż nie dziala mi dobrze ten kod. Może moglibyście wskazać mi błąd. > kod programu
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#define MAX_AUTOR 10
#define MAX_TYTUL 10
typedef struct ksiazka {
char autor[MAX_AUTOR], tytul[MAX_TYTUL];
int rokWydania, liczbaStron;
double cena;
char wypozyczona;
int ileWypozyczen;
} ksiazka;
void fgets2(char *s, int n)
{
char *r = fgets(s, n, stdin);
if (r) {
int len = strlen(s);
if (len>0 && s[len - 1] == '\n') {
s[len - 1] = 0;
}
else {
while (getchar() != '\n')
continue;
}
}
}
void wczytajAutor(char *s)
{
bool ok;
if(s!=NULL)
do {
printf("Podaj autora (tylko litery)> ");
fgets2(s, MAX_AUTOR);
ok = true;
int len = strlen(s);
for (int i = 0; i<len; ++i) {
if (!isalpha(s[i])) {
ok = false;
break;
}
}
if (!ok)
printf("Podany napis zawiera znaki inne niz litery\n");
} while (!ok);
}
void wczytajTytul(char *s)
{
bool ok;
do {
printf("Podaj tytul ksiazki(litery i liczby)> ");
fgets2(s, MAX_TYTUL);
ok = true;
int len = strlen(s);
for (int i = 0; i<len; ++i)
if (!isalnum(s[i]))
{
ok = false;
break;
}
} while (!ok);
}
void wczytajRokWydania(int *rok)
{
bool ok;
do
{
ok = true;
printf("Podaj Rok Wydania ksiazki> ");
scanf("%d", rok);
if (rok <= 0 && rok > 2018)
{
ok = false;
printf("rok jest niepoprawny");
}
} while (!ok);
}
void liczbaStron(int *lstron)
{
bool ok;
do
{
ok = true;
printf("Podaj liczbe stron> ");
scanf("%d", lstron);
if (lstron < 0)
{
ok = false;
printf("podaj prawidlawa ilosc stron (>0)");
}
} while (!ok);
}
void cenaKsiazki(double *s)
{
bool ok;
do
{
ok = true;
printf("Podaj cene ksiazki> ");
scanf("%lf", s);
if (s <= 0)
{
ok = false;
}
} while (!ok);
}
void wypozyczona(char *s)
{
printf("wypozyczona?> ");
scanf("%s", s);
}
void iloscWypozyczen(int *ilosc)
{
printf("Podaj ilosc wypozyczen> ");
scanf("%d", ilosc);
}
ksiazka wczytajKsiazka(ksiazka *k)
{
fflush(stdin);
wczytajAutor(k->autor);
fflush(stdin);
wczytajTytul(k->tytul);
wczytajRokWydania(&k->rokWydania);
liczbaStron(&k->liczbaStron);
cenaKsiazki(&k->cena);
wypozyczona(&k->wypozyczona);
iloscWypozyczen(&k->ileWypozyczen);
}
void wypiszKsiazka(const ksiazka *k) {
printf("Autor:%s\n", k->autor);
printf("Tytul:%s\n", k->tytul);
printf("Rok wydania:%d\n", k->rokWydania);
printf("Liczba Stron:%d\n", k->liczbaStron);
printf("Cena:%lf\n", k->cena);
printf("wypozyczona: %s\n", k->wypozyczona);
printf("Ile wypozyczen:%d\n", k->ileWypozyczen);
}
ksiazka * wczytajKsiazki(int n) {
ksiazka *tab = (ksiazka*)malloc(sizeof(ksiazka)*n);
if (tab) {
for (int i = 0; i<n; ++i) {
printf("Podaj dane ksiazki %d\n", i);
wczytajKsiazka(&tab[i]);
}
}
return tab;
}
void wypiszKsiazki(ksiazka *tab, int n) {
for (int i = 0; i<n; ++i) {
printf("Ksiazka %d\n", i);
wypiszKsiazka(&tab[i]);
}
}
int main()
{
int n = 2;
ksiazka k1;
//wczytajKsiazka(&k1);
//wypiszKsiazka(&k1);
ksiazka *tab = wczytajKsiazki(n);
wypiszKsiazki(tab, n);
if (tab)
free(tab);
return 0;
}