0

ポインターと malloc を使用して構造体に 2 つの入力 (monto と loc) を入力する必要がある簡単な演習をしようとしています。データを印刷しようとすると、ゴミが表示されます。何が起こっているのかを確認しようとしたので、入力後にデータを出力し、-1414812757 -15883928734546002000000000000000000000.00

struct transaccion{
    int loc;
    float monto;
    char nombre[50];
} cliente,*pcliente;

int cargadatos (struct transaccion*);
void mostrarlocalidad(struct transaccion*,int);
void mostrarestructura(struct transaccion*);

void main()
{
    int tam=50,ll;
    struct transaccion *pTrans;
    pTrans=(struct transaccion*)malloc(sizeof(struct transaccion)*tam);
    pTrans[0].monto=5;
    if(pTrans==NULL){
        puts("Falta memoria");
        exit(3);
    }

    ll=cargadatos(pTrans);
    mostrarlocalidad(pTrans,ll);
    free(pTrans);
    system("pause");
}

int cargadatos (struct transaccion *pTrans)
{
    int i=0;
    while (pTrans[i].loc!=0){
        puts ("ingrese numero de localidad");
        scanf("%d", &pTrans[i].loc); fflush (stdin);
        puts ("ingrese monto");
        scanf("%.2f",&pTrans[i].monto); fflush(stdin);
        int j=0;
        for (j=0; j<=i; j++)  {
            if (pTrans[j].loc==pTrans[i].loc){
                pTrans[j].monto=pTrans[j].monto+pTrans[i].monto;
                i--;
            }
        }
        printf("%d %.2f \n",pTrans[i].loc,pTrans[i].monto);
        i++;
    }
    return;
}

私は何時間も多くのことを試してきましたが、エラーがどこにあるのかわかりません。

4

4 に答える 4