Hi, I'm new here :)
the problem is if i increment one member, for eg

---------------------------------------

struct day
    {
    int hour[40];
    int min[40];
    int mins[40];
    int rec;
    int totmins;
    int totsal;
    char    status;
    int temp;
    };

struct month
{   struct day *da;};

struct year
{   struct month *mn;};

struct roll
{
    int rn;
    char name[30];
    int salary;
    struct year *yr;
};



int    t1=100,t2=25,t3=12,t4=31,    y1,m1,d1,  y2,m2,d2,  y,m,d,  y3,y4, i,j,noofemp=0;
char fname[20],t5[30],t6,t7;
clrscr();

struct roll *rol;
struct year *yr;
struct month *mn;
struct day *da;
  rol=(struct roll*)malloc(t1*sizeof(struct roll));
     if( rol== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  yr=(struct year*)malloc(t2*sizeof(struct year));
     if( yr== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  mn=(struct month*)malloc(t3*sizeof(struct month));
     if( mn== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  da=(struct day*)malloc(t4*sizeof(struct day));
     if( da== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}

now here consider

rollnum=1;y=15;m=1;d=17;noofemp=100

for(i=1;i<=noofemp;i++)
    {
    rol[i].rn=i;
    rol[i].yr[y].mn[m].da[d].rec=1;
    }
    getch();

roll[1].yr[y].mn[m].da[d].rec++;

------------------------------------------------
it is incrementing all other members too...
I mean it is doing

roll[2].yr[y].mn[m].da[d].rec++;
roll[3].yr[y].mn[m].da[d].rec++;
roll[4].yr[y].mn[m].da[d].rec++;
roll[5].yr[y].mn[m].da[d].rec++;
.
.
.
roll[100].yr[y].mn[m].da[d].rec++;

whats the error?

-----------------------------------------------------
the actual aim if the program is to read a text file of rollno,name and salaryperday in to the 1st structure[i mean the array? err...]

sample file of emp.txt

1   emp1    230 
2   emp2    335 
3   emp3    250
4   emp4    250 
5   emp6    250 

then read the log files[we have a biometric device which takes fingerprint from the employes and gives me a log file with name as date, like 25-01-15
where the 1st is roll, 2nd is hour of 18:57 and 3rd is minute of 18:**57 **, 4th is the conversion of 18:46 into minutes

sample file of DD-MM-YY.dat

01 07 01 00430 
11 07 01 00430 
02 07 01 00430 
15 07 01 00430 
08 07 01 00431 
05 07 01 00436 
23 07 01 00453 
03 07 01 00473 
08 13 01 00790 
23 13 01 00790 
02 13 01 00816 
23 14 01 00842 
08 14 01 00851 
02 14 01 00866 
15 14 01 00872 
11 14 01 00880 
05 15 01 00907 
15 15 01 00914 
03 15 01 00920 
05 15 01 00934 
11 15 01 00942 
03 19 01 01197 
08 19 01 01197 
23 19 01 01197 
02 19 01 01198 
11 19 01 01198 
15 19 01 01198 
05 20 01 01200 
01 20 01 01200 

complete C code:
please jump to -------this part sets the rec value to 0 in the above D,M,Y for all the employees initially------- and ---this is not working as expected---
use ctrl+f to find it...

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <time.h>
#include<string.h>

void dummy(float *a)
{
    float b=*a; //perform some floating access
    dummy (&b); //calling a floating point function
}


/*--------- data+details file structure --------*/

struct day
    {
    int hour[40];
    int min[40];
    int mins[40];
    int rec;
    int totmins;
    int totsal;
    char    status;
    int temp;
    };
struct month
{   struct day *da;};

struct year
{   struct month *mn;};

struct roll
{
    int rn;
    char name[30];
    int salary;
    struct year *yr;
};



main()
{

  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );




int    t1=100,t2=25,t3=12,t4=31,    y1,m1,d1,  y2,m2,d2,  y,m,d,  y3,y4, i,j,noofemp=0;
char fname[20],t5[30],t6,t7;
clrscr();

struct roll *rol;
struct year *yr;
struct month *mn;
struct day *da;
  rol=(struct roll*)malloc(t1*sizeof(struct roll));
     if( rol== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  yr=(struct year*)malloc(t2*sizeof(struct year));
     if( yr== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  mn=(struct month*)malloc(t3*sizeof(struct month));
     if( mn== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  da=(struct day*)malloc(t4*sizeof(struct day));
     if( da== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}


printf("Edit \"empdetails.txt\" for adding new or changing roll no, name, salary per day.");

printf("Enter begining date: MM DD YY:");
if ((scanf("%d%d%d",&d1,&m1,&y1)!=3))
{printf("Invalid input");}

printf("\nEnter ending date: MM DD YY:");
if ((scanf("%d%d%d",&d2,&m2,&y2)!=3))
{printf("Invalid input"); }

if(y1-y2>1)
printf("\nCant input greater than 1 year");

d=d1;m=m1;y=y1;
//if(y1==y2)
//{y3=0;y4=0;}
//else
//{y3=0;y4=1;}



    FILE *emp_file;
    FILE *calcsal_file;
    emp_file = fopen("emp.txt","r");
    calcsal_file = fopen("calcsalary.txt","w");

if(emp_file==NULL)
{
    printf("\nERROR Accessing source file for computing salary.\n\nPlease use a text document with the following info:\n Roll   Name    Salary/Day\n\n\n\nFilename of the text document MUST BE \"empdetails\"\ (without the quotes\) ");
    getch();
    exit(0);
}

/*--------- get data from file --------*/
printf("\nReading Employee Details.");
i=0             ;
while(fscanf(emp_file,"%d%s%d",&t1,&t5,&t3)!=EOF)
{
i++;
rol[t1].rn=i;
strcpy(rol[t1].name,t5);
rol[t1].salary=t3;

if(noofemp<t1)
noofemp=t1;

}
printf("\nNumber of employees: %d",noofemp);
printf("\nEMPLOYEES:");
for(i=1;i<=noofemp;i++)
printf("\nRoll:%02d\tName:%25s\tSalary%03d",rol[i].rn,rol[i].name,rol[i].salary);
getch();
fclose(emp_file);

/*--------- get details from file --------*/


for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{
    sprintf(fname,"%02d-%02d-%02d.dat",d,m,y);
    emp_file = fopen(fname,"r");



    if(emp_file==NULL)
    {   printf("\nERROR Accessing %s for computing salary",fname);
        getch();
        exit(0);
    }
        printf("\nReading %s\t",fname);
        getch();

** for(i=1;i<=noofemp;i++) this part sets the rec value to 0 in the above D,M,Y for all the employees initially
{
rol[i].rn=i;
rol[i].yr[y].mn[m].da[d].rec=0;
}
**

while(fscanf(emp_file,"%d %d %d %d \n",&t1,&t2,&t3,&t4)!=EOF)

{

** rol[t1].yr[y].mn[m].da[d].rec++; this is not working as expected
j=rol[t1].yr[y].mn[m].da[d].rec;**

            printf("\nreading emp:%d j:%d rec:%d",rol[t1].rn,j,rol[t1].yr[y].mn[m].da[d].rec) ;
            rol[t1].yr[y].mn[m].da[d].hour[j]=t2;
            rol[t1].yr[y].mn[m].da[d].min[j]=t3;
            rol[t1].yr[y].mn[m].da[d].mins[j]=t4;

            printf("\nRead Roll:%02d Time:%02d:%02d  Mins:%05d  Name:%s Salary:%03d Record:%02d",rol[t1].rn,rol[t1].yr[y].mn[m].da[d].hour[j],rol[t1].yr[y].mn[m].da[d].min[j],rol[t1].yr[y].mn[m].da[d].mins[j],rol[t1].name,rol[t1].salary,rol[t1].yr[y].mn[m].da[d].rec);


        }

        fclose(emp_file);
        getch();
        printf(".");




}
}
}
fprintf(calcsal_file,"\n                                company name");
fprintf(calcsal_file,"\n                                   Salary Calculator v3.0                   Run on: ");
fprintf(calcsal_file,"\n                         Report From %d-%d-%d to %d-%d-%d",d1,m1,y1,d2,m2,y2);
for(i=1;i<=noofemp;i++)
        {
fprintf(calcsal_file,"\n\n Roll: %d  Name:%s        Salary/Day:%d",i,rol[i].name,rol[i].salary);
fprintf(calcsal_file,"\n -----------------------------------------------------------------------------------------------------------------------------------");
fprintf(calcsal_file,"\n Date   In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out | Total Hours | Status | Salary / Day | Salary /Hour   ");
for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{

fprintf(calcsal_file,"\n %d-%d-%d",d,m,y);
for(j=1;j<=rol[i].yr[y].mn[m].da[d].rec;j++)
{fprintf(calcsal_file,"\n %02d:%02d  ",rol[i].yr[y].mn[m].da[d].hour[j],rol[i].yr[y].mn[m].da[d].min[j]);}

}
}
}

}

free(rol);
free(yr);
free(mn);
free(da);

getch();
}

Edited 1 Year Ago by kARTechnology

You expect us to analyze and comment on 300 lines of code and output? Someone perhaps will, but it won't be me. I see a lot of issues with your code, most of which is just silly stuff. You look at it again, with a critical eye and the lessons you should have learned in class, and try again... Also, indenting your code properly will make it a lot easier to read.

now here consider

rollnum=1;y=15;m=1;d=17;noofemp=100
for(i=1;i<=noofemp;i++)
{
rol[i].rn=i;
rol[i].yr[y].mn[m].da[d].rec=1;
}
getch();
roll[1].yr[y].mn[m].da[d].rec++;

------------------------------------------------
it is incrementing all other members too...
I mean it is doing

roll[2].yr[y].mn[m].da[d].rec++;
roll[3].yr[y].mn[m].da[d].rec++;
roll[4].yr[y].mn[m].da[d].rec++;
roll[5].yr[y].mn[m].da[d].rec++;
.
.
.
roll[100].yr[y].mn[m].da[d].rec++;

whats the error?

Have you looked to actually see what you have done with memory allocations. It seems that you have the following :

rol=(struct roll*)malloc(t1*sizeof(struct roll));
yr=(struct year*)malloc(t2*sizeof(struct year));
mn=(struct month*)malloc(t3*sizeof(struct month));
da=(struct day*)malloc(t4*sizeof(struct day));

But then given that month has the structure :

struct month 
  { 
     day* da;
  };

However, at not point do you make da point to anything. That the program doesn't seg.fault is chance I think.

You have two problems, (i) you allocate say 12 months of memory,
BUT only allocate 31 days of memory -- that does for January but
nothing for February etc. (ii) You don't make da point to anything,
it is just a floating pointer.

What you need is to put some code after your massive memory allocations, which sets the pointers to point to something e.g.

 int mIndex;

 mn=(struct month*) malloc(12 * sizeof(struct month));
 for(mIndex=0;mIndex<12;mIndex++)
   {
     mn[mIndex]=(struct day*) malloc(31 * sizeof(struct day));
   }

Finally, I don't know what this code does but this cannot be a good way to do it. I think a re-think is required. Start small, and test litte bits and get them working, e.g just try a single day first, then try a month etc.

This article has been dead for over six months. Start a new discussion instead.