0

I created a class for display for each fabrication name the employee and piece. I use structure also pointer, but always the first line gives me name of the employee is null and piece is empty.

My result is like this

FICHE DE GESTION DES IDEMNITES DES EMPLOYES DE MP (par journee)
-----------------------------------------------------------------
 Nom Atelier de Fabrication 1: 1     Nom Responsable Atelier 1 : 1
                                Nom NPF IndiceP     IndP
(null)  1   .       40.000000               
empname 1   A       20.000000
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "h3.h"

MP MOTO[20];
MP *p1;
EMPLOYE *p2; 
float TIndFabrication=0;
int NAF;
char ***NomE;
char **NomAF;
char **NomRA;
char **IndiceP;

char *saisir()
{
    char *TABL,INTRO[20];
 
    scanf("%s",&INTRO);
    TABL=malloc(strlen(INTRO)+1);
    if(TABL)
        strcpy(TABL,INTRO);
    else {
        printf("pas d'espace mémoire!!");
        exit(-1);
    }
}

float traitement_1(char IndiceP,int NPF)
{
    float  Indp;
    int taux;
    if(IndiceP=='A')
        taux=20;
    else if(IndiceP=='B')
        taux=10;
    else if(IndiceP=='C')
        taux=5;
    else
        printf("Erreur!!");
    Indp=taux*NPF;
    return Indp;
 }

float traitement_2(MP MOTO[20])
{ 
    for(p1=MOTO;p1<MOTO+NAF;p1++)
    {
        for(p2=p1->EMP;p2<p1->EMP+p1->NEF;p2++)
        {
              p1->TIndp+= p2->Indp;
        }   
    }
    return  p1->TIndp; ;
}

float traitement_3(MP MOTO[20])
{   
    for(p1=MOTO;p1<MOTO+NAF;p1++)
    {       
       TIndFabrication+=p1->TIndp;
    }
    return TIndFabrication;
}

void afficher( MP MOTO[20],char ***NomE,char **IndiceP,char **NomAF,char **NomRA)
{
    FILE* fichier=NULL; 
    fichier=fopen("f3.txt","a"); 
    if(fichier==NULL) 
    { 
        fputs("\n ce fichier n existe pas\n",fichier); 
    } 
    else 
    { 
        fputs("\nFICHE DE GESTION DES IDEMNITES DES EMPLOYES DE MP (par journee)\n",fichier); 
        for(p1=MOTO;p1<MOTO+NAF;p1++) 
        { 
            fputs("-----------------------------------------------------------------\n",fichier); 
            fprintf(fichier," Nom Atelier de Fabrication %d: %s\t Nom Responsable Atelier %d : %s\n",p1-MOTO+1,NomAF[p1-MOTO],p1-MOTO+1,NomRA[p1-MOTO]); 
            fputs("                                Nom\tNPF\tIndiceP\t\tIndP\n",fichier); 

When is use plus the one employee code display the first line (null) 1 . 40.000000 and second is correct

            for (p2=p1->EMP; p2<p1->EMP+p1->NEF; p2++) 
            { 
                fprintf(fichier,"\t\t\t\t%s\t%d\t%c\t\t%f\n",  NomE[p1-MOTO][p2-(p1->EMP)],p2->NPF,IndiceP[p1-MOTO][p2-(p1->EMP)],p2->Indp); 
            } 
            fputs("\n-----------------------------------------------------------------\n",fichier);  
            fprintf(fichier,"                                Total Indemnites %fDH",p1->TIndp);    
            fputs("\n-----------------------------------------------------------------\n",fichier); 
        } 
        fprintf(fichier,"\nTotaux des indemnites des employes du groupe MP pendant la journee : %.2fDH",TIndFabrication); 
     
    } 
    fclose(fichier);
} 

void libereE( MP MOTO[20],char ***NomE,char **NomAF,char **NomRA)
{
    for(p1=MOTO;p1<MOTO+NAF;p1++)
    {
        free(NomAF[p1-MOTO]);
        free(NomRA[p1-MOTO]);
        for(p2=p1->EMP;p2<p1->EMP+p1->NEF;p2++)
        {
            free(NomE[p1-MOTO][p2-p1->EMP]);
        }
    }
}

void main()
{
    char INTRO[20];
    int taux;
    MP MOTO[20];
  
    printf("Entrer le nombre d'atelier de fabrication:");
    scanf("%d",&NAF);
    NomAF=malloc(NAF*sizeof(*NomAF));
    NomRA=malloc(NAF*sizeof(*NomRA));
    NomE=malloc(NAF*sizeof(*NomE));
    IndiceP=malloc(NAF*sizeof(*IndiceP));

    for(p1=MOTO;p1<MOTO+NAF;p1++)
    {
        p1->TIndp=0.0;
        printf("entrer le nom de l'atelier %d:",p1-MOTO+1);
        NomAF[p1-MOTO]=saisir();
        printf("entrer le nom de responsable de l'atelier %d :",p1-MOTO+1); 
        NomRA[p1-MOTO]=saisir();
        printf("entrer le nombre des employers d'atelier %d :\n",p1-MOTO+1);
        scanf("%d",&p1->NEF);
        for(p2=p1->EMP;p2<p1->EMP+p1->NEF;p2++)
        {
            NomE[p1-MOTO]=malloc( (p1->NEF)*sizeof(*NomE) );
            IndiceP[p1-MOTO]=malloc( (p1->NEF)*sizeof(*IndiceP) );
            printf("entrer le nom d'emloyer %d au sein de l'atlelier %d: ",p2-p1->EMP+1,p1-MOTO+1);
            NomE[p1-MOTO][p2-p1->EMP]=saisir();
            printf("entrer le nombre des pieces fabrique par l'employer %d au sein de l'atlelier %d:\n ",p2-p1->EMP+1,p1-MOTO+1);
            scanf("%d",&p2->NPF);;
            printf("entrer l'indice des pieces (A,B ou C):");
            scanf("%s",&IndiceP[p1-MOTO][p2-p1->EMP] );
            p2->Indp=traitement_1( IndiceP[p1-MOTO][p2-p1->EMP] ,p2->NPF);  
        }
    }
    p1->TIndp=traitement_2(MOTO);
    TIndFabrication=traitement_3 (MOTO);
    afficher( MOTO,NomE,IndiceP,NomAF,NomRA);
     
    libereE(MOTO,NomE,NomAF,NomRA);
}

