1

summarizing what I'm doing, I'm carrying out a supermarket management project. in which I have to make a list saved in a txt file for the clients and products, and I'm confused because it worked on the clients, but on the products it is encrypted.

I imagine that the problem is in the code that I am going to put here. this is the structure of the products

typedef struct _produto{
    int ID;
    char nome[10];
    float preco;
    int validade;
    int quantidade;
    struct _produto* next;
} Produto; 

this is a procedure that will ask the values to user

void adicionarP(Produto* listaProduto) {
    Produto* novoProduto = (Produto*)calloc(1, sizeof(Produto));
    int auxInt = -1;
    float auxF = -1;
    
    printf("Digite o nome do produto: ");
    fflush(stdin); 
    fgets(novoProduto->nome, 10, stdin);
    novoProduto->nome[strcspn(novoProduto->nome, "\n")] = 0; // tira a nova linha "\n" que tenho no nome
    
    novoProduto->ID= obterIdAleatorioP(listaProduto);
    
    printf("Digite o preco do produto:");
    scanf("%f", &auxF);
    fflush(stdin);
    novoProduto->preco = auxF;

    auxInt = -1;
    printf("Digite a validade do produto:");
    scanf("%d", &auxInt);
    fflush(stdin);
    novoProduto->validade = auxInt;
    
    auxInt = -1;
    printf("Digite a Quantidade do stock:");
    scanf("%d", &auxInt);
    fflush(stdin);
    novoProduto->quantidade = auxInt;
    
    listaProduto = adicionarProduto(listaProduto, novoProduto);

    printf("\nAdicionado com sucesso!\n");
}

in the procedure above there are two functions, one called "adicionarProduto" that will basically see the list and when null will add the new product

Produto* adicionarProduto(Produto *listaProduto, Produto *novoProduto)
{
    if(listaProduto == NULL) return novoProduto;

    listaProduto->next = adicionarProduto(listaProduto->next, novoProduto);

    return listaProduto;
}

the function obterIdAleatorioP will generate a random id

int obterIdAleatorioP(Produto* listaProdutos) {
    int randomId;
    time_t t;

    srand((unsigned) time(&t));
    
    do {
        randomId = rand() % 10; 
    } while(existeProduto(listaProdutos,randomId));
    
    return randomId;
}

before doing any function(example: the function in the top) txt file:

1:IceCream:2.00:123:321

after doin any function txt file:

11802128:IceCream:2.00:123:321:0.000000:11802152:11802148
11825056::0.000000:11825080:11825076
11802176:pe:0.000000:11802200:11802196
Pac0
  • 21,465
  • 8
  • 65
  • 74
  • 1
    [srand() — why call it only once?](https://stackoverflow.com/q/7343833/995714) – phuclv Jun 02 '22 at 15:39
  • 2
    [about `fflush(stdin)`](https://stackoverflow.com/questions/2979209/using-fflushstdin) – Barmar Jun 02 '22 at 15:49
  • I tried to remove this function and add as I add the other numbers and it didn't come encrypted again. but thanks for your time, I appreciate it very much – Francisco Lemos Jun 02 '22 at 15:51
  • What do you mean by "on the products it's encrypted"? – Barmar Jun 02 '22 at 15:51
  • 2
    Don't post pictures of text, post text as properly formatted text. – Jabberwocky Jun 02 '22 at 15:59
  • i update the post, now there are example of txt file maybe it will be easier to understand – Francisco Lemos Jun 02 '22 at 16:00
  • @FranciscoLemos Images of code / output cause multiple problems compared to formatted code as text, see meta post [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/a/285557/479251) – Pac0 Jun 02 '22 at 16:01
  • @FranciscoLemos maybe, but still post you text as properly formatted text. – Jabberwocky Jun 02 '22 at 16:01
  • By "encrypted", do you mean that the data is not understood? or do you mean that it is actually encrypted and you can decrypt it to reproduce the original data? I suspect your data is not actually encrypted. – William Pursell Jun 02 '22 at 16:07
  • I imagine it's being encrypted but that's what I think I don't know if it's not being recognized or not. is that I think I have the data collection right, but yes, that could be the problem. but as you can see only the numbers are like this the name is right – Francisco Lemos Jun 02 '22 at 16:09

0 Answers0