Home > Net >  How to put struct inputs into arrays?
How to put struct inputs into arrays?

Time:08-22

I want to be able to input a struct, then put that struct into an array slot. Like, "input the data, put it into array slot 1, then another set of inputs into array slot 2" and so on.

(Btw, I'm aware of the whole 'gets() is obsolete' thing, but the thing I'm using, Dev-C , uses it just fine.)

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

struct res {
    char nome[40];
    char endereco[100];
    char pedido[200];
    char valor[20];
};

int main(int argc, char * argv[]) {     
    char M[100];
    int c = 1;
    int c2;
    int menu;
    int cod = 0;

    while (cod < 100) {
        cod = cod   1;
        struct res R1, R2, R3, R4;

        system("cls");
        printf("Digite seu nome: \n");
        gets(R1.nome);
        fflush(stdin);

        printf("Digite seu endereco: \n");
        gets(R2.endereco);
        fflush(stdin);

        printf("Digite seu pedido: \n");
        gets(R3.pedido);
        fflush(stdin);

        printf("Digite o valor total que vai pagar: \n");
        gets(R4.valor);
        fflush(stdin);

        system("cls");
        printf("============================\n");
        printf("Codigo: %d\n", c);
        printf("Nome: %s\n", R1.nome);
        printf("Endereco: %s\n", R2.endereco);
        printf("Pedido: %s\n", R3.pedido);
        printf("Valor: %s\n", R4.valor);
        system("pause");

        system("cls");
        printf("Escolha uma opcao\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:
            c = c   1;
        
            break;
        
            case 2: 
            system("cls");
            printf("Digite o codigo: \n");
            scanf("%d", &c2);
            fflush(stdin);
            if (c2 = c) {
            }
            else {
                printf("Codigo nao encontrado");
            }
            break;
    
            case 3:
            break;
    
            case 4:
            return 0;
        
            default:
            printf("Opcao invalido");
            system("pause");
        }
    }

    return 0;
}

CodePudding user response:

You could use an array of structs

#define SIZE 4

struct res x[SIZE];

To access an element in the array:

x[0].nome; // access 0th struct's member called "nome"

Notes:

  • Your code is vulnerable to a buffer overflow attack since gets() does not perform bounds checking on the size of its input.

  • fflush(stdin); invokes undefined behavior .

  • Always check the return value of scanf() and always use a width specifier where possible. Otherwise you should consider using a more safer and reliable alternative such as fgets().

CodePudding user response:

When you are trying to achieve an objective, it is handy to have a simplified version of code that does what you want to do. Study the following and think about how you might elaborate on it to get what you want/need. (I don't understand the 'prompts' of the menu you provided, and haven't tried to adapt this example to suit that.)

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

// Simplified version of your struct
// use 'typedef' to save lots of typing
typedef struct {
    char nome[40];
    char endereco[100];
} res_t;

// Use a function to collect information from user into one record
// here 'data entry' is simulated with a simple 'sprintf'
void fill( res_t *record, int x ) {
    sprintf( record->nome, "person %d", x   1 );
    sprintf( record->endereco, "other %d", (x   1) * 14 );
}

// Use a function to show information from one record
void show( res_t *record ) {
    memset( record, 0, sizeof *record ); // erase to nulls. clean start.
    printf( "Nome: %s\n", record->nome );
    printf( "Endereco: %s\n", record->endereco );
}

int main() {

#   define SIZE 2 // Use an alias for the quantity

    res_t arr[ SIZE ]; // define the array

    // a loop to collect the data
    int i;
    for( i = 0; i < SIZE; i   )
        fill( &arr[ i ], i );

    // a loop to display the data collected
    for( i = 0; i < SIZE; i   )
        show( &arr[ i ] );

    return 0;
}

Output:

Nome: person 1
Endereco: other 14
Nome: person 2
Endereco: other 28

Finally, "... into array slot 1" suggests you need to understand that array "slots" (called "elements") are numbered from 0, not 1. An array of 5 elements has "slots" 0, 1, 2, 3 and 4. The number of elements is 5.

  •  Tags:  
  • c
  • Related