Reputation:
Context: We are in a library. We wrote two structures: Livre (book in english) with titre (title), nombre_pages (number of pages) and statut (borrowed already or not? )
Lecteur (the reader) (nom = name; prenom = firstname; nb_livres = number of books the reader has booked already; and a struct livres)
I'm trying to do a function in which the parameters are: 1) Array with different readers (structure Lecteur) 2) The size of the array (with a pointer because it will evolve) 3) The reader (structure Lecteur) that has to be delete of the array.
Here is my function:
#include <stdio.h>
struct Livre {
char titre[100];
int nombre_pages;
int statut; // Book already borrowed = 1, Available = 0
};
struct Lecteur {
char nom[100];
char prenom[100];
int nb_livres; // le nombre de livres dans le tableau "livres"
struct Livre* livres[100]; // livres deja empruntes (eventuellement rendus)
};
void desabonnement(struct Lecteur * plecteurs[], int * nombre_lecteurs,
struct Lecteur * lect) {
struct Lecteur empty = { // Cette variable me permettra de transformer la valeur qui m'intérésse pas
0
};
int i = 0;
int j = 0;
while ((plecteurs[i]->nom != lect->nom) &&
(plecteurs[i]->prenom != lect->prenom)) {
i++;
}
while (j < plecteurs[i]->nb_livres) {
plecteurs[i]->livres[j]->statut = 0;
j++;
}
while (i < * nombre_lecteurs) {
*plecteurs[i] = *plecteurs[i + 1];
i++;
}
*plecteurs[i] = empty;
}
int main() {
struct Livre l1 = { "boom" , 50 , 1 };
struct Livre l2 = { "bim" , 50 , 1 };
struct Livre l3 = { "chaud" , 50 , 0 };
struct Livre l4 = { "tcho" , 50 , 1 };
struct Livre l5 = { "braa" , 50 , 1 };
struct Livre *p1 = & l1;
struct Livre *p2 = & l2;
struct Livre *p3 = & l3;
struct Livre *p4 = & l4;
struct Livre *p5 = & l5;
struct Lecteur le1 = { "Boso" , "Nen" , 2 , {&l1, &l2} };;
struct Lecteur le2 = { "Jogar" , "Elo" , 1 , {&l3} };;
struct Lecteur le3 = { "marche" , "silteplait" , 2 , {&l4, &l5} };;
struct Lecteur *tableau_test[3] = {&le1, &le2, &le3};
int le_nombre = 3;
desabonnement(tableau_test, &le_nombre, &le3);
printf(" %d ", tableau_test[0]->nb_livres);
return 0;
}
Upvotes: 0
Views: 87
Reputation: 232
Actually what is your question?
Just some remarks:
First I'd consider (plecteurs[i]->nom != lect->nom)
a shorthand for
strcmp(plecteurs[i]->nom, lect->nom) != 0
. Same applies to prenom
.
You should correct this.
The snippet
while (j < plecteurs[i]->nb_livres) {
plecteurs[i]->livres[j]->statut = 0;
j++;
}
makes the books available without returning them. To me this seems unlogical, because the reader may not have given back the book. But this may be conforming with the (homework?) assignment you are working at. Note that the array of books may have holes your code ignores. Are all books packed to the beginning of the array when a book is returned? In other word, is
struct Lecteur le4 = { "murche" , "silmeplait" , 2 , {&l4, 0, &l5} };
invalid?
I didn't test your function but a first superficial look seems that it should
compile and with the strcmp()
- correction it sould run as you expect.
(Your null-lector is invalid, learn the use of NULL
pointers).
Besides: Why do you use a double semicolon ;;
?
Upvotes: 0
Reputation: 4104
The problem is in line while (i < * nombre_lecteurs)
of function void desabonnement(struct Lecteur * plecteurs[], int * nombre_lecteurs, struct Lecteur * lect)
. This should be while (i+1 < * nombre_lecteurs)
.
See complete working code here.
Note: In your actual code, You should decrease the le_nombre
when deletion is complete to reflect the new size (I have done this in the corrected code here).
Upvotes: 2