Vector struct and pointers

2

I have to get data (name and phone) from some people in a struct , then store them in a vector, all by a function / procedure. At the time of printing some strange characters come out.

I'm using the C ++ DEV platform

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

#define MAX 2

typedef struct dadosPessoais    
    { 
        char nome = ' ';
        int telefone = -1;
    }DadosPessoais; 

void inserir(DadosPessoais *vetor);
void listar(DadosPessoais *vetor);

int main()
{

    int escolha=1;
    DadosPessoais vetor[MAX];

// se a escolha for diferente de 3, ele continua... o que inicialmente é verdade
// pois escolha é igual a 1

    while (escolha!=5)
    {

        printf("\n\n ----------------------- ");

        printf("\n 1 - Inserir novo registro ");
        printf("\n 2 - Limpar registros da tabela ");
        printf("\n 3 - Fechar Programa ");
        printf("\n\n Escolha uma opcao: ");
        scanf("%d",&escolha);


// estrutura switch
        switch (escolha)
         {

            case 1:
            {
                system ("cls");
                inserir(vetor);
                break;
            }

            case 2:
            {
                system ("cls");
                listar(vetor);
                break;
            }   

// opção padrão
            default:
            {
                system ("cls");

// se for escolhida a opção 3, ele pula o while utilizando continue para isso 
                if( escolha==3)
                {
                    continue;
                }
// caso o usuário digite um numero acima de 5, ele irá informar que nao existe essa opção
                printf("\n\n Nenhuma opcao foi escolhida ");
                break;
            }
        }   

    }
    if( escolha==3)
    printf("\n\n O Programa foi fechado");

    system("PAUSE"); 

}

void inserir(DadosPessoais *vetor)
{
    int x=3, i, espaco ;

    for(i=0; i<x; i++)
    {
        if (vetor[i].nome == ' ')
        {
            espaco = 1;
            break;
        }
        else
        {
            espaco = 2;
        }       

    }

    if (espaco == 1)
    {
        printf("Digite nome: \n");
        scanf(" %s", &vetor->nome);

        printf("Digite o telefone: \n ");
        scanf(" %d", &vetor->telefone);
    }
    else
    {
        printf("Nao ha espaco vago \n ");
    }
}

void listar(DadosPessoais *vetor)
{
    int x=MAX, i, espaco ;

    for(i=0; i<x; i++)
    {
        if (vetor[i].nome != ' ')
        {
            printf(" %c", &vetor[x].nome);
            printf("\n");
            printf(" %c", &vetor[x].telefone);
            printf("\n");
        }
        else
        {
            printf("Vetor vazio");
        }       
    }

}
    
asked by anonymous 15.10.2014 / 23:51

1 answer

3

The question could be considered too broad because it has several problems. Stack Overflow works best with well-defined problems with well-focused questions.

I'll list some problems in your code that need fixing. From there you'll be able to improve some things and be able to ask more focused questions when you have a more specific problem.

  • Type char stores only one character. To store several characters you need to declare as array , something like char nome[40] where you will reserve 40 characters within struct itself which is quite inefficient so it can be used for didactic purposes. Or you can declare as char * nome and it can be of indeterminate size, occupying only space for a pointer (there you have to do the allocation out).
  • You do not initialize the vector. That's why it's very easy to get a lot of junk. The fact that you reserve memory for the variable in the statement does not mean that memory is cleared.
  • You are using an integer to save the phone. Although a phone number looks like a numeric value it is not. I would use a string for this as well. Remembering that if you insist on using int , you can not use numbers longer than 9 digits or start with zero (which technically does not exist).
  • This if ( escolha==3) within default is very strange. If you want to do something when the choice is 3, mount case to this. And put everything there, you do not have to have the other if equal below. It is weird to while execute until escolha is 5. And another detail, you only need a system ("cls"); in all your code.
  • This way of checking if there is space in the vector to put new data is extra flawed. You would need to initialize the data with NULL and check this.
  • This espaco variable is completely unnecessary. If you find vague space in the vector, you process the inclusion there in that if , you do not need to create a confusing algorithm.
  • If you are using a vector, you must use an index on the variable. In scan you probably wanted to &vetor[i]->nome . And in the other probably &vetor[i].telefone (if using a int and not a pointer to int , would have to use the . operator and not the -> operator.
  • Then in printf() you are using the wrong operator again. You are prompted to print the pointer when you want to print the content pointed to by it, like: &vetor[x]->nome .
  • No printf() would have to use the %s and %d specifiers. The same error was committed in scanf() .
  • You are using the variable x in loop but it does not change, who is varying is i .
  • You can only say that the vector is empty if it passes through for and no item is printed, you can not say this while it is inside for .
  • There are a number of other small details that will not make as much difference as unused variables, sometimes using MAX and sometimes using a literal 3 that does not even match the current value of MAX , inconsistency between usage pointer and array , comments that claim to do something that is not real, etc. that make your code weird or even wrong.

    I did not do a very thorough analysis. There may be more mistakes or I may have misunderstood. You need to improve by parts.

    I think I've helped a lot, more than just doing it for you and I'm sure you want to do it on your own.

        
    16.10.2014 / 01:52