Yet another issue came up. Everything works just fine, but all that's left is to figure out how to put each input up in separate arrays, so the searching function I created would work properly. Whenever I put a couple of orders, then try and search via code (codigo), it only detects the most recent entry. I know the solution has something to do with malloc() and/or strcpy(), but I don't know the how or where of it.
Translation: Case 1: Make an order Case 2: Search your order (via code) Case 3: List of orders Case 4: Exit
[If I remove every fflush(), it just lets me put the name, and leaves everything else empty, so I have to keep fflush().]
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define TAM 100
typedef struct order
{
char nome[50];
char endereco[150];
char pedido[300];
char valor[10];
};
int main()
{
struct order lista[TAM];
int busca, acha, i, menu;
i = 0;
menu = 0;
int codigo = 0;
int c = 0;
while(menu != 4)
{
system("cls");
printf("====================\n");
printf("Selecione uma opcao\n");
printf("====================\n");
printf("1 - Cadastrar pedido\n");
printf("2 - Consultar pedido\n");
printf("3 - Emitir relatorio\n");
printf("4 - Sair\n");
scanf("%d", &menu);
fflush(stdin);
switch(menu)
{
case 1:
system("cls");
printf("Digite seu nome: \n");
scanf("I[^\n]", lista[i].nome);
fflush(stdin);
printf("Digite seu endereco: \n");
scanf("9[^\n]", lista[i].endereco);
fflush(stdin);
printf("Digite seu pedido: \n");
scanf(")9[^\n]", lista[i].pedido);
fflush(stdin);
printf("Digite o valor total: \n");
scanf("%9[^\n]", lista[i].valor);
fflush(stdin);
system("cls");
printf("Codigo: %d\n", codigo);
printf("Nome: %s\n", lista[i].nome);
printf("Endereco: %s\n", lista[i].endereco);
printf("Pedido: %s\n", lista[i].pedido);
printf("Valor total: %s\n", lista[i].valor);
system("pause");
i = i 1;
codigo = codigo 1;
break;
case 2:
system("cls");
printf("Insira o codigo que deseja buscar:\n");
scanf("%d", &busca);
fflush(stdin);
i = 0;
acha = 0;
while(i < TAM && acha == 0)
{
if(codigo == busca)
{
acha = 1;
}
else
{
i = i 1;
codigo = codigo 1;
}
if(acha == 1)
{
printf("=============================\n");
printf("Codigo: %d\n", codigo);
printf("Nome: %s\n", lista[i].nome);
printf("Endereco: %s\n", lista[i].endereco);
printf("Pedido: %s\n", lista[i].pedido);
printf("Valor total: %s\n", lista[i].valor);
printf("=============================\n");
system("pause");
}
else
{
printf("\n Codigo nao encontrado\n");
system("pause");
break;
}
}
break;
case 3:
system("cls");
c = 0;
for(i = 0; i < TAM; i ){
printf("=============================\n");
printf("Codigo: %d\n", c);
printf("Nome: %s\n", lista[i].nome);
printf("Endereco: %s\n", lista[i].endereco);
printf("Pedido: %s\n", lista[i].pedido);
printf("Valor total: %s\n", lista[i].valor);
printf("=============================\n");
c = c 1;
}
system("pause");
break;
case 4:
return 0;
default:
printf("Opcao invalido\n");
system("pause");
}
}
return 0;
}
EDIT: Sorry for not mentioning what I'm working on. This is sort of a food ordering system, where you put your name, address, what you want to order, and the amount of money you wish to pay. It's a college project so nothing too much is required.
CodePudding user response:
you dont need to search, the input number is the index of the entry, just check that its not out of range
case 2:
system("cls");
printf("Insira o codigo que deseja buscar:\n");
scanf("%d", &busca);
fflush(stdin);
if(busca < codigo)
{
printf("=============================\n");
printf("Codigo: %d\n", busca );
printf("Nome: %s\n", lista[busca ].nome);
printf("Endereco: %s\n", lista[busca ].endereco);
printf("Pedido: %s\n", lista[busca ].pedido);
printf("Valor total: %s\n", lista[busca].valor);
printf("=============================\n");
system("pause");
}
else
{
printf("\n Codigo nao encontrado\n");
system("pause");
break;
}
break;