Problem with reading string in C inside for()

Asked

Viewed 19 times

0

I’m doing an algorithm for reading data for discount accounting, but the first data I have to read is the name. Everything worked perfectly, but I have to limit it to 10 "people" to fill in the data and so I put the whole structure of the algorithm in the command for(). The problem is that, after receiving start the repetition again, it does not let fill in the name again, jumping straight to the next given var int. I tried with while() but it didn’t work either. Someone would know what to be?

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

int main ()
{
    int count;
    int salario, cpf, depend, salmin, renda, faixa1, faixa2, faixa3, faixa4;
    char contrib[61];
    float irrf, impdepen, desctot;
    
    
    for (count = 1; count <= 10; count++){
        
            printf("CONTRIBUINTE %i", count);
                    
            printf("\n\nINFORMAR O NOME DO CONTRIBUINTE:\n");
            fgets(contrib,61,stdin);
            
            printf("INFORMAR O CPF DO CONTRIBUINTE:\n");
            scanf("%i", &cpf);
            
            printf("INFORMAR A RENDA DO CONTRIBUINTE:\n");
            scanf("%i", &salario);
            
            printf("INFORMAR O NUMERO DE DEPENDENTES DO CONTRIBUINTE:\n");
            scanf("%i", &depend);
            
            printf("INFORMAR O VALOR DO SALARIO MINIMO ATUAL:\n");
            scanf("%i", &salmin);
            
            impdepen = depend * 0.046 * salmin;
            faixa1 = salmin * 2;
            faixa2 = salmin * 3;
            faixa3 = salmin * 5;
            faixa4 = salmin * 7;
            
            if (salario > 0 && salario < faixa1){
                printf("CONTRIBUINTE ISENTO DE DESCONTO\n\n");
            }else if (salario > faixa1 && salario <= faixa2) {
                irrf = salario * 0.05;
                desctot = irrf - impdepen;
                printf("CONTRIBUINTE %s\n PORTADOR DO CPF: %i\nTENDO %i DEPENDENTES\nCOM RENDA DECLARADA DE %i REAIS\nTEM DESCONTO DE %.2f REAIS COM BASE NO CALCULO DE DESCONTO\n\n", contrib, cpf, depend, salario, desctot);
            }else if (salario > faixa2 && salario <= faixa3) {
                irrf = salario * 0.1;
                desctot = irrf - impdepen;
                printf("CONTRIBUINTE %s\n PORTADOR DO CPF: %i\nTENDO %i DEPENDENTES\nCOM RENDA DECLARADA DE %i REAIS\n TEM DESCONTO DE %.2f REAIS COM BASE NO CALCULO DE DESCONTO\n\n", contrib, cpf, depend, salario, desctot);
            }else if (salario > faixa3 && salario <= faixa4) {
                irrf = salario * 0.15;
                desctot = irrf - impdepen;
                printf("CONTRIBUINTE %s\n PORTADOR DO CPF: %i\nTENDO %i DEPENDENTES\nCOM RENDA DECLARADA DE %i REAIS\nTEM DESCONTO DE %.2f REAIS COM BASE NO CALCULO DE DESCONTO\n\n", contrib, cpf, depend, salario, desctot);
            }else if (salario > faixa4) {
                irrf = salario * 0.2;
                desctot = irrf - impdepen;
                printf("CONTRIBUINTE %s\n PORTADOR DO CPF: %i\nTENDO %i DEPENDENTES\nCOM RENDA DECLARADA DE %i REAIS\nTEM DESCONTO DE %.2f REAIS COM BASE NO CALCULO DE DESCONTO\n\n", contrib, cpf, depend, salario, desctot);
            }
    }
        
    return 0;
}

Thanks in advance for your attention!

  • 1

    The code must be between ``` and not '''.

  • The old known end-of-line character problem that remains in the input buffer causes, in your case, the reading of the string contrib consider only this character and have zero length. See question 12.26b at: http://www.faqs.org/faqs/C-faq/faq/

No answers

Browser other questions tagged

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