I can not register a person's name in my calendar

-1

I am making a very simple agenda, the numbers I have managed to castrate however the names of the people I could not.

My code

#include <stdio.h>
#include <malloc.h>

#define MAX 50

typedef struct
{
  int numero[MAX];
  char nome[MAX];
  int tam;

} AGENDA;

void imprime(AGENDA *L);
void cadastrar(AGENDA *lista);
void inicia(AGENDA *lista);

int main(int argc, char** argv)
{
  AGENDA *agenda = (AGENDA*)malloc(sizeof(AGENDA) * MAX);
  int op;
  do
  {
     printf("1-adicionar numero\n");
     printf("2-remover numero\n");
     printf("3-procurar numero\n");
     printf("4-Imprimir\n");
     printf("6-incia-a-agenda\n");
     printf("7-Parar\n");
     scanf("%d", &op);
     switch(op)
     {
     case 1:
         cadastrar(agenda);
         break;
     case 6:
         inicia(agenda);
         break;
     case 4:
         imprime(agenda);
         break;
    }
 }
  while(op != 7);
   free(agenda);
   return 0;
 }
void cadastrar(AGENDA *lista)
{
 int op;
 do
 {
    printf("Digite o numero\n");
    scanf("%d", &lista->numero[lista->tam]);
    printf("Digite o seu nome\n");
    scanf(" %[^\n]", lista->nome[lista->tam]);
    printf("Digite 1 para continuar 2 para parar\n");
    scanf("%d", &op);
    lista->tam++;
 }
 while(op != 2);
}

void imprime(AGENDA *L)
{
 int i;
 for(i = 0; i < L->tam; i++)
 {
    printf("%d\n", L->numero[i]);
    printf("%s\n", L->nome[i]);
 }
}

void inicia(AGENDA *lista)
{
  lista->tam = 0;
}
    
asked by anonymous 29.06.2018 / 18:57

2 answers

3

The error lies in how you structured the data in the structure. nome must be an array, not a vector. Doing:

typedef struct
{
  int numero[MAX];
  char nome[MAX][MAX];
  int tam;

} AGENDA;

In your code today, you are trying to save a string to a char, which is not possible. Since you want to use multiple strings, what you should do is use an array of characters.

One extra comment I would like to make is that, in particular, I would attack the problem differently. Instead of having the struct save all the contacts, have each struct store a contact and from there make a vector of Contacts. So the structure would become:

typedef struct {
  int numero;
  char nome[MAX];
} Contato;
    
29.06.2018 / 19:05
5

I rewrote the code to show more or less how it is done. Note that there is still a lot of improvement in it. There are errors that can happen easily and are being checked, other details, but that's how you really do it, even something simple.

Placing multiple arrays within struct does not make any sense, what you want is simpler and more viable than this. Your calendar%% must only have 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 entry. If you want you can call it contact, but I do not know if it's suitable in a real context. In it will have the number and the name. The number I changed to string because it's usually the phone number, so struct is not appropriate . If it's something else, then it seems to make less sense to even let the person type this.

I have predefined that the name will have 30 characters, you can use the size you want.

I better control typing. You have flaws in using int so, but for an exercise that's fine.

I've given a better name to the function that initializes the calendar so that it's clear that you need to release it. And I created it before any way, even to avoid another mistake. And I changed the startup to reboot. There is an error there in the reboot, which I have in some way introduced, see if you can identify and fix it, so exercise really.

If you do not understand some point ask specific questions.

I made other simplifications and improvements. Since you want to learn pay attention to every detail, even in the spaces. I had others wrong that I corrected, it gets exercise to 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);
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
30.06.2018 / 08:49