The program compiled more or less without problems but suddenly I got this error and I do not know how to solve it. The program is not completely finished, I still have to solve errors in the results of operations but that should not be the problem (no?).
#include<stdio.h>
#include<stdlib.h>
#define TAM 1000
#define TRUE 1
#define FALSE 0
int menu(){
int op;
do{
printf("--------------- Menu de opciones ---------------\n\n");
printf("1. Consulta de un compuesto\n\n2. Compuestos con mejor y peor balance.\n\n3. Compuestos de bajo rendimiento.\n\n");
printf("4. Generar balances positivos y negativos.\n\n0. Terminar.\n\n");
printf("------------------------------------------------\n\nElige una opcion (0-4) :\n");
scanf("%d", &op);
if(op<0 || op>4){ printf("Opcion incorrecta, vuelva a elegir.\n\n"); system("PAUSE"); system("cls");}
}while(op<0 || op>4);
return op;
}
int leer_datos(float ingresos[TAM], float costesProd[TAM], float costesDist[TAM]){
int i, n;
float in, pr, di;
FILE *f1;
for(i=0;i<TAM;i++){
ingresos[i]=0;
costesProd[i]=0;
costesDist[i]=0;
}
f1=fopen("compuestos.txt", "r");
if(f1==NULL){fclose(f1); return -1;}
while(fscanf(f1, "%d%f%f%f" ,&n, &in, &pr, &di)!=EOF){
ingresos[n]=in;
costesProd[n]=pr;
costesDist[n]=di;
}
fclose(f1);
return 0;
}
void consulta_compuesto(float ingresos[TAM], float costesProd[TAM], float costesDist[TAM]){
int x;
printf("Codigo del compesto: \n");
scanf("%d", &x);
if(x>=0 && x<=999){
if(ingresos[x]==0){
printf("\n\nCompuesto no disponible\n\n");
system("PAUSE");
}
else{
printf("\nIngresos: %.2f\n\nCostes de produccion: %.2f\n\nCostes de distribucion: %.2f\n\n", ingresos[x], costesProd[x], costesDist[x]);
printf("BALANCE: %.2f\n\n\n\n", ingresos[x]-costesProd[x]-costesDist[x]);
system("PAUSE");
}
}
else{printf("\n\nCodigo no valido\n\n"); system("PAUSE");}
}
void compuestos_mejor_peor(float ingresos[TAM], float costesProd[TAM], float costesDist[TAM],int *mej, int *per){
int i=0;
float bmej, bper;
*mej=ingresos[i]-(costesProd[i]-costesDist[i]);
*per=ingresos[i]-(costesProd[i]-costesDist[i]);
for(i=0;i<TAM;i++){
if(ingresos[i]-(costesProd[i]-costesDist[i])>*mej) *mej=i;
if(ingresos[i]-(costesProd[i]-costesDist[i])<*per) *per=i;
}
}
void compuestos_bajo_rendimiento(float ingresos[TAM], float costesProd[TAM], float costesDist[TAM]){
int i, k=0;
float media, m=0, md;
for(i=0;i<TAM;i++){
m+=ingresos[i];
if(ingresos[i]!=0) k++;
}
media=m/k;
md=media*0.1;
printf("Compuestos de bajo rendimiento:\n\n");
for(i=0;i<TAM;i++){
if(ingresos[i]<md && costesDist[i]>costesProd[i]){
printf("%d\n", i);
}
}
system("PAUSE");
}
int generar_balances(float ingresos[TAM], float costesProd[TAM], float costesDist[TAM]){
int i;
FILE *fpos, *fneg;
fpos=fopen("pos.txt", "w");
if(fpos==NULL) return -1;
fneg=fopen("neg.txt", "w");
if(fneg==NULL) return -1;
for(i=0;i<TAM;i++){
if(ingresos[i]-(costesProd[i]-costesDist[i])>0){
fprintf(fpos,"%d (%f euros)\n", i, ingresos[i]-(costesProd[i]-costesDist[i]));
}
else fprintf(fneg,"%d (%f euros)\n", i, ingresos[i]-(costesProd[i]-costesDist[i]));
}
fclose(fpos);
fclose(fneg);
return 0;
}
int main(){
int op, e, mej=0, per=0, r;
float ingresos[TAM], costesProd[TAM], costesDist[TAM];
e=leer_datos(ingresos, costesProd, costesDist);
if(e==-1){printf("Error a leer el fichero"); return 0;}
do{
op=menu();
switch(op){
case 1: consulta_compuesto(ingresos, costesProd, costesDist);
break;
case 2: compuestos_mejor_peor(ingresos, costesProd, costesDist, &mej, &per);
printf("\n\nCompuesto con mejor balance: %d (%.2f euros)\n\n", mej, ingresos[mej]-costesDist[mej]-costesProd[mej]);
printf("Compuesto con peor balance: %d (%.2f euros)\n\n", per, ingresos[per]-costesDist[per]-costesProd[per]);
break;
case 3: compuestos_bajo_rendimiento(ingresos, costesProd, costesDist);
break;
case 4: r = generar_balances(ingresos, costesProd, costesDist);
if(r==-1){
printf("ERROR creando fichero neg.txt\n\n");
}
else {printf("Los ficheros se han creado correctamente.\n\n");}
break;
}
}while(op!=0);
return 0;
}
I think the problem may be in the function generate_balances (), but I'm not sure.