Tie problem for

Asked

Viewed 122 times

2

I’m having a problem and I can’t identify it in my code, I have a for with 60 iterations, and it’s falling in if’s when I identify it (i in this case) is divisible by 5 or 3 only in the first iteration, when i is 0. After that he passes the others until end without stopping in the if’s.

int main(){

int i,j,opcao,qtd_taxi,qtd_passageiro,tamFilaTaxi=0,tamFilaPassageiro=0;
char id_a_inserir[3];

Fila* taxis = criar_fila();
Fila* passageiros = criar_fila();

for(i=0;i<60;i++){
    printf("-- Minuto %d -- \n",i);
    if(i%5==0){
        while(qtd_taxi<0 || qtd_taxi>5){
            printf("Digite a quantidade de carros: \n");
            scanf("%d",&qtd_taxi);
            fflush(stdin);
            if(qtd_taxi>=0 && qtd_taxi<=5){
                for(j=0;j<qtd_taxi;j++){
                    tamFilaTaxi ++;
                    sprintf(id_a_inserir, "t%d", tamFilaTaxi);
                    inserir(taxis,id_a_inserir);
                }
            }else printf(" *Quantidade invalida! \n \n");
        }
        qtd_taxi=0;
    }
    if(i%3==0){
        while(qtd_passageiro<0 || qtd_passageiro>3){
            printf("Digite a quantidade de passageiros: \n");
            scanf("%d",&qtd_passageiro);
            fflush(stdin);
            if(qtd_passageiro>=0 && qtd_passageiro<=3){
                for(j=0;j<qtd_passageiro;j++){
                    tamFilaPassageiro ++;
                    sprintf(id_a_inserir, "p%d", tamFilaPassageiro);
                    inserir(passageiros,id_a_inserir);
                }
            }else printf(" *Quantidade invalida! \n \n");
        }
        qtd_passageiro=0;
    }
}
  • their if seem ok, but you’re not initiating the variables you use in the while... That may be the problem, because once you enter the first time you opened to 0 and then never enter.

  • in fact it drops us if right in the first iteration, then passes all the others without happening anything.

  • but think about me, after I enter the first time you assign value 0 to the variables you use in while soon never again the while will enter

  • but I have to reset them after each operation, how can I proceed then?

  • what the purpose of if?

  • when he enters the if%5 you want to add cars until it enters a value other than 0-5?

  • try to change his while for do.. while :)

  • o for this representing one hour (60 min), and every 5 minutes it must include cars in a list (1º if), and every 3 minutes it must include passengers in another list (2º if), I used the rest of the division to know when to perform each operation. Ex: every 3 minutes, goes 0,3,6,9. So if it is divisible by 3, falls in the if.

Show 4 more comments

1 answer

3


As I explained in the comments, the first time will always come and then you are assigning 0 the variable you use to control the while then it will always give false and will never enter. To force you to enter at least once you can use the do ... while

for(i=0;i<60;i++){
    printf("-- Minuto %d -- \n",i);
    if(i%5==0){
        do {
            printf("Digite a quantidade de carros: \n");
            scanf("%d",&qtd_taxi);
            // ... restante
        } while(qtd_taxi<0 || qtd_taxi>5);
        qtd_taxi=0;
    }
    if(i%3==0){
        do {
            printf("Digite a quantidade de passageiros: \n");
            scanf("%d",&qtd_passageiro);
            // restante do código
        } while(qtd_passageiro<0 || qtd_passageiro>3);
        qtd_passageiro=0;
    }
}

Browser other questions tagged

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