help with structures

1

Hello I need to fill a structure with a condition that is when I enter a negative weight or my path is greater than 3000 stop asking me to enter more data here I leave something that I have done but I can not make the condition

#include <stdio.h>
#include <stdlib.h>
#define TAM  2000
struct comercio{
    int codigo;
    char entrega;
    float peso;
    float recorrido;
};

void lleratoscomercio(struct comercio *Objcomercio);
void promediodelpeso(struct comercio *Objcomercio);
void recorridodistancia(struct comercio *Objcomercio);
void formaentregas(struct comercio *Objcomercio);
void recudototal(struct comercio *Objcomercio);
int main()
{
    char elem [50];
    struct comercio micomercio[TAM], *ptrcomercio;
    ptrcomercio=&micomercio[0];
    lleratoscomercio(ptrcomercio);


    //return 0;
}
void lleratoscomercio(struct comercio *Objcomercio){
int i;
int cont=0;


    while(Objcomercio->peso < 0 || Objcomercio->recorrido<3000){
        printf("ingrese el codigo %d:",i+1);
        scanf("%d",&Objcomercio->codigo);

        printf("ingrese el tipo de entrega desiganado por una sola letra:");
        scanf("%s",&Objcomercio->entrega);

        printf("ingrese el peso:");
        scanf("%f",&Objcomercio->peso);

        printf("ingrese el recorrido:");
        scanf("%d",&Objcomercio->recorrido);

    }

}

I hope you help me out of hand thanks

    
asked by Hector Luna 22.10.2018 в 03:18
source

1 answer

0

First of all I point out a small detail, recorrido is of float type and you scan it as type % d , integer, when it should be floating: p>

scanf("%f", &Objcomercio->recorrido);

Now, for the condition I would do it with a do{...}while(...) to be able to allow at least the entry of the data at least once and thus evaluate the condition. Here is an easy way to do it:

int main()
{
    char elem [50];
    struct comercio micomercio[TAM];
    lleratoscomercio(micomercio);


    return 0;
}
void lleratoscomercio(struct comercio *Objcomercio){
    int i;
    int cont=0;


    do{
        printf("ingrese el codigo %d:",i+1);
        scanf("%d", &Objcomercio[i].codigo);

        printf("ingrese el tipo de entrega desiganado por una sola letra:");
        scanf("%s", &Objcomercio[i].entrega);

        printf("ingrese el peso:");
        scanf("%f", &Objcomercio[i].peso);

        printf("ingrese el recorrido:");
        scanf("%f", &Objcomercio[i].recorrido);

        i++;
    }while(Objcomercio[i-1].peso > 0 && Objcomercio[i-1].recorrido < 3000);

    for(int j = 0;  j < i; j++){
        printf("%d\n", Objcomercio[j].codigo);
    }

}

I made some changes:

  • I sent the array struct micomercio directly to the function and use it as an array.
  • I used a do while cycle for the condition, in which I changed it to AND , just in case one of the two is not met (with weight & lt ; 0 or travel & 3000) the cycle ends.

Well, I hope it helps, greetings.

    
answered by 22.10.2018 в 08:18