Code enters infinite loop

Asked

Viewed 101 times

-1

Why is this code on loop infinite?

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

int main()
{
int i;
float salario[10],  valorDataBase, salarioReajustado[10];

printf("Informe o percentual  de reajuste da data-base: \n");
scanf("%f",  &valorDataBase);


    for (i=0 ; i<10 ;  i=i++)
    {
        printf("Informe o seu salario atual: \n");
        scanf("%f", &salario[i]);
    }
    //Impressão da lista de  dados do vetor salario reajustado
    for (i=0 ; i<10 ;  i=i++)
    {
        salarioReajustado[i] = salario[i] + salario[i]*valorDataBase/100;
        printf("Seu salario reajustado sera %f \n",salarioReajustado[i]);
    }

getch();
return(0);
}
  • 1

    i=i++ should only be i++

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

6

And I ask, why are you increasing the variable i and attributing to itself? This is undefined behavior. If only incremental works, then it is correct. You must have miscopied it from somewhere. Anyway the ideal is never copy anything, is to understand how it works to use properly. Simplifying:

#include <stdio.h>

int main() {
    float salario[10], valorDataBase;
    printf("Informe o percentual  de reajuste da data-base: \n");
    scanf("%f", &valorDataBase);
    for (int i = 0; i < 10; i++) {
        printf("Informe o seu salario atual:\n");
        scanf("%f", &salario[i]);
    }
    for (int i = 0; i < 10; i++) printf("Seu salario reajustado sera %f\n", salario[i] + salario[i] * valorDataBase / 100);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Final tip in real cases monetary values cannot be of the type float.

  • Just to complement, i = i++ first assign and then increment, on account of that the variable i always is 0, if you did so i = ++i would work, because first would increment and then assign.

Browser other questions tagged

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