What am I missing when using EOF in this case? C language

Asked

Viewed 54 times

1

I’m doing an exercise that basically asks to enter multiple lines of product data for a trader, but does not specify when to finish the repeat loop. In other examples you had how, for example, to end the loop when the registration number is equal to 0 or -1. I’m trying to use the EOF but unsuccessfully.

Follow my code below:`

#include <stdio.h>
#include <math.h>

int main (){

    int cod_produto=0, n_vendas=0, cod_maiorl=0, cod_maiorv;
    float valor_compra=0, valor_venda=0, total_compra=0, total_venda=0, lucro_total;
    int id1=0, id2=0, id3=0; //mercadorias que geraram lucros menor que 10%, >=10% ou ==20% e >20%
    float lucro, m_lucro=0,m_venda=0; //para calcular a % de lucro;
    
    while(scanf("%d%f%f%d", &cod_produto,&valor_compra, &valor_venda,&n_vendas)!=EOF){
        lucro = valor_venda*100/valor_compra-100;
        if(lucro<10) id1++; else if(lucro>=10 && lucro<=20) id2++; else if(lucro>20) id3++;
        if(lucro>m_lucro) {
            m_lucro = lucro;
            cod_maiorl=cod_produto;
        }
        if(n_vendas>m_venda){
            m_venda = n_vendas;
            cod_maiorv = cod_produto;
        }
        total_compra += valor_compra*n_vendas; total_venda+=valor_venda*n_vendas;
    }
    lucro_total = ((total_venda*100)/total_compra)-100;

    printf("Quantidade de mercadorias que geraram lucro menor que 10%%: %d\n", id1);
    printf("Quantidade de mercadorias que geraram lucro maior ou igual a 10%% e menor ou igual a 20%%: %d\n", id2);
    printf("Quantidade de mercadorias que geraram lucro maior que 20%%: %d\n", id3);
    printf("Codigo da mercadoria que gerou maior lucro: %d\n", cod_maiorl);
    printf("Codigo da mercadoria mais vendida: %d\n", cod_maiorv);
    printf("Valor total de compras: %.2f, valor total de vendas: %.2f e percentual de lucro total: %.2f%%", total_compra, total_venda,lucro_total);

    return 0;
}

`

  • Are you reading the terminal input, or passing a file as the default input? If you are reading from the terminal, how do you insert the EOF character?

  • At the terminal, it’s for an online judge of the college, how do you insert the character I don’t know young.

  • Like scanf returns the amount of values that were correctly read, you could check if they were read 4 values: while(scanf(....) == 4)

  • 1

    By default, keyboard read drivers convert the Control-D character typed at the beginning of a line into a file end indicator (EOF).

1 answer

0

Running in the terminal? If so, you can save what your input would be in a text file and test by redirecting the input. Assuming the data file is named input.txt, you can run as follows.

 C:\Users\usuario> nome_programa.exe < entrada.txt

Or, you can redirect the input into the code itself with the freopen function (just for testing). The example below shows how I did to use as input of my program the data that was in the name file in. If your file is in the same directory there is no need to put the complete path.

 freopen("C:/Users/slipn/Desktop/in", "r", stdin);

Browser other questions tagged

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