Program stopping working in C

Asked

Viewed 36 times

-1

Hello, I am having a problem with a code of mine in C. It is a work for college and the code is simple, it is a script to receive data (users and products), sell the products and list existing customers, I did the part of user registration and products normally, but on the part of searching if a user already exists is giving an error, it simply gives "the program stopped responding".

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

char nomeCliente[100][50];
int cpf[50][30];

char descricaoProduto[20][30];
int valorDeVenda[50];

int i;

int menu;

void cadastroClientes();
void cadastroProduto();
void venderProdutos();

int main()
{
    printf("1 - Cadastrar usuário");
    printf("\n2 - Cadastrar produto\n");
    printf("3 - Vender Produtos\n");
    printf("4-Relatorios\n");
    printf("\nOpção a ser acessada: ");
    scanf("%d", &menu);

    switch (menu)
    {
    case 1:
        cadastroClientes();
        break;
    case 2:
        cadastroProduto();
        break;
    case 3:
        venderProdutos();
        break;
    }
}

void cadastroClientes()
{
    static int linha;
    do
    {

        printf("\nNome: \n");
        scanf("%s", &nomeCliente[i]);
        printf("\n CPF: ");
        scanf("%s", &cpf);
        printf("\ndigite 1 para fazer outro cadastro ou outro valor para voltar ao menu ");
        scanf("%d", &menu);
        if (menu != 1)
        {
            main();
        }
        linha++;
    } while (menu == 1);
}

void cadastroProduto()
{
    do
    {
        printf("\n nome do produto: ");
        scanf("%s", &descricaoProduto);
        printf("\npreço de venda ");
        scanf("%s", &valorDeVenda);
        printf("\n digite 2 para fazer outro cadastro de produto ou outro número para voltar ao menu ");
        scanf("%d", &menu);
        if (menu != 2)
        {
            main();
        }
    } while (menu == 2);
}

void venderProdutos()
{

    char cliente;

    printf("digite o nome do cliente: ");
    scanf("%c", &cliente);

    for (i = 0; i < 100; i++)
    {
        if (strcmp(nomeCliente[i], cliente) == 0)
        {
            printf("encontrou");
        }
        else
        {
            printf("erro");
        }
    }
}

the error is somewhere in the "sell Products()" function, specifically in strcmp. I tested the comparison between the "customer name, "the")" and it worked, so I think there’s some problem with the index or something.

1 answer

0

You declared the variable cliente as a type char.

This means that it will store a single character without even being followed by a null Terminator, like strings in C.

Try declaring it as an array of characters of size 50 (same size you are storing in nomeCliente) and then do the scanf using %s to read a string, or use %49s to make sure that the input will not burst the size of your array.

int venderProdutos()
{
    char cliente[50];

    printf("digite o nome do cliente: ");
    scanf("%49s", cliente);

    for (i = 0; i < 100; i++)
    {
        if (strcmp(nomeCliente[i], cliente) == 0)
        {
            printf("encontrou");
            return i;
        }
    }

    printf("erro");
    return -1;
}

It would also be nice to receive how many customers you have populated in the array of nomeCliente in the function, this way you would only run this number instead of going through all 100 positions, after all, we do not know if all 100 positions are filled.

  • thank you very much bro, that’s right

  • if you can help me with another little problem I’m facing, thank you... I also have to list several types of data, I’m doing this using the following code snippet: "int i; for (i = 0; i < 100; i++) { printf("%s n", client name[i]); }" but the "error" is showing 100 results, which is the size I put there, but I want to show only existing clients, if there is only 1, can only show 1 and so on, the way I did it is always showing 100 results, and when there is no client it shows 100 (nulls)

  • now that I realize that’s exactly what you said down there, but I keep asking the question because I’m not quite sure how I would do it the way you said it

  • C does not keep count of how many array items are populated, you have to declare a variable to keep that count. It even looks like you already declared the variable linha (which you should initialize with 0). Just use it as the delimiter for (i = 0; i < linha; i++)

Browser other questions tagged

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