i fixed that piece of codeisnt this comparing the num within the strucutre?if not please explain .howevre it is compiling but program still crashes.Code:while(k>=0 && temp.partnum < list[k].partnum)
Printable View
i fixed that piece of codeisnt this comparing the num within the strucutre?if not please explain .howevre it is compiling but program still crashes.Code:while(k>=0 && temp.partnum < list[k].partnum)
Any warnings?
--
Mats
this suppsed to read fromt the partnum within the structureCode:while(k>=0 && temp.partnum < list[k].partnum)
it reads but the program still crashes.how should i corerct it?
the same warning about the interger pointer tingy
[Warning] passing arg 1 of `sort' makes pointer from integer without a cast
And what do you pass in for list, how long is the list, and what value is k? If k is outside the valid range it will crash. If list is not a valid address of the first element in your list, then it would also be likely to crash.
--
Mats
this my final code please let me know if i can be more efficient or if there are errors anywheere
thanks for the help peoplesCode:#include <stdio.h>
typedef struct
{
int partnum;
char desc[50];
int qtyInStock;
double unitPrice;
} part_info;
#define MAX 50
part_info part[MAX];
int main()
{
void readDesc(FILE * in, char data[]);
void AddInOrder(int i);
FILE * in = fopen("parts.dat","r");
if (in == NULL)
{
printf("File not found: parts.dat\n");
system("pause");
return 1;
}
int id, i=0, finished=0;
double TotalSales=0;
char trans[3];
fscanf(in,"%d",&id);
while (id != 0 && i<MAX)
{
part[i].partnum = id;
readDesc(in, part[i].desc);
fscanf(in,"%d",&part[i].qtyInStock);
fscanf(in,"%lf",&part[i].unitPrice);
if (i>0) AddInOrder(i);
i++;
fscanf(in,"%d",&id);
}
while (finished==0)
{
char ch=getc(in);
while (ch== ' ')
ch=getc(in);
trans[0]=getc(in);
if (trans[0] != EOF)
{
trans[1]=getc(in);
trans[2]='\0';
if (strcmp(trans,"ls")==0)
{
printf("\nPart No.\tDescription\t\tQuantity in Stock\tPrice\n\n");
int j;
for (j=0; j<i; j++)
printf("%d\t\t%-30s\t%d\t\t%5.2lf\n",part[j].partnum, part[j].desc, part[j].qtyInStock, part[j].unitPrice);
}
else
if (strcmp(trans,"sp")==0)
{
int j, pnum, qsold;
fscanf(in,"%d",&pnum);
fscanf(in,"%d",&qsold);
for (j=0; j<i; j++)
{
if (part[j].partnum > pnum)
{
printf("\nPart Number not found: %d....\n",pnum);
break;
}
else
if (part[j].partnum == pnum)
{
if (part[j].qtyInStock < qsold)
{
printf("\nNot enough quantity in stock...\n");
break;
}
else
{
part[j].qtyInStock-=qsold;
TotalSales+= (part[j].unitPrice * qsold);
break;
}
}
}
}
else
if (strcmp(trans,"ap")==0)
{
fscanf(in,"%d",&part[i].partnum);
readDesc(in, part[i].desc);
fscanf(in,"%d",&part[i].qtyInStock);
fscanf(in,"%lf",&part[i].unitPrice);
if (i>0) AddInOrder(i);
i++;
}
else
if (strcmp(trans,"cp")==0)
{
int j, pnum, nprc;
fscanf(in,"%d",&pnum);
fscanf(in,"%d",&nprc);
for (j=0; j<i; j++)
{
if (part[j].partnum > pnum)
{
printf("\nPart Number not found: %d....\n",pnum);
break;
}
else
if (part[j].partnum == pnum)
{
part[j].unitPrice=nprc;
break;
}
}
}
else
if (strcmp(trans,"cq")==0)
{
int j, pnum, qty;
fscanf(in,"%d",&pnum);
fscanf(in,"%d",&qty);
for (j=0; j<i; j++)
{
if (part[j].partnum > pnum)
{
printf("\nPart Number not found: %d....\n",pnum);
break;
}
else
if (part[j].partnum == pnum)
{
part[j].qtyInStock+=qty;
break;
}
}
}
}
else
finished=1;
}
printf("\nTotal Sales: %5.2lf\n",TotalSales);
system("pause");
return 0;
}
void AddInOrder(int i)
{
int j=i;
part_info temp;
while (j>=0 && (part[i].partnum < part[j-1].partnum))
{
temp=part[j];
part[j] = part[j-1];
part[j-1] = temp;
j--;
}
}
void readDesc(FILE * in, char data[])
{
char c;
int i=0;
c=getc(in);
while (c != '*') c=getc(in);
c=getc(in);
data[i]=c;
c=getc(in);
while (c != '*')
{
i++;
data[i]=c;
c=getc(in);
}
i++;
data[i]='\0';
}