Class the structur

main.ch3.h

#ifndef def_H3_H_
#define H3_H

typedef struct EMPLOYE EMPLOYE;
struct EMPLOYE{
    int NPF;
    float Indp;
};

typedef struct MP MP;
struct MP {
    float TIndp;
    int NEF;
    struct EMPLOYE EMP[20];
};

char* saisir();
float traitement_1 (char IndiceP,int NPF);
float traitement_2( MP MOTO[20]);
float traitement_3( MP MOTO[20]);
void afficher( MP MOTO[20],char ***NomE,char **IndiceP,char **NomAF,char **NomRA);
void libereE( MP MOTO[20],char ***NomE,char **NomAF,char **NomRA);

#endif
Gerhardh
  • 9,405
  • 2
  • 12
  • 34
Hajar
  • 9
  • 2
  • Welcome to SO. Without knowing how you get the data into your arrays it is hard to say much. – Gerhardh Aug 18 '20 at 21:25
  • 1
    Not related to your problem, but you should not call `fputs("\n ce fichier n existe pas\n",fichier);` and `fclose(fichier);` if `fichier==NULL` – Gerhardh Aug 18 '20 at 21:26
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables? If not, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Aug 18 '20 at 21:36
  • 1
    When seeking debugging help, please provide a [mre]. This example should include the exact input, expected output and actual output. Also, the example would be easier for other people to read and understand if the program's output were in English. – Andreas Wenzel Aug 18 '20 at 21:42
  • 1
    `NomAF[p1-MOTO]` - that is subtracting 2 pointers, but using it as an index. I don't think this will work like you think it does. – Michael Dorgan Aug 18 '20 at 22:39
  • regarding: `I create a class for display` this is C, not C++, so there are no `class`es. However, there are `struct`s. – user3629249 Aug 18 '20 at 23:32
  • 2
    regarding: `#ifndef def_H3_H_ #define H3_H` is this expected to be a 'include guard'? if so, then both names have to be exactly the same – user3629249 Aug 18 '20 at 23:35
  • OT: regarding; `main.ch3.h #ifndef def_H3_H_ #define H3_H` it is typical for a header file name and the include guard to be the same spelling. – user3629249 Aug 18 '20 at 23:40
  • 2
    OT: for ease of readability and understanding (the compiler doesn't care) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Aug 18 '20 at 23:43
  • OT: regarding: `void libereE( MP MOTO[20],char ***NomE,char **NomAF,char **NomRA)` Please read [three star programmer](https://wiki.c2.com/?ThreeStarProgrammer) – user3629249 Aug 18 '20 at 23:50
  • OT: regarding: `MP *p1;` and `for(p1=MOTO;p1 – user3629249 Aug 18 '20 at 23:56
  • OT: always check the returned value from C library functions to determine if the function was successful or not. regarding; `scanf("%d",&NAF);` The `scanf()` family of functions returns the number of successful `input format conversion specifiers` In the current scenario, there is just 1. Suggest: `if( scanf("%d",&NAF) != 1 ) { fprintf( stderr, "scanf for NAF failed\n" ); }` – user3629249 Aug 19 '20 at 00:00
  • `char *saisir()` empty parameter list is a deprecated feature. Use `(void)` if a function does not take parameters. Also you allocate memory in that function but you do not return anything. This causes a memory leak. You should clearly enable warnings in your compiler and take care about them! `void main()` This is not a valid prototype for `main`. it must return `int` – Gerhardh Aug 19 '20 at 08:41
  • Not a problem here, but this is wrong: `NomE[p1-MOTO]=malloc( (p1->NEF)*sizeof(*NomE) );` You need `sizeof(**NomE)` – Gerhardh Aug 19 '20 at 09:18
  • @MichaelDorgan it sounds horrible, but it will work as expected. – Gerhardh Aug 19 '20 at 09:23
  • @AndreasWenzel oh, I always thought the "Lets move to chat" notification is only shown if all participants are allowed to go there... – Gerhardh Aug 19 '20 at 11:08
  • I would recommend rewriting your function prototypes: `void libereE( MP MOTO[20],char ***NomE,char **NomAF,char **NomRA)` => `void libereE(int NAF, MP MOTO[20], char **NomE[NAF], char *NomAF[NAF], char *NomRA[NAF])` – Gerhardh Aug 19 '20 at 11:10
  • Then change to loop: `for(p1=MOTO;p1 `for (int i1 = 0; i1 < NAF; i1++)` and `free(NomAF[p1-MOTO]);` => `free(NomAF[i1]);` This will increase readability a lot! – Gerhardh Aug 19 '20 at 11:10
  • You can also change the loops to a readable version without adjusting the function signature – Gerhardh Aug 19 '20 at 11:10
  • I test all suggestions is not working – Hajar Aug 19 '20 at 11:45
  • @Hajar: After fixing all issues mentioned above, you may want to post your new code, so others can see it. If you only say "is not working", then nobody can help you. Also, I strongly suggest that you read my links that I posted above about how to debug your own program and how to create a [mre]. – Andreas Wenzel Aug 19 '20 at 13:33

0 Answers0