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 thewhile
... That may be the problem, because once you enter the first time you opened to0
and then never enter.– Maicon Carraro
in fact it drops us if right in the first iteration, then passes all the others without happening anything.
– Mathi901
but think about me, after I enter the first time you assign value 0 to the variables you use in
while
soon never again thewhile
will enter– Maicon Carraro
but I have to reset them after each operation, how can I proceed then?
– Mathi901
what the purpose of if?
– Maicon Carraro
when he enters the
if%5
you want to add cars until it enters a value other than 0-5?– Maicon Carraro
try to change his
while
fordo.. while
:)– Maicon Carraro
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.
– Mathi901
Let’s go continue this discussion in chat.
– Mathi901