Okay, hopefully after this, I won't ask anymore questions here. I redid the entire code, replacing gets() with scanf(). Now though, whenever I choose to put a new order for the program, I just put the name and the process just ends, showing "return value 3221225477", preventing me from typing in the address, request and price. I screwed up somewhere, but I don't know what. Here's what I did:
Translation:
- Case 1: Type name, address, order, total price.
- Case 2: Search for code (Issue: new string overwrites old one constantly)
- Case 3: Show list of all orders created.
- Case 4: Exit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define TAM 100
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;
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;
}
CodePudding user response:
well the immediate cause is this
int busca, acha, i, menu; <<<<=== declare i , but not set to a value
....
scanf("I[^\n]", lista[i].nome); <<<==== use i
you must set i to zero
int i = 0;