I'm expecting this output:
ACC @ USAGE
101 @ 1302
102 @ 1062
105 @ 1004
159 @ 1031
160 @ 895
1071 @ 2316
1176 @ 1914
1286 @ 1872
1395 @ 1362
2999 @ 1702
Yet.... my program give me the following output...
ACC == USAGE
ACC:101 == 1302 KW
ACC:102 == 1062 KWh
ACC:105 == 1004 KWh
ACC:159 == 1031 KWh
ACC:160 == 895 KWh
ACC:1071 == 2316 KWh
ACC:1176 == 1914 KWh
ACC:1286 == 1872 KWh
ACC:1395 == 1362 KWh
ACC:3000 == 1367 KWh <--- weired!!
Please help me to find out the problem .... if possible correct it...
thanks!!
Code:
#include <stdio.h>
FILE *om, *nm, *tran;
int main(void)
{
int c_acc_no,kwh_used=0,c_acc_no_t,daily_kwh_used_t,day_t,writeok=0,tran_acc[29],tran_kwh[29],i,j,newacc[10],newkwh[10];
char split_c,c_type,name[25],add[30];
double total;
om=fopen("oldmaster.dat","r");
nm=fopen("newmaster.dat","w");
tran=fopen("trans.dat","r");//open trans file with read mode
for(i=0;i<=10;i++)
{
newkwh[i]=0;
newacc[i]=0;
}
//fscanf from transaction file for account number and kilo watts used then store them to array
for(i=1;i<30;i++)
{
fscanf(tran,"%d%c%d%c%d%c",&tran_acc[i],&split_c,&tran_kwh[i],&split_c,&day_t,&split_c);
//printf("%d : %d - %d\n",i,array_acc[i],array_kwh[i]);
}
for(j=1,i=2;i<=30;i++)//FOR loop until i equal 30
{
printf("%dL: %d - %d\n",i,tran_acc[i-1],tran_acc[i]);
//if account number from tran_acc[n-1] (pervious acc num) is same with current tran_acc[n]
//then add pervious kilo watt and current kilo watt to newkwh[i-1]
//else store pervious(n-1) to newacc (ready to
if(tran_acc[i-1]==tran_acc[i])
{
newkwh[j]+=(tran_kwh[i-1]);
}
else
{
printf("NOT EQUAL %d != %d\n\n",tran_acc[i-1],tran_acc[i]);
newkwh[j]+=(tran_kwh[i-1]);
newacc[j]=tran_acc[i-1];
j++;
}
}
for(i=1;i<=10;i++)
printf("ACC:%d == %d KWh\n",newacc[i],newkwh[i]);
fcloseall();//close all file
printf("Completed\n\n\n\t\t\tNew Master File Updated !!!\a\n");
}
================
here the trans.dat file
================
101#545#2#
101#512#5#
101#245#1#
102#130#4#
102#821#2#
102#111#3#
105#236#2#
105#256#1#
105#512#3#
159#160#3#
159#384#4#
159#487#5#
160#320#1#
160#225#7#
160#350#3#
1071#996#1#
1071#653#5#
1071#667#7#
1176#992#6#
1176#472#5#
1176#450#3#
1286#970#5#
1286#680#4#
1286#222#3#
1395#204#4#
1395#394#1#
1395#764#3#
2999#701#5#
2999#666#6#
2999#335#1#