Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(C Language) Only show filled entries in list

I hope this will be the last time I ask something here. Everything in my Food Ordering Algorithm is finished, but showing the list of filled orders shows every slot available (0 to 100), rather than just the entries filled out. Any way to fix that?

Case 3: Shows all orders made. (But I want just the entries that are filled out, not the empty ones.)

#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("%49[^\n]", lista[i].nome);
                fflush(stdin);
                
                printf("Digite seu endereco: \n");
                scanf("%149[^\n]", lista[i].endereco);
                fflush(stdin);
                
                printf("Digite seu pedido: \n");
                scanf("%299[^\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);
            

                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;
            
                
            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;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

change

for(i = 0; i < TAM; i++){

to

for(i = 0; i < codigo ; i++){

codigo is the count og entrries, TAM is the max number of entries

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading