#include "stdafx.h"
#include <stdio.h>
#include<stdlib.h>
#include<iostream>
typedef struct Sale
{
    int clientNumber[4];
    char Item[45];
    char PartNum[12];
    int UniCost[11];
    char DayOfMonth[20];

};
Sale rSale[197];
typedef struct Data
{
    int clientNumber[4];
    char Company[34];
    char ABN[14];
    char StreetName[36];
    char Suburb[24];
    char State[11];
    char PostCode[5];
    char Phone[15];

};
 Data rData[10];

int _tmain(int argc, _TCHAR* argv[])
{
    FILE *fp;
    FILE *fps;
    fp=fopen("clinet.txt","r");
    fps=fopen("records.txt","r");
    if ((fp != (FILE *)NULL) && (fps != (FILE *)NULL)  )
    {
        int x,j,i,b;
        j=0;
        for (x=0; x<10; x++)
    {
    fread(rData[x].clientNumber,10,3,fp);
    fgets(rData[x].Company,sizeof(rData[0].Company),fp);
    fgets(rData[x].ABN,sizeof(rData[0].ABN),fp);
    fgets(rData[x].StreetName ,sizeof(rData[0].StreetName),fp);
    fgets(rData[x].Suburb,sizeof(rData[0].Suburb),fp);
    fgets(rData[x].State,sizeof(rData[0].State),fp);
    fgets(rData[x].PostCode,sizeof(rData[0].PostCode), fp);
    fgets(rData[x].Phone,sizeof(rData[0].Phone),fp);
    }
    for (i=0; i<197 ;i++)
        {
    fread(rSale[i].clientNumber,1,3,fps);
    fgets(rSale[i].ItemDesc,sizeof(rSale[i].ItemDesc),fps);
    fgets(rSale[i].PartNum,sizeof(rSale[i].PartNum),fps);
    fread(rSale[i].UniCost,3,7,fps);
    fgets(rSale[i].DayOfMonth,sizeof(rSale[i].DayOfMonth),fps);

    }
    for (b=0;b<10;b++)
    {
            printf("TOF ------------ \n\n\n");
            printf("ABN %s\n",rData[b].ABN);
            printf("%s\n",rData[b].Company);
            printf("%s\n",rData[b].StreetName);
            printf("%s      %s  %s\n",rData[b].Suburb,rData[b].State,rData[b].PostCode);
            printf("%s\n\n\n\n",rData[b].Phone);
        for (j=0;j<197;j++)
    {
        if(*(rData[b].clientNumber) == *(rSale[j].clientNumber))
        {
            printf("%s%s$%s%s",rSale[j].ItemDesc,rSale[j].PartNum,rSale[j].UniCost,rSale[j].DayOfMonth);
printf("%i",totalunitcost)[inlinecode]can u guys help do the totalunitcost for each specific customer, i tried everything could get it done thank u[/inlinecode]


}}}

    }
        }
    int k;
    scanf("%i", k);
return 0;
}       

>>can u guys help do the totalunitcost for each specific customer, i tried everything could get it done

If totalunitcost is the simple sum of rSale.UniCost, then just sum it up while reading the data file.


fread(rSale[i].UniCost,3,7,fps);
totalunitcost += atof(rSale[i].UniCost); // assume this is a float

yeah but i think its gonna add all the unicost in the file wel the problem i want to add the specific unitcost for each customer for example
here i'm printing all records for each of the 10 customers

for (b=0;b<10;b++)
    {
            printf("TOF ------------ \n\n\n");
            printf("ABN %s\n",rData[b].ABN);
            printf("%s\n",rData[b].CompanyName);
            printf("%s\n",rData[b].StreetName);
            printf("%s      %s  %s\n",rData[b].Suburb,rData[b].State,rData[b].PostCode);
            printf("%s\n\n\n\n",rData[b].Phone);
        for (j=0;j<197;j++)
    {
        if(*(rData[b].CustNum) == *(rSale[j].CustNum))
        {
            printf("%s%s$%s%s",rSale[j].ItemDesc,rSale[j].PartNum,rSale[j].UniCost,rSale[j].DayOfMonth);
the totalunitcost goes here so it prints only the total unitcost for each customer , printf("%i",totalunitcost);

        }}}
thanks mate

    }*/

I would add a float totalunitcost field to struct data that is the total cost for the customer. When when a line is read from the data file look up the customer record in struct data and update its total unit cost.


>>int clientNumber[4]
why is this an array of 4 integers?? all you need is one int. Same with unit cost

typedef struct Sale
{
	int clientNumber;
	char Item[45];
	char PartNum[12];
	int UniCost;
	char DayOfMonth[20];
};


typedef struct Data
{
    int clientNumber;
	char Company[34];
	char ABN[14];
	char StreetName[36];
	char Suburb[24];
	char State[11];
	char PostCode[5];
	char Phone[15];
float totalunitcost;

};

The fread line also changes somewhat. Is the client number in binary format in the file (is it readable with Notepad.exe?). If not, then this should work, if it is readable with notepad.exe then you need to use fgets() and atol()

fread(&rData[x].clientNumber,1,sizeof(int),fp);

fread(&rSale[i].UniCost,1,sizeof(int),fps);

If the integers are visible when the file is view by Notepad.exe, then you will need to use this algorithm

char buf[40];
fgets(buf,sizeof(buf),fp);
rData[x].clientNumber = atol(buf);

fgets(buf,sizeof(buf),fp);
rSale[i].UniCost = atof(buf);
// search array of struct data for customer number,
// then update total unit cost

rData[x].totalunitcost += rSale[i].UniCost;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.