I can’t seem to register a person’s name in my calendar

Asked

Viewed 71 times

-1

I’m making a very simple schedule, the numbers I managed to castrate but the names of the people I couldn’t get.

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

2 answers

5

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.

3


The error is in how you structured the data in the structure. nome should be a matrix, not a vector. Making:

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

} AGENDA;

In your code currently, 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 a character array.

An extra comment I would like to make is that, in particular, I would attack the problem in another way. Instead of making the struct store all contacts, make each struct store a contact and from there make a vector of contacts. Soon, the structure would become:

typedef struct {
  int numero;
  char nome[MAX];
} Contato;
  • 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 ?

  • 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))

  • Entedi Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.