-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.
thank you very much bro, that’s right
– Heitor Kenzou
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)
– Heitor Kenzou
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
– Heitor Kenzou
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 delimiterfor (i = 0; i < linha; i++)
– Andre