Track data count shows wrong result

Asked

Viewed 57 times

-1

A city hall conducted a survey among its inhabitants, collecting data on the population’s salary. The city wants to know: a) Number of people with a salary higher than R $ 2000,00.Number of people with a salary lower than R $ 2000,00The end of data reading will be with the entry of value -1 for salary.

I believe I’m not entirely wrong, but when I execute him, the first if kind of runs over each other if, at the time of counting the number of people counts only the number of the first if that I put.

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

int contador1=0,contador2=0, salario;

main()

{
    do{
       printf("Digite o seu salario (para cancelar digite -1): ");
       scanf("%f", &salario);

       if(salario>=2000){
            contador1=contador1+1;
       }

       if(salario<2000){
            contador2=contador2+1;
       }
    }while(salario>0);

   printf("Pessoas com salario maior de 2000.00 reais: %d \ne menor de 2000.00 reais: %d\n",contador1, contador2);

   system("pause");
   return 0;


}

2 answers

1

When you do the following:

scanf("%f", &salario);

you are using the flag %f which is a Float number.

You should use the decimal integer, since the variable is an integer:

scanf("%d", &salario);

More about the possible flags used here (in English).

  • Wow. I forgot it, because I actually spent SO much time trying to solve it that I ended up changing so many things, I changed variables, I added, I took.. and I ended up forgetting that, thank you very much! I guess I wouldn’t have realized the mistake.

1


There are several problems in the algorithm.

One of them is that the data type used for salary is an integer and then asks to type in a floating point type. You have to reconcile the two. If you compiled is using a very bad compiler, I suggest looking for another one. In fact there are other problems in the code that the compiler should not pass up.

In fact float nor is the right type for something like salary, but it is acceptable for exercise, only you know this guy has a problem with monetary values.

The exercise asks to leave when typing -1, and this is not being verified. Once you enter this number you must close the repetition algorithm immediately, you cannot continue running other parts of it because it would give a final result misguided by counting what you should not (the -1).

Then you should just check if the value is in one of the tracks to establish the counter. The other track is exactly opposite to the first one, so just do the counter on else of this comparison block. The else just for that.

The while can be simplified when the output condition is inside it (it can be as if it were infinite since the output will be manual).

Variable names could be more significant and increment could be simplified.

The statement is ambiguous (actually it is wrong strictly speaking), its code considered that if it is equal to 2000 considers as if it were bigger, I kept this.

I’ve improved a few other things.

#include <stdio.h>

int main() {
    int abaixo2000 = 0, acima2000 = 0;
    while (1) {
        float salario;
        printf("Digite o seu salario (para cancelar digite -1): ");
        scanf("%f", &salario);
        if (salario == -1) break;
        if (salario < 2000) abaixo2000++;
        else acima2000++;
    }
    printf("Pessoas com salario maior de 2000.00 reais: %d\ne menor de 2000.00 reais: %d\n", acima2000, abaixo2000);
}

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

  • Ah, yes, I get it! Thank you very much for the answer, it was a lot clearer some things I didn’t know yet.

Browser other questions tagged

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