Administrator of Parking in C

Asked

Viewed 189 times

0

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

Do a C program to assist in the administration of a parking lot For this procedure the following has been informed:

  • for each car must be informed the plate, the amount of hours that stayed in the parking
  • at the beginning of the opening of the parking must be established the amount to be charged per hour
  • It is known that the parking lot has 30 box
  • at the end of the day a report should be generated where you should have the car plate, the hour Qtde parked and the amount to be charged for each vehicle. should also be informed the total value of the parking box.

I was able to do it to a certain extent.. but I’m stuck when it comes to displaying the final figures.. how do I calculate the final value of the total?? which formula?

follows 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);

}
}

1 answer

1


Actually, there are several problems in your code:

  1. Does not initialize array dimension variables, such as i;
  2. Works improperly with arrays whose contents are a string (another array);
  3. Uses a proprietary and "deprecated library";
  4. Does not use tests on data entry, which could lead to problems accessing limits outside the array.

I redid your code with the adjustments I mentioned. It is commented for better visualization. As for the problem of the sum of hours, just add to the total variable the sum of hours of all 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 for you to test: https://repl.it/Ib4J/28

  • thanks.. I had managed to find in google.. only now it’s giving error in my string.. I declared right? if I inform more than 2 plates the application hangs

  • In fact, it contains a yes error, because the variable i has uninformed value. Vc is creating a table without dimensioning with an unidentified variable. I will fix your program and post the solution in an edition of the code.

  • I already put the code redone, no errors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.