This code is meant to find the most expensive beer from a list of beers in "pivo.txt". When it's looking for the cheapest beer, the if statement is executed and the program works fine but when i turn the comparison sign from > to < it never executes. Is this a logical problem or does it have something to do with comparing floats (casting them to int didn't work)
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char ime[50];
double kol;
double cijena;
}pivo;
char* cheap_price(pivo *p,int n)
{
int i=0,j=0;
double raz,maxraz;
maxraz = p[i].kol/p[i].cijena;
for(i=0;i<n;i++)
{
raz = p[i].cijena/p[i].kol;
printf("%lf\n",raz);
if(raz<maxraz)
{
maxraz = raz;
printf("min%lf\n",maxraz);
j=i;
}
}
return p[j].ime;
}
int main()
{
pivo *p;
int n,i;
FILE *file = fopen("pivo.txt","r");
fscanf(file,"%d",&n);
p = (pivo*)malloc(n*sizeof(pivo));
for(i=0;i<n;i++)
fscanf(file,"%s %lf %lf",p[i].ime,&p[i].kol,&p[i].cijena);
printf("\nCheapest beer is %s",cheap_price(p,n));
}
The txt file looks like this
9
Zlatorog 0.33 13.00
Calsberg 0.5 17.00
Ozujsko 0.5 13.00
Stella 0.33 13.00
Tuborg 0.5 15.00
Karlovacko 0.33 14.00
Guiness 0.33 18.00
Bavaria 0.25 15.00
Becks 0.33 17.00
And the debug from the printf I've placed inside the if statement is empty (if never executes)
Aucun commentaire:
Enregistrer un commentaire