Hi I dont know why when I run in xcode it give me exec bad access but when I run using terminal it work fine
Here is mt code
//
// main.c
// Harbour Unload System
//
// Created by Guide on 15/10/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
struct harbour
{
int harbourId;
int maxCap;
int timeUpload;
double amount;
};
struct ship
{
int shipId;
int sMaxCap;
int sActCap;
int aTime;
};
int sort(const void *x, const void *y)
{
const struct ship
*ship1 = (struct ship *)x,
*ship2 = (struct ship *)y;
if ( ship1->aTime > ship2->aTime ) return 1;
if ( ship1->aTime < ship2->aTime ) return -1;
return 0;
}
void readHarbour( struct harbour *dock)
{
int i=0, lineCount=0;
FILE *harbourf;
harbourf = fopen("data.txt","r");
if (harbourf == NULL)
{
printf("Error: This file could not be opened or File not be found.\n");
}
do
{
fscanf(harbourf, "%d %d %d %lf", &dock[i].harbourId, &dock[i].maxCap, &dock[i].timeUpload, &dock[i].amount);
lineCount++;
i++;
} while (feof(harbourf) == 0);
}
void readShip( struct ship *ship)
{
int i=0, lineCount=0;
FILE *shipfile;
shipfile = fopen("data2.txt","r");
if (shipfile == NULL)
{
printf("Error: This file could not be opened or File not be found.\n");
}
do
{
fscanf(shipfile, "%d %d %d %d", &ship[i].shipId, &ship[i].sMaxCap, &ship[i].sActCap, &ship[i].aTime); // exec bad access
lineCount++;
i++;
} while (feof(shipfile) == 0);
}
void processing(struct harbour h[100], struct ship s[100])
{
int i, lineCount =24;
int count0=1 ,count1=1, count2=1, count3=1, count4=1, count5=1, count6=1, count7=1,count8=1, count9=1;
double totalAmount;
// Sort array Here
qsort(s, lineCount, sizeof(struct ship), sort);
for(i=0;i<lineCount;i++)
{
if(s[i].sMaxCap <= h[0].maxCap)
{
if(count0 <= 2)
{
totalAmount = s[i].sActCap * h[0].amount;
//}
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[0].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count0++;
}
}
else if(s[i].sMaxCap <= h[1].maxCap)
{
if(count1 <= 2)
{
totalAmount = s[i].sActCap * h[1].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[1].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count1++;
}
}
else if(s[i].sMaxCap <= h[2].maxCap)
{
if(count2 <= 2)
{
totalAmount = s[i].sActCap * h[2].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[2].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count2++;
}
}
else if(s[i].sMaxCap <= h[3].maxCap)
{
if(count3 <= 2)
{
totalAmount = s[i].sActCap * h[3].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[3].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count3++;
}
}
else if(s[i].sMaxCap <= h[4].maxCap)
{
if(count4 <= 2)
{
totalAmount = s[i].sActCap * h[4].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[4].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count4++;
}
}
else if(s[i].sMaxCap <= h[5].maxCap)
{
if(count5 <= 2)
{
totalAmount = s[i].sActCap * h[5].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[6].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count5++;
}
}
else if(s[i].sMaxCap <= h[6].maxCap)
{
if(count6 <= 2)
{
totalAmount = s[i].sActCap * h[6].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[6].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count6++;
}
}
else if(s[i].sMaxCap <= h[7].maxCap)
{
if(count7 <= 2)
{
totalAmount = s[i].sActCap * h[7].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[7].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count7++;
}
}
else if(s[i].sMaxCap <= h[8].maxCap)
{
if(count8 <= 2)
{
totalAmount = s[i].sActCap * h[8].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[8].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count8++;
}
}
else if(s[i].sMaxCap <= h[9].maxCap)
{
if(count9 <= 2)
{
totalAmount = s[i].sActCap * h[9].amount;
printf("\n");
printf("Processing complete\n");
printf("####################\n");
printf("Harbour id# %d\n", h[9].harbourId);
printf("Arrive Time: %d\n", s[i].aTime);
printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
printf(" total payment $ %.2f\n", totalAmount);
printf("\n");
count9++;
}
}
}
}
int main()
{
struct harbour h[100];
struct ship s[100];
readShip(s);
readHarbour(h);
processing(h, s);
return 0;
}