Reputation: 11
typedef struct Projet
{
/* data */
char theme[20];
int diff;
char etudiant1[4]; //L'Etudiant 1 affecté dans le projet par Matricule
char etudiant2[4]; //L'Etudiant 2 affecté dans le projet par Matricule
}Projet;
/****---Function to add a project---****/
int AddProjet(int nmbProjet){
int rows;
Projet TabProj[nmbProjet]; // Table to store the projects
/* --Loop to get the project info-- */
for ( rows = 0; rows < nmbProjet; rows++)
{
printf("\t--> Theme %d : ",rows+1);
scanf(" %s",&TabProj[rows].theme);
/****Project Theme****/
printf("\t--> La difficulte [1-10] : ");
scanf(" %i",&TabProj[rows].diff);
/*****its difficulty*****/
printf("\n");
}
}
/*****************************************/
/****---Function to add 2 students in a project---****/
int AffEtudiant(int nmbPrjAff){
int choix_prj_aff;
Projet TabProj[nmbPrjAff];//Table of the projects
puts("--> Choisir un projet :");//Choose a project
/***--Loop to list all the project that the user has added--***/
for (int i = 0; i < nmbPrjAff; i++)
{
printf("\t%d- Projet %d :\n",i+1,i+1);
printf("\t -> %s",TabProj[i].theme);
printf("\n");
printf("\t -> %d",TabProj[i].diff);
printf("\n\n");
}
printf("--> Votre choix est : ");//Choice
scanf("%d",&choix_prj_aff);
printf("\n<----------------------->\n\n");
printf("--> Entrer le Matricule de 1 etudiant : ");//Enter the code of student 1
scanf("%s",&TabProj[choix_prj_aff-1].etudiant1);
printf("--> Entrer le Matricule de 2 etudiant : ");//Enter the code of student 2
scanf("%s",&TabProj[choix_prj_aff-1].etudiant2);
printf("\n<----------------------->\n\n");
}
/*****************************************************/
The problem is when I try to add two students after adding the projects the second function which is called (AffEtudiant
) can't get the projects data (theme/difficulty) to list them. Instead it shows some garbage values, even though I called the same table that I have set in the function of adding projects (Project TabProj[]
);
How can I get that table of data to use it in another function?
Upvotes: 1
Views: 52
Reputation: 311563
TabProj
is a local variable inside each of the functions and it gets freed once the function exists. You should define it outside the functions and pass a point to it to the function when you want the function to update it.
Upvotes: 1