C Parking Administrator

0

How do I calculate the totals of the car park values as stated below:

Make a C program to help manage a parking lot For this procedure the following was reported:

  • For each car you should be informed of the license plate, the number of hours you have been in the parking lot
  • At the start of the parking opening, the amount to be charged per hour must be established
  • It is known that parking has 30 boxes
  • At the end of the day a report should be generated where you should have the car plate, the number of hours parked and the amount to be charged to each vehicle. you should also be informed of the total value of the parking box.

I managed to do it to some extent .. but I'm stuck at the time of displaying the final values .. how do I calculate the final value of the total ?? which formula?

follow my code below:

#include<stdio.h>
#include<math.h>
#include<conio.h>

main(void){

int i, qtdCarro, hora[i];
char placa[i][256];
float valorHora, valor, total;

printf("Valor por hora: ");
scanf("%f", &valorHora);
fflush(stdin);

printf("Numero de carros: ");
scanf("%d", &qtdCarro);
fflush(stdin);
if(qtdCarro > 30){
    printf("O Estacionamento so possui 30 vagas");
    return 0;
} else {
    for (i=1; i<= qtdCarro ; i++){
    printf("Placa do veiculo %d: ", i);
    scanf("%s", &placa[i]);
    fflush(stdin);
    printf("Horas do veiculo %d: ", i);
    scanf("%d", &hora[i]);
    fflush(stdin);      
}   


    for (i=1 ; i <= qtdCarro; i++){
    valor = hora[i] * valorHora;
    printf("Veiculo da placa %s ficou %d horas e gastou %f reais \n", placa[i], hora[i], valor);


}

    printf("Valor total gasto no estacionamento e de: %f \n", total);

}
}
    
asked by anonymous 08.06.2017 / 00:17

1 answer

1

In fact, there are several issues in your code:

  • Does not initialize array dimension variables, such as i;
  • Inappropriate work with arrays whose contents are a string (another array);
  • Uses a proprietary and deprecated library;
  • Does not use data entry tests, which could lead to problems accessing boundaries outside the array.
  • I've refined your code with the settings I've spoken. It is commented for better visualization. As for the problem of the sum of the hours, it is enough to add to the total variable the sum of the hours of all the cars. As follows:

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    
    main(void){
    
        int i, qtdCarro, hora[30];
        char *placa[30];  //declaração do array de strings
        float valorHora, total[30], total_geral=0;  //total_geral é o somatório de todas as horas
                                                    //o vetor total armazena o total de cada veículo
        printf("Valor por hora: ");
        scanf(" %f", &valorHora);
        fflush(stdin);
    
        //leitura do número de carros - dentro dos limites do estacionamento
        do{
            printf("Numero de carros: ");
            scanf(" %d", &qtdCarro); printf("\n"); 
            if(qtdCarro > 30)
              printf("\nO Estacionamento so possui 30 vagas\n");
            fflush(stdin);
        }while(qtdCarro>30);
    
        for (i=0;i<qtdCarro; i++){
            printf("Placa do veiculo %d: ", i+1);
            // a leitura da string abaixo não necessita de &, pois o nome do array já é um ponteiro!
            // perceba que estou usando a "aritmética de endereços" pois facilita a legibilidade do código.
            // como o vetor foi declarado como *placa[30], facilita o acesso a cada elemento em cada 'linha' do array        
            scanf(" %s", placa + i);
            //printf("Placa do veiculo %d é %s\n", i+1, placa + i);  //debugando o código, basta descomentar o inicio da linha
            fflush(stdin);
            printf("Horas do veiculo %d: ", i+1);
            scanf("%d", &hora[i]);
            fflush(stdin);  printf("\n");      
        }   
    
        for(i=0;i<qtdCarro;i++){
          total[i]=hora[i]*valorHora;
          printf("Veiculo da placa %s ficou %d horas e gastou %f reais \n", placa+i, hora[i], total[i]);
          total_geral+=total[i]; 
        }
          printf("\nValor total arrecadado no estacionamento é de: %f \n", total_geral);
    }
    

    Link to you test: link

        
    08.06.2017 / 01:47