-1

Sorry if this sounds stupid, but 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;
}

Hope this makes some sense. Thanks in advance.

EDIT: I probably should've mentioned that this is just for a college project, nothing too major. So I don't think attacks are a factor here.

Scuttler
  • 13
  • 3
  • 1
    So what is the problem you have with creating an array of `struct res`? You seem to know how to create an array of `char` - there's not really any difference – UnholySheep Aug 21 '22 at 23:05
  • Declare an array of structs: `struct res my_array[MAX_LEN];` then just access each struct entry `fgets(my_array[index].nome , sizeof(my_array[index].nome));` – kaylum Aug 21 '22 at 23:05
  • 4
    "*gets() is obsolete' thing, but the thing I'm using, Dev-C++, uses it just fine.)*". That's like saying you don't need a seat belt because you have been driving without one just fine. That is, until you crash. Same here - `gets` is inherently unsafe because it allows overflowing the buffer. Of course it "works fine" if you don't overflow it but that's until someone enters a long input. – kaylum Aug 21 '22 at 23:06
  • [no need to `fflush(stdin)`](https://stackoverflow.com/questions/18170410/what-is-the-use-of-fflushstdin-in-c-programming) – yano Aug 21 '22 at 23:10
  • 1
    @kaylum it's more like saying that you don't need a seatbelt because of *the model of car you're driving*. – Karl Knechtel Aug 22 '22 at 01:29
  • 1
    Welcome to Stack Overflow. *What actually is the question here*? It seems that you know how to create an array, and that you know how to assign to an element of an array. It seems that you want to make an array where the elements are structs (your `struct res` type), so that you can assign a struct to be an element of that array. So... did you try making an array with that element type, and then assigning the elements? What problem(s) did you encounter? What (in your own estimation) don't you understand about the task, and what do you need to know from us? – Karl Knechtel Aug 22 '22 at 01:31

2 Answers2

4

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().

programmer
  • 669
  • 3
  • 11
0

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.

Fe2O3
  • 6,077
  • 2
  • 4
  • 20