I rewrote the code to show more or less how it is done. Note that there is still much to improve on it. There are errors that can happen easily and are being checked out, out of other details, but this is how you do it for real, even something simple.
Place several arrays within the struct
makes no sense, what you want is simpler and more viable than this. Your struct
agenda should have only the size and a pointer to the contact list. This is already conceptually correct and makes it easy to do the rest.
I created a new structure with the contact input. If you want you can call it contact, but I don’t know if it is suitable in a real context. It will have the number and the name. The number I changed to string because it’s usually the phone number, so the int
is not appropriate. If it’s something else, then it seems to make less sense to let the person type it.
I predetermined that the name will have 30 characters, you can use the size you want.
I controlled the typing better. It has flaws in using the scanf()
so, but for an exercise is good.
Note that I gave a better name to the function that initializes the agenda, to make it clear that you need to release it. And I created before anyway, even to avoid another error. And I switched the boot to reboot. There’s an error there in the reboot, which in a way I introduced, see if you can identify and fix, so really exercise.
If you don’t understand any point ask specific questions.
I have made other simplifications and improvements. Since you want to learn, pay attention to every detail, even in the spaces. There were others wrong that I corrected, gets exercise identify them.
#include <stdio.h>
#include <malloc.h>
#define MAX 50
typedef struct {
char numero[12]; //permite um telefone nacional
char nome[31]; //permite 30 caracteres
} Entrada;
typedef struct {
int tamanho;
Entrada *contatos;
} Agenda;
void cadastrar(Agenda *agenda) {
int op = 1;
do {
printf("Digite o telefone\n");
scanf("%11s", agenda->contatos[agenda->tamanho].numero);
printf("Digite o seu nome\n");
scanf(" %30[^\n]", agenda->contatos[agenda->tamanho].nome);
printf("Digite 1 para continuar 2 para parar\n");
scanf("%d", &op);
agenda->tamanho++;
} while (op != 2);
}
void imprime(Agenda agenda) {
for (int i = 0; i < agenda.tamanho; i++) printf("%11s - %s\n", agenda.contatos[i].numero, agenda.contatos[i].nome);
}
Agenda iniciaAlocaAgenda() {
return (Agenda) { .tamanho = 0, .contatos = malloc(sizeof(Entrada) * MAX) };
}
void liberaAgenda(Agenda agenda) {
free(agenda.contatos);
}
int main() {
Agenda agenda = iniciaAlocaAgenda();
int op = 1;
do {
printf("1-Adicionar numero\n");
printf("2-Remover numero\n");
printf("3-Procurar numero\n");
printf("4-Imprimir\n");
printf("6-Reincia a agenda\n");
printf("7-Parar\n");
scanf("%d", &op);
switch (op) {
case 1:
cadastrar(&agenda);
break;
case 2:
case 3:
break;
case 4:
imprime(agenda);
break;
case 5:
break;
case 6:
agenda = iniciaAlocaAgenda();
break;
}
} while (op != 7);
liberaAgenda(agenda);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Thanks, just one more question on that part I’ve allotted AGENDA agenda = (AGENDA)malloc(sizeof(AGENDA) * MAX); struct, was not to have created the String matrix ?
– rafael marques
No, the only thing you did was create several AGENDA. In your case, it’s as if you were saving all contacts within a single AGENDA type variable as I understand it. So I suggested the idea of creating the Contact structure and then creating a contact vector, using the same malloc:
Contato *agenda = malloc (MAX * sizeof(Contato))
– João Pedro Henrique
Entedi Thank you very much
– rafael marques