Вот , собственно, сама задачка. А вот и сам код.Только не могу понять, в чем ошибка.Почему не верно сравнивается в цикле..Первый элемент с нужным названием ищется верно(элемент с которым я потом буду сравнивать все последующие с таким же названием.)Я был бы очень рад, если бы вы нашли мою ошибку..(Извиняюсь заранее за глупые ошибки , я не умышленно )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int j , i;
FILE *f;
char name [25];
printf ("enter the name of the file: ");
scanf ("%s", name);
if((f = fopen(name, "r")) == NULL)
{
printf ("file not found");
system ("PAUSE");
}
typedef struct{
char Name[30];
char Price[30];
char Shelf_life[30];
}stuff;
int n=0,k;
stuff a;
stuff *x=NULL;
while (!feof(f)){
if(fscanf (f,"%s", a.Name)!=1)
break;
fscanf (f,"%s",a.Price);
fscanf (f,"%s",a.Shelf_life);
x=(stuff*)realloc(x,sizeof(stuff)*(n+1));
x[n]=a;
n++;
}
fclose(f);
for (i=0;i<n;i++){
printf("%s %s %s\n", x[i].Name,x[i].Price,x[i].Shelf_life);}
char Name[30];
printf("enter the product name : \n");
scanf (" %s", Name);
int t,s,min=0;
for (i=0;i<n;i++){
if (strcmp(x[i].Name, Name) == 0){
t=i;
break;
}
}
for (s=t+1;s<n;s++ ){
if (strcmp(x[s].Name, Name) == 0){
if (strcmp(x[s].Price,x[t].Price) < 0){
min=s;
}
else {
min=t;}
}
}
printf("minimum price : %s \n", x[min].Price);
system("PAUSE");
free(x);
return 0;
